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

[Bug in >=1.2.0] PhpStan is unable to discover entities #262

Closed
keichinger opened this issue Jan 24, 2022 · 3 comments
Closed

[Bug in >=1.2.0] PhpStan is unable to discover entities #262

keichinger opened this issue Jan 24, 2022 · 3 comments

Comments

@keichinger
Copy link

Bug report

Affected versions: 1.2.0...1.2.3
Works in: <=1.1.0

Hi,

we're using PhpStan in our CI for a couple of years now in a lot of projects. Over the weekend, all of our CIs have gone from green to red when running using at least v1.2.0 or newer.
The projects who are being affected are all Symfony 5.4 apps, running with doctrine/dbal v3.3.0.

On the Symfony side, we're relying purely on auto_mapping and haven't explicitly specified paths to entities within bundles, other than the default configuration for our own app:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

Now, when our CI (or locally) tries to execute PhpStan using php vendor/bin/phpstan analyse --memory-limit 4G --ansi -c vendor-bin/test/vendor/becklyn/php-cs/phpstan/symfony.neon . (the config looks like this: https://github.com/Becklyn/php-cs/blob/release/phpstan/symfony.neon), we're getting an exception from the AnnotationDriver that is complaining about missing paths to our entities.

Is there a configuration that we're missing or is this simply a bug in either Doctrine or PhpStan?

Exception + Stacktrace
  • Prepended by „a very very long list of migrations and entities”

Uncaught Doctrine\Persistence\Mapping\MappingException: Specifying the paths to your entities is required in the AnnotationDriver to retrieve all class names. in /Users/keichinger/dev/current/censored/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:35
#0 /Users/keichinger/dev/current/censored/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php(200): Doctrine\Persistence\Mapping\MappingException::pathRequired()
#1 /Users/keichinger/dev/current/censored/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(310): Doctrine\Persistence\Mapping\Driver\AnnotationDriver->getAllClassNames()
#2 /Users/keichinger/dev/current/censored/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(216): Doctrine\ORM\Mapping\ClassMetadataFactory->addDefaultDiscriminatorMap(Object(Doctrine\ORM\Mapping\ClassMetadata))
#3 /Users/keichinger/dev/current/censored/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php(391): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
#4 /Users/keichinger/dev/current/censored/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php(279): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('App\Entity\Cont...')
#5 /Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/ObjectMetadataResolver.php(115): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('App\Entity\Cont...')
#6 /Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan-doctrine/src/Rules/Doctrine/ORM/PropertiesExtension.php(70): PHPStan\Type\Doctrine\ObjectMetadataResolver->getClassMetadata('App\Entity\Cont...')
#7 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassPropertiesNode.php(121): PHPStan\Rules\Doctrine\ORM\PropertiesExtension->isInitialized(Object(PHPStan\Reflection\Php\PhpPropertyReflection), 'slot')
#8 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Properties/UninitializedPropertyRule.php(50): PHPStan\Node\ClassPropertiesNode->getUninitializedProperties(Object(PHPStan\Analyser\MutatingScope), Array, Array)
#9 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(94): PHPStan\Rules\Properties\UninitializedPropertyRule->processNode(Object(PHPStan\Node\ClassPropertiesNode), Object(PHPStan\Analyser\MutatingScope))
#10 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(545): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser{closure}(Object(PHPStan\Node\ClassPropertiesNode), Object(PHPStan\Analyser\MutatingScope))
#11 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#12 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(510): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Namespace_), Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#13 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(269): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Namespace_), Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#14 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(181): PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#15 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/Analyser.php(69): PHPStan\Analyser\FileAnalyser->analyseFile('/Users/keiching...', Array, Object(PHPStan\Rules\Registry), NULL)
#16 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyserRunner.php(62): PHPStan\Analyser\Analyser->analyse(Array, Object(Closure), NULL, true, Array)
#17 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseApplication.php(163): PHPStan\Command\AnalyserRunner->runAnalyser(Array, Array, Object(Closure), NULL, true, true, '/Users/keiching...', NULL, NULL, Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput))
#18 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseApplication.php(95): PHPStan\Command\AnalyseApplication->runAnalyser(Array, Array, true, '/Users/keiching...', Object(PHPStan\Command\Symfony\SymfonyOutput), Object(PHPStan\Command\Symfony\SymfonyOutput), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput))
#19 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseCommand.php(175): PHPStan\Command\AnalyseApplication->analyse(Array, false, Object(PHPStan\Command\Symfony\SymfonyOutput), Object(PHPStan\Command\Symfony\SymfonyOutput), false, true, '/Users/keiching...', Array, Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput))
#20 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259): PHPStan\Command\AnalyseCommand->execute(Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Output\ConsoleOutput))
#21 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(848): _PHPStan_70b6e53dc\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Output\ConsoleOutput))
#22 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(259): _PHPStan_70b6e53dc\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\AnalyseCommand), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Output\ConsoleOutput))
#23 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157): _PHPStan_70b6e53dc\Symfony\Component\Console\Application->doRun(Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_70b6e53dc\Symfony\Component\Console\Output\ConsoleOutput))
#24 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(94): _PHPStan_70b6e53dc\Symfony\Component\Console\Application->run()
#25 phar:///Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(95): _PHPStan_70b6e53dc{closure}()
#26 /Users/keichinger/dev/current/censored/vendor-bin/test/vendor/phpstan/phpstan/phpstan(7): require('phar:///Users/k...')
#27 /Users/keichinger/dev/current/censored/vendor/bin/phpstan(112): include('/Users/keiching...')
#28 {main}

Code snippet that reproduces the problem

Expected output

  • No internal exceptions

Did PHPStan help you today? Did it make you happy in any way?

PhpStan has been making me happy since one of the very first versions on a daily basis ❤️ That is also why I'm trying to support you guys through PhpStan Pro and through numerous bug reports and issues :)

@ondrejmirtes
Copy link
Member

Hello, sorry for the inconvenience! phpstan-doctrine 1.2.0 tries to bring some of the benefits of having a configured objectManagerLoader (https://github.com/phpstan/phpstan-doctrine/#configuration) to those users who didn't configure it. And after the release I'm discovering all the edge-cases and situations that need to be thought of.

Please try phpstan-doctrine 1.2.5: https://github.com/phpstan/phpstan-doctrine/releases/tag/1.2.5

And feel free to configure objectManagerLoader to see everything phpstan-doctrine has to offer :)

@keichinger
Copy link
Author

@ondrejmirtes thanks for the awesome and very fast fix! It's working again 🥰

@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 Feb 25, 2022
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

2 participants