Skip to content

Commit

Permalink
ForbidNotNormalizedTypeRule (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Dec 12, 2023
1 parent d5ec924 commit 0630e81
Show file tree
Hide file tree
Showing 5 changed files with 835 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -89,6 +89,9 @@ parameters:
enabled: true
forbidMethodCallOnMixed:
enabled: true
forbidNotNormalizedTypeRule:
enabled: true
checkDisjunctiveNormalForm: true
forbidNullInAssignOperations:
enabled: true
blacklist: ['??=']
Expand Down Expand Up @@ -563,6 +566,24 @@ function example($unknown) {
}
```

### forbidNotNormalizedType
- Reports any PhpDoc or native type that is not normalized, which can be:
- when child and parent appears in its union or intersection
- when same type appears multiple times in its union or intersection
- when DNF is not used
- configurable by `checkDisjunctiveNormalForm`
- Main motivation here is that PHPStan normalizes all types before analysis, so it is better to see it in codebase the same way PHPStan does

```php
/**
* @return mixed|false // denied, this is still just mixed
*/
public function getAttribute(string $name)
{
return $this->attributes[$name] ?? false;
}
```

### forbidNullInAssignOperations
- Denies using [assign operators](https://www.php.net/manual/en/language.operators.assignment.php) if null is involved on right side
- You can configure which operators are ignored, by default only `??=` is excluded
Expand Down
14 changes: 14 additions & 0 deletions rules.neon
Expand Up @@ -68,6 +68,9 @@ parameters:
enabled: true
forbidMethodCallOnMixed:
enabled: true
forbidNotNormalizedTypeRule:
enabled: true
checkDisjunctiveNormalForm: true
forbidNullInAssignOperations:
enabled: true
blacklist: ['??=']
Expand Down Expand Up @@ -174,6 +177,10 @@ parametersSchema:
forbidMethodCallOnMixed: structure([
enabled: bool()
])
forbidNotNormalizedTypeRule: structure([
enabled: bool()
checkDisjunctiveNormalForm: bool()
])
forbidNullInAssignOperations: structure([
enabled: bool()
blacklist: arrayOf(string())
Expand Down Expand Up @@ -265,6 +272,8 @@ conditionalTags:
phpstan.rules.rule: %shipmonkRules.forbidMatchDefaultArmForEnums.enabled%
ShipMonk\PHPStan\Rule\ForbidMethodCallOnMixedRule:
phpstan.rules.rule: %shipmonkRules.forbidMethodCallOnMixed.enabled%
ShipMonk\PHPStan\Rule\ForbidNotNormalizedTypeRule:
phpstan.rules.rule: %shipmonkRules.forbidNotNormalizedTypeRule.enabled%
ShipMonk\PHPStan\Rule\ForbidNullInAssignOperationsRule:
phpstan.rules.rule: %shipmonkRules.forbidNullInAssignOperations.enabled%
ShipMonk\PHPStan\Rule\ForbidNullInBinaryOperationsRule:
Expand Down Expand Up @@ -374,6 +383,11 @@ services:
class: ShipMonk\PHPStan\Rule\ForbidMethodCallOnMixedRule
arguments:
checkExplicitMixed: %checkExplicitMixed%

-
class: ShipMonk\PHPStan\Rule\ForbidNotNormalizedTypeRule
arguments:
checkDisjunctiveNormalForm: %shipmonkRules.forbidNotNormalizedTypeRule.checkDisjunctiveNormalForm%
-
class: ShipMonk\PHPStan\Rule\ForbidMatchDefaultArmForEnumsRule
-
Expand Down

0 comments on commit 0630e81

Please sign in to comment.