-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
[TypeDeclaration] Skip ArrayDimFetch for optional items in native array-shapes #5060
Conversation
shouldn't this tests pass? |
failure in
is unrelated |
...ector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/native_array_shape.php.inc
Outdated
Show resolved
Hide resolved
...ector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/native_array_shape.php.inc
Outdated
Show resolved
Hide resolved
...hod/AddMethodCallBasedStrictParamTypeRector/Fixture/skip_native_optional_array_shape.php.inc
Outdated
Show resolved
Hide resolved
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
...ector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/native_array_shape.php.inc
Outdated
Show resolved
Hide resolved
...ector/ClassMethod/AddMethodCallBasedStrictParamTypeRector/Fixture/native_array_shape.php.inc
Outdated
Show resolved
Hide resolved
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
|
||
final class SkipNativeOptionalShape { | ||
private function doFoo() { | ||
$shape = pathinfo(''); |
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.
it seems pathinfo()
getNativeType() PHPStan bug, getting native type of $shape
itself got union array and string:
PHPStan\Type\UnionType #11106
types: array (2)
| 0 => PHPStan\Type\ArrayType #11170 ...
| 1 => PHPStan\Type\StringType #11098 ...
normalized: true
sortedTypes: true
cachedDescriptions: array (1)
| 4 => 'array|string'
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.
on native return, pathinfo()
got array
or string
https://www.php.net/manual/en/function.pathinfo.php
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.
I think the best way to verify if the shape is array or string is by using assertion, eg:
$shape = pathinfo('');
+ assert(is_array($shape));
$this->doBar($shape['dirname']); // dirname is only conditionally returned
then, it will be skipped
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.
it seems can be fixed with the following check:
$nativeVariableType = $scope->getNativeType($expr->var);
if (! $nativeVariableType instanceof MixedType && (! $nativeVariableType instanceof ArrayType || ! $nativeVariableType->getItemType() instanceof MixedType)) {
$type = $scope->getType($expr);
if ($expr->dim instanceof String_) {
$targetKey = null;
$variableType = $scope->getType($expr->var);
if ($variableType instanceof ArrayType) {
foreach ($variableType->getKeyTypes() as $key => $type) {
if ($type instanceof \PHPStan\Type\Constant\ConstantStringType && $type->getValue() === $expr->dim->value) {
$targetKey = $key;
break;
}
}
if ($targetKey !== null && in_array($targetKey, $variableType->getOptionalKeys(), true)) {
$type = $scope->getNativeType($expr);
}
}
}
}
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.
No description provided.