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

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Sep 21, 2023

No description provided.

@staabm
Copy link
Contributor Author

staabm commented Sep 21, 2023

shouldn't this tests pass?

@staabm
Copy link
Contributor Author

staabm commented Sep 21, 2023

failure in

2) Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Php80Test::test with data set #3
Failed on fixture file "pathinfo_return.php.inc"
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
      *
      * @psalm-assert-if-true =non-empty-string $filename
      */
-    public static function extension($filename): string|array
+    public static function extension($filename): array|string
     {
         return pathinfo($filename, PATHINFO_EXTENSION);
     }

/home/runner/work/rector-src/rector-src/packages/Testing/PHPUnit/AbstractRectorTestCase.php:239
/home/runner/work/rector-src/rector-src/packages/Testing/PHPUnit/AbstractRectorTestCase.php:171
/home/runner/work/rector-src/rector-src/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Php80Test.php:16

is unrelated

@staabm staabm changed the title [TypeDeclaration] Skip ArrayDimFetch for native array-shapes [TypeDeclaration] Skip ArrayDimFetch for optional native array-shapes Sep 21, 2023
@staabm staabm changed the title [TypeDeclaration] Skip ArrayDimFetch for optional native array-shapes [TypeDeclaration] Skip ArrayDimFetch for optional items in native array-shapes Sep 21, 2023
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
@staabm staabm marked this pull request as ready for review September 21, 2023 15:19
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

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.

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.

2 participants