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

False positive when instantiating an object from a string that contains a fully qualified class name #5002

Closed
AlexHowansky opened this issue May 13, 2021 · 3 comments

Comments

@AlexHowansky
Copy link

Bug report

This issue appeared in 0.12.86, and still exists in 0.12.87.

When instantiating a class from a string that contains the class name, PHPStan will error if the class name is fully qualified -- i.e., if it starts with a backslash.

class Foo {}

// works fine
$class = new Foo();

// works fine
$name = 'Foo';
$class = new $name();

// fails with "Instantiated class \Foo not found."
$name = '\Foo';
$class = new $name();

// fails with "Instantiated class \Foo not found."
$name = '\\Foo';
$class = new $name();

https://phpstan.org/r/fb04310a-7e86-45a4-bbdc-c306ccb91f09

Expected output

This is a false positive. No error should be reported.

@s3tezsky
Copy link

Hello there, we have a simillar issue to FQCN defined by strings in shopsys. Our issue is a bit different but I belive it is related (as @AlexHowansky we are experiencing it from version 0.12.86).
We have failing standards for creating mocks using FQCN with leading backslash, but there are only some of them reported.

See our output of phpstan:
https://github.com/shopsys/shopsys/pull/2318/checks?check_run_id=2577320726#step:4:406
and code of that build:
https://github.com/shopsys/shopsys/blob/1551dec992ea385a3c4f454bc7589949f2d2a3b0/packages/framework/tests/Unit/Model/Security/AuthenticatorTest.php#L22

As I wrote above, it is strange that not all of those occurences are reported (e.g. see line 18 in the same file).

Cheers!

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@428baf5

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants