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 ArrayAccess->offsetExists narrowing #3760

Draft
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Dec 29, 2024

closes phpstan/phpstan#3323 as described in comment

Comment on lines 69 to 81
foreach($scope->getType($node->var)->getObjectClassReflections() as $classReflection) {
$implementsTags = $classReflection->getImplementsTags();

if (
!isset($implementsTags[\ArrayAccess::class])
|| !$implementsTags[\ArrayAccess::class]->getType() instanceof GenericObjectType
) {
continue;
}

$implementsType = $implementsTags[\ArrayAccess::class]->getType();
$arrayAccessGenericTypes = $implementsType->getTypes();
if (!isset($arrayAccessGenericTypes[1])) {
continue;
}

return $this->typeSpecifier->create(
$node->var,
new HasOffsetValueType($keyType, $arrayAccessGenericTypes[1]),
$context,
$scope,
);
}
Copy link
Contributor Author

@staabm staabm Dec 29, 2024

Choose a reason for hiding this comment

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

reading the "raw" array-access generic type, because ObjectType->getValueType will append null for possibly non-existant offsets. the added null makes it impossible for us to differentiate a nullable-typed generic from a non-nullable generic.

https://3v4l.org/2uhaE


I guess this logic should be moved into the type-system to make it also work for isset() on ArrayAccess objects

@staabm staabm force-pushed the access branch 3 times, most recently from 3c17859 to 4482676 Compare December 29, 2024 13:58
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.

Add the sameBehaviour for offsetExists/offsetGet than isset
1 participant