Skip to content

Commit 8562844

Browse files
Add checkBenevolentUnionTypes docs
1 parent 5892281 commit 8562844

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

website/src/config-reference.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,38 @@ parameters:
457457

458458
When set to `true`, PHPStan is strict about values with an unspecified (implicit `mixed`) type. It enables the same checks for values with no type specified that rule level 9 enables for explicitly specified `mixed` type values.
459459

460+
### `checkBenevolentUnionTypes`
461+
462+
<div class="text-xs inline-block border border-green-600 text-green-600 bg-green-100 rounded px-1 mb-4">Available in PHPStan 1.9.0</div>
463+
464+
**default**: `false`
465+
466+
PHPStan defines benevolent union types, such as `array-key`. Benevolent unions aren't checked strictly even at the highest level:
467+
468+
```php
469+
public function requireInt(int $value): void {}
470+
public function requireString(string $value): void {}
471+
472+
/**
473+
* @param array-key $value1 // array-key is a benevolent union (int|string)
474+
* @param int|string $value2
475+
*/
476+
public function test($value1, int|string $value2): int
477+
{
478+
$this->requireInt($value1); // No error
479+
$this->requireString($value1); // No error
480+
$this->requireInt($value2); // Error
481+
$this->requireString($value2); // Error
482+
}
483+
```
484+
485+
Enable stricter analysis of benevolent union types with the `checkBenevolentUnionTypes` option (needs level 7 or higher):
486+
487+
```yaml
488+
parameters:
489+
checkBenevolentUnionTypes: true
490+
```
491+
460492
Exceptions
461493
------------
462494

0 commit comments

Comments
 (0)