-
Notifications
You must be signed in to change notification settings - Fork 551
Respect phpstan level in ParameterCastableToNumberRule #4873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| use PHPStan\Analyser\ArgumentsNormalizer; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Node\Expr\TypeExpr; | ||
| use PHPStan\Reflection\ParameterReflection; | ||
| use PHPStan\Type\ErrorType; | ||
| use PHPStan\Type\Type; | ||
|
|
@@ -34,22 +35,31 @@ | |
| return null; | ||
| } | ||
|
|
||
| $typeResult = $this->ruleLevelHelper->findTypeToCheck( | ||
| $arrayTypeResult = $this->ruleLevelHelper->findTypeToCheck( | ||
| $scope, | ||
| $parameter->value, | ||
| '', | ||
| static fn (Type $type): bool => $type->isArray()->yes() && !$castFn($type->getIterableValueType()) instanceof ErrorType, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following change follows the same idea than https://github.com/phpstan/phpstan-src/pull/4166/changes for instance.
Here this allows to transform the iterable value type |
||
| static fn (Type $type): bool => $type->isArray()->yes(), | ||
|
Check warning on line 42 in src/Rules/ParameterCastableToStringCheck.php
|
||
| ); | ||
|
|
||
| $arrayType = $arrayTypeResult->getType(); | ||
| if (!$arrayType->isArray()->yes()) { | ||
| return null; | ||
| } | ||
|
|
||
| $typeResult = $this->ruleLevelHelper->findTypeToCheck( | ||
| $scope, | ||
| new TypeExpr($arrayType->getIterableValueType()), | ||
| '', | ||
| static fn (Type $type): bool => !$castFn($type) instanceof ErrorType, | ||
| ); | ||
|
|
||
| if ( | ||
| ! $typeResult->getType()->isArray()->yes() | ||
| || !$castFn($typeResult->getType()->getIterableValueType()) instanceof ErrorType | ||
| ) { | ||
| if (!$castFn($typeResult->getType()) instanceof ErrorType) { | ||
| return null; | ||
| } | ||
|
|
||
| return RuleErrorBuilder::message( | ||
| sprintf($errorMessageTemplate, $parameterName, $functionName, $typeResult->getType()->describe(VerbosityLevel::typeOnly())), | ||
| sprintf($errorMessageTemplate, $parameterName, $functionName, $arrayTypeResult->getType()->describe(VerbosityLevel::typeOnly())), | ||
| )->identifier('argument.type')->build(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| /** @var mixed $a */ | ||
| $a = doFoo(); | ||
| echo array_sum([$a]) . "\n\n"; | ||
| echo array_product([$a]) . "\n\n"; | ||
|
|
||
| $b = rand(0, 1) ? 42 : ''; | ||
| echo array_sum([$b]) . "\n\n"; | ||
| echo array_product([$b]) . "\n\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MixedType should not be transform automatically to ErrorType, we will rely on the ruleLevelHelper inside the parameterCastableToStringCheck to decide.