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

[TypeDeclaration] Skip ArrayDimFetch for optional items in native array-shapes #5060

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class NativeArrayShape {
private function doFoo() {
$shape = pathinfo('/www/htdocs/inc/lib.inc.php')
staabm marked this conversation as resolved.
Show resolved Hide resolved
$this->doBar($shape['basename']);
}

private function doBar($param) {

}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class NativeArrayShape {
private function doFoo() {
$shape = pathinfo('/www/htdocs/inc/lib.inc.php')
staabm marked this conversation as resolved.
Show resolved Hide resolved
$this->doBar($shape['basename']);
}

private function doBar(string $param) {

}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector\Fixture;

final class SkipNativeOptionalShape {
private function doFoo() {
$shape = pathinfo('');
Copy link
Member

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'

Copy link
Member

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

Copy link
Member

@samsonasik samsonasik Sep 21, 2023

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

Copy link
Member

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);
                        }
                    }
                }
            }

Copy link
Member

Choose a reason for hiding this comment

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

$this->doBar($shape['dirname']); // dirname is only conditionally returned
}

private function doBar($param) {

}
}
?>