-
Notifications
You must be signed in to change notification settings - Fork 474
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
base: 2.1.x
Are you sure you want to change the base?
Conversation
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, | ||
); | ||
} |
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.
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.
I guess this logic should be moved into the type-system to make it also work for isset()
on ArrayAccess
objects
3c17859
to
4482676
Compare
closes phpstan/phpstan#3323 as described in comment