Skip to content

Fix resolving class const fetch for constant strings#1416

Merged
ondrejmirtes merged 1 commit intophpstan:1.7.xfrom
rvanvelzen:bug-7391
Jun 12, 2022
Merged

Fix resolving class const fetch for constant strings#1416
ondrejmirtes merged 1 commit intophpstan:1.7.xfrom
rvanvelzen:bug-7391

Conversation

@rvanvelzen
Copy link
Copy Markdown
Contributor

@ondrejmirtes ondrejmirtes merged commit f6f1631 into phpstan:1.7.x Jun 12, 2022
@ondrejmirtes
Copy link
Copy Markdown
Member

Thank you! Please also open a PR with that namespace slice fix 😊

@rvanvelzen rvanvelzen deleted the bug-7391 branch June 13, 2022 06:27
@mvorisek
Copy link
Copy Markdown
Contributor

Although the reported https://phpstan.org/r/89fb126f-4736-4208-b599-a5264d181e18 demo is fixed, I still need the following fix in my code /w phpstan 1.7.14:

    /**
     * @param MethodCall|StaticCall $methodCall
     */
    protected function getMethodCallScopeType(CallLike $methodCall, Scope $scope): Type
    {
        if ($methodCall instanceof StaticCall) {
+            // while loop needed to fix https://github.com/phpstan/phpstan/issues/7391
            $clExpr = $methodCall->class;
+            while ($clExpr instanceof ClassConstFetch && $clExpr->name instanceof Identifier && strtolower($clExpr->name->name) === 'class') {
+                $clExpr = $clExpr->class;
+            }
            $classNameType = $scope->getType(new ClassConstFetch($clExpr, 'class'));
            if ($classNameType instanceof ConstantStringType) {
                return new ObjectType($classNameType->getValue());
            } elseif ($classNameType instanceof GenericClassStringType) {
                return $classNameType->getGenericType();
            }

            throw new \Exception('Unexpected scope class name class: ' . get_class($classNameType));
        }

        return $scope->getType($methodCall->var);
    }

@rvanvelzen
Copy link
Copy Markdown
Contributor Author

Please create a new issue showing exactly what you're trying to accomplish. I though it was about 'Foo'::class which is resolved by #1425 but your code is not related to that at all.

@mvorisek
Copy link
Copy Markdown
Contributor

In custom phpstan extension, I need this code to resolve method class from (static::class)::createEntity() call.

@rvanvelzen
Copy link
Copy Markdown
Contributor Author

You can use $scope->resolveTypeByName() in case $methodCall->class is a Name:

$typeCallback = function () use ($node): Type {
if ($node->class instanceof Name) {
$staticMethodCalledOnType = $this->resolveTypeByName($node->class);
} else {
$staticMethodCalledOnType = TypeTraverser::map($this->getType($node->class), static function (Type $type, callable $traverse): Type {
if ($type instanceof UnionType) {
return $traverse($type);
}
if ($type instanceof GenericClassStringType) {
return $type->getGenericType();
}
if ($type instanceof ConstantStringType && $type->isClassString()) {
return new ObjectType($type->getValue());
}
return $type;
});

@mvorisek
Copy link
Copy Markdown
Contributor

I tried, but the $methodCall->class is instanceof PhpParser\Node\Expr\ClassConstFetch. I would expect $scope->getType(new ClassConstFetch($methodCall->class, 'class')) to resolve it, but as written above, it currently needs the while loop to unnest the ClassConstFetch.

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.

ClassConstFetch resolve is broken

3 participants