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

Fix resolving class const fetch for constant strings #1416

Merged
merged 1 commit into from
Jun 12, 2022

Conversation

rvanvelzen
Copy link
Contributor

@ondrejmirtes ondrejmirtes merged commit f6f1631 into phpstan:1.7.x Jun 12, 2022
@ondrejmirtes
Copy link
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
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
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
Contributor

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

@rvanvelzen
Copy link
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
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
3 participants