-
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, | ||
| 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) { | ||
|
Member
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. If you're not sure about how to work with RuleLevelHelper::findTypeToCheck, write thorough test for LevelsIntegrationTest. (The test regenerates the JSON files with expectations so 2nd run should always succeed.) Before level 7, it should only complain about a type that is never castable to number. Also I suspect we'll need also a separate test file for PHP 8.3+ because of supportsObjectsInArraySumProduct.
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. I added tests in LevelsIntegrationTest to show the result. I don't think it's worth using separate test file for PHP 8.3+ or - ; because the only difference is for some specific object type which are already tested. |
||
| 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,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects array, mixed given.", | ||
| "line": 26, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| [ | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 19, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 20, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 21, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 22, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 24, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 25, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Call to function array_sum() on a separate line has no effect.", | ||
| "line": 26, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 20, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Method Levels\\ArraySum\\Foo::test() has parameter $implicitlyMixed with no type specified.", | ||
| "line": 13, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<int|string> given.", | ||
| "line": 21, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects array, array<int>|Levels\\ArraySum\\Foo given.", | ||
| "line": 22, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects array, array<string>|Levels\\ArraySum\\Foo given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| }, | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects array, array<int|string>|Levels\\ArraySum\\Foo given.", | ||
| "line": 24, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects an array of values castable to number, array<string> given.", | ||
| "line": 23, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [ | ||
| { | ||
| "message": "Parameter #1 $array of function array_sum expects array, mixed given.", | ||
| "line": 25, | ||
| "ignorable": true | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace Levels\ArraySum; | ||
|
|
||
| class Foo | ||
| { | ||
| /** | ||
| * @param array<int> $arrayOfInt | ||
| * @param array<string> $arrayOfString | ||
| * @param array<int|string> $arrayOfUnion | ||
| * @param mixed $explicitlyMixed | ||
| */ | ||
| public function test(array $arrayOfInt, array $arrayOfString, array $arrayOfUnion, $explicitlyMixed, $implicitlyMixed): void | ||
| { | ||
| $objectOrArrayOfInt = rand(0, 1) ? new Foo() : $arrayOfInt; | ||
| $objectOrArrayOfString = rand(0, 1) ? new Foo() : $arrayOfString; | ||
| $objectOrArrayOfUnion = rand(0, 1) ? new Foo() : $arrayOfUnion; | ||
|
|
||
| array_sum($arrayOfInt); | ||
| array_sum($arrayOfString); | ||
| array_sum($arrayOfUnion); | ||
| array_sum($objectOrArrayOfInt); | ||
| array_sum($objectOrArrayOfString); | ||
| array_sum($objectOrArrayOfUnion); | ||
| array_sum($explicitlyMixed); | ||
| array_sum($implicitlyMixed); | ||
| } | ||
| } |
| 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.
The following change follows the same idea than https://github.com/phpstan/phpstan-src/pull/4166/changes for instance.
&&and||doesn't work always very well in UnionTypeCriteriaCallback it's sometimes better to split the check into multiple findTypeToCheck calls.Here this allows to transform the iterable value type
$type->getIterableValueType()into a StrictMixed when it's a Mixed for instance orarray<mixed>is never reported at any level.