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

Using anonymous class breaks discovering symbols (Fatal error) #3663

Closed
davidbrnovjak opened this issue Jul 23, 2020 · 6 comments
Closed

Using anonymous class breaks discovering symbols (Fatal error) #3663

davidbrnovjak opened this issue Jul 23, 2020 · 6 comments

Comments

@davidbrnovjak
Copy link

davidbrnovjak commented Jul 23, 2020

Bug report

Using anonymous class definition inside another class leads to Fatal error during symbol discovery

Internal error: Internal error: Class 'Granularity' not found in file /var/www/html/sts3/app/modules/Statistics/Granularity.php

Removing the anonymous class definition resolves the error.

Code snippet that reproduces the problem

Unable to reproduce in playground.

Class snippet
<?php

declare(strict_types=1);

class Granularity
{
    protected static function provideInstances(): array
    {
        $myclass = new class() extends Granularity { };

        return [];
    }
}
PHPStan debug trace
PHP Fatal error:  Uncaught Error: Class 'Granularity' not found in phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129) : eval()'d code:1
Stack trace:
#0 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129): eval()
#1 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/ClassBlacklistReflectionProvider.php(97): PHPStan\Reflection\Runtime\RuntimeReflectionProvider->getAnonymousClassReflection(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope))
#2 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/ChainReflectionProvider.php(55): PHPStan\Reflection\ReflectionProvider\ClassBlacklistReflectionProvider->getAnonymousClassReflection(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope))
#3 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/MemoizingRef in phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129) : eval()'d code on line 1
Fatal error: Uncaught Error: Class 'Granularity' not found in phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129) : eval()'d code:1
Stack trace:
#0 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129): eval()
#1 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/ClassBlacklistReflectionProvider.php(97): PHPStan\Reflection\Runtime\RuntimeReflectionProvider->getAnonymousClassReflection(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope))
#2 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/ChainReflectionProvider.php(55): PHPStan\Reflection\ReflectionProvider\ClassBlacklistReflectionProvider->getAnonymousClassReflection(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope))
#3 phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/ReflectionProvider/MemoizingRef in phar:///var/www/html/sts3/vendor/phpstan/phpstan/phpstan/src/Reflection/Runtime/RuntimeReflectionProvider.php(129) : eval()'d code on line 1
@mergeable
Copy link

mergeable bot commented Jul 23, 2020

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@davidbrnovjak
Copy link
Author

davidbrnovjak commented Jul 24, 2020

Created repository with minimal code to reproduce. https://github.com/davidbrnovjak/phpstan_issue_3663

[root@brnovjakd1 phpstan_test]#  vendor/bin/phpstan analyze -c config.phpstan.neon --no-progress
 ------ --------------------------------------------------------------
  Line   Granularity.php
 ------ --------------------------------------------------------------
         Internal error: Class 'Granularity' not found
         Run PHPStan with --debug option and post the stack trace to:
         https://github.com/phpstan/phpstan/issues/new
 ------ --------------------------------------------------------------

Last version without this issue is 0.12.25 and can be reproduced using version 0.12.26 onwards.

@dbrekelmans
Copy link

Seems to work as expected: https://phpstan.org/r/eb5ce9c0-a6e3-4057-8bd9-f41591e4677a

Can you reproduce the issue on phpstan.org?

@ondrejmirtes
Copy link
Member

I confirm this is a real bug, It cannot be reproduced on phpstan.org because the bug is in OptimizedDirectorySourceLocator which the playground doesn't use.

@ondrejmirtes
Copy link
Member

Turns out the problem wasn't in OptimizedDirectorySourceLocator at all. The problem was that we couldn't safely use RuntimeReflectionProvider::getAnonymousClassReflection() with the hybrid reflection approach. So anonymous classes will always be analysed using static reflection from now on: phpstan/phpstan-src@4cad0c6

@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 May 14, 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