-
Notifications
You must be signed in to change notification settings - Fork 576
Report array_column() reading a property absent from the array's object elements
#5887
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
Open
phpstan-bot
wants to merge
7
commits into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-vt4ilzi
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ab7169
Report `array_column()` reading a property absent from the array's ob…
staabm 2b4e252
Resolve array_column property checks through ArrayColumnHelper
phpstan-bot e65531c
Check array_column() argument count before reflection lookups
phpstan-bot ddc1425
Report array_column() reading a property absent from enum cases
phpstan-bot 891a66a
Add ArrayColumnRule test for union element type
phpstan-bot c15084f
Cover array_column() reading a missing property off DateTimeImmutable…
phpstan-bot 5ee45e2
Cover array_column() property checks with named arguments
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\ArgumentsNormalizer; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Rules\IdentifierRuleError; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\Php\ArrayColumnHelper; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function count; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * Reports `array_column()` calls reading a property that does not exist on the | ||
| * objects contained in the source array. | ||
| * | ||
| * @implements Rule<FuncCall> | ||
| */ | ||
| final class ArrayColumnRule implements Rule | ||
| { | ||
|
|
||
| public function __construct( | ||
| private readonly ReflectionProvider $reflectionProvider, | ||
| private readonly ArrayColumnHelper $arrayColumnHelper, | ||
| private readonly bool $treatPhpDocTypesAsCertain, | ||
| private readonly bool $treatPhpDocTypesAsCertainTip, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!($node->name instanceof Node\Name)) { | ||
| return []; | ||
| } | ||
|
|
||
| // array_column() requires at least the array and the column key - bail | ||
| // out before the more expensive reflection lookups below. | ||
| if (count($node->getArgs()) < 2) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { | ||
| return []; | ||
| } | ||
|
|
||
| $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); | ||
| if ($functionReflection->getName() !== 'array_column') { | ||
| return []; | ||
| } | ||
|
|
||
| $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( | ||
| $scope, | ||
| $node->getArgs(), | ||
| $functionReflection->getVariants(), | ||
| $functionReflection->getNamedArgumentsVariants(), | ||
| ); | ||
|
|
||
| $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); | ||
| if ($normalizedFuncCall === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $normalizedFuncCall->getArgs(); | ||
| if (count($args) < 2) { | ||
| return []; | ||
| } | ||
|
staabm marked this conversation as resolved.
|
||
|
|
||
| $arrayArg = $args[0]->value; | ||
| $valueType = $scope->getType($arrayArg)->getIterableValueType(); | ||
| $nativeValueType = $scope->getNativeType($arrayArg)->getIterableValueType(); | ||
|
|
||
| $errors = []; | ||
| foreach ($this->checkColumn($args[1]->value, $valueType, $nativeValueType, '#2 $column_key', $scope) as $error) { | ||
| $errors[] = $error; | ||
| } | ||
|
|
||
| if (count($args) >= 3) { | ||
| foreach ($this->checkColumn($args[2]->value, $valueType, $nativeValueType, '#3 $index_key', $scope) as $error) { | ||
| $errors[] = $error; | ||
| } | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| private function checkColumn(Node\Expr $columnExpr, Type $valueType, Type $nativeValueType, string $parameter, Scope $scope): array | ||
| { | ||
| $checkedValueType = $this->treatPhpDocTypesAsCertain ? $valueType : $nativeValueType; | ||
|
|
||
| $columnType = $scope->getType($columnExpr); | ||
| $missingProperties = $this->arrayColumnHelper->findMissingObjectProperties($checkedValueType, $columnType); | ||
| if ($missingProperties === []) { | ||
| return []; | ||
| } | ||
|
|
||
| $nativeMissingPropertyNames = []; | ||
| foreach ($this->arrayColumnHelper->findMissingObjectProperties($nativeValueType, $columnType) as $nativeMissingProperty) { | ||
| $nativeMissingPropertyNames[$nativeMissingProperty->getValue()] = true; | ||
| } | ||
|
|
||
| $errors = []; | ||
| foreach ($missingProperties as $propertyNameType) { | ||
| $errorBuilder = RuleErrorBuilder::message(sprintf( | ||
| 'Parameter %s of function array_column expects a valid property name, %s given, but %s does not have such property.', | ||
| $parameter, | ||
| $propertyNameType->describe(VerbosityLevel::value()), | ||
| $checkedValueType->describe(VerbosityLevel::typeOnly()), | ||
| ))->identifier('arrayColumn.property'); | ||
|
|
||
| if ( | ||
| $this->treatPhpDocTypesAsCertain | ||
| && $this->treatPhpDocTypesAsCertainTip | ||
| && !isset($nativeMissingPropertyNames[$propertyNameType->getValue()]) | ||
| ) { | ||
| $errorBuilder->treatPhpDocTypesAsCertainTip(); | ||
| } | ||
|
|
||
| $errors[] = $errorBuilder->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPStan\Type\Php\ArrayColumnHelper; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<ArrayColumnRule> | ||
| */ | ||
| class ArrayColumnRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new ArrayColumnRule( | ||
| self::createReflectionProvider(), | ||
| self::getContainer()->getByType(ArrayColumnHelper::class), | ||
| $this->shouldTreatPhpDocTypesAsCertain(), | ||
| true, | ||
| ); | ||
| } | ||
|
|
||
| #[RequiresPhp('>= 8.2')] | ||
| public function testRule(): void | ||
| { | ||
| $tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; | ||
| $this->analyse([__DIR__ . '/data/array-column.php'], [ | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'wrong_key' given, but ArrayColumnRuleTest\\NonFinalObject does not have such property.", | ||
| 72, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 76, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #3 \$index_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 78, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 79, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #3 \$index_key of function array_column expects a valid property name, 'missing2' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 79, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\Suit does not have such property.", | ||
| 87, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'value' given, but ArrayColumnRuleTest\\PureSuit does not have such property.", | ||
| 93, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\PureSuit does not have such property.", | ||
| 94, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'wrong_key' given, but ArrayColumnRuleTest\\NonFinalObject does not have such property.", | ||
| 108, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject|ArrayColumnRuleTest\\NonFinalObject does not have such property.", | ||
| 119, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'Price' given, but DateTimeImmutable does not have such property.", | ||
| 130, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #2 \$column_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 140, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #3 \$index_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 141, | ||
| $tipText, | ||
| ], | ||
| [ | ||
| "Parameter #3 \$index_key of function array_column expects a valid property name, 'missing' given, but ArrayColumnRuleTest\\FinalObject does not have such property.", | ||
| 142, | ||
| $tipText, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.