Skip to content
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

Implement ArrayColumnRule #3706

Draft
wants to merge 4 commits into
base: 1.12.x
Choose a base branch
from
Draft

Implement ArrayColumnRule #3706

wants to merge 4 commits into from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Dec 4, 2024

closes phpstan/phpstan#5101
closes phpstan/phpstan#12188

ArrayColumnRule re-uses the logic from the array_column return type extension and reports problems when the logic implies the result will be a never-type.

there are more open problems with array_column and I would approach those after this PR got merged

@clxmstaab clxmstaab force-pushed the array-col branch 2 times, most recently from 0ab86d3 to 9345415 Compare December 4, 2024 14:44
@staabm
Copy link
Contributor Author

staabm commented Dec 4, 2024

@herndlm any opinions on the rule?

only the last commit is important. commits 1-3 are pure refactorings, which could land as a separate previous PR, in case ondrej is fine with the approach in general

btw: I have no idea why the test is failling only with PHP <= 8.1
-> found the cause: PHPStan is pessimistic and allows "dynamic properties" in PHP < 8.2

[$returnValueType, $iterableAtLeastOnce] = $this->arrayColumnHelper->getReturnValueType($arrayType, $columnKeyType, $scope);
if ($returnValueType instanceof NeverType || $iterableAtLeastOnce->no()) {
$errorBuilder = RuleErrorBuilder::message(sprintf(
'Cannot access column %s on %s.',
Copy link
Contributor Author

@staabm staabm Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will need a better message.. maybe mentioning array offsets or property access, but how to name it in unions of array/objects..?

maybe we can find a better generic message

@clxmstaab clxmstaab force-pushed the array-col branch 2 times, most recently from d6812de to adb4d58 Compare December 4, 2024 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant