phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Call to a member function getConfiguration() on null

Open h4rvk4 opened this issue 3 years ago • 7 comments

Bug report

Upgraded phpstan from 1.3.3 to 1.4.3 and now I get exceptions.

I don't know if the error trace is really helpfull, but it says it wants to be posted here.

PS: I figured out, that it's not a phpstan-core problem. The problem is raised by phpstan/phpstan-doctrine which was updated from 1.0.4 to 1.2.8.

PPS: Currently used doctrine ORM version is 2.11.1.

Exception message

(Message repleats for many different files)

` Internal error: Internal error: Call to a member function getConfiguration() on null in file /path/to/file.php

 Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md:                                                                                           
 #0 /code/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(144): Doctrine\ORM\Mapping\ClassMetadataFactory->completeIdGeneratorMapping()                                      
 phpstan/phpstan#1 /code/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php(391): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata()                          
 phpstan/phpstan#2 /code/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php(279): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata()            
 phpstan/phpstan#3 /code/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/ObjectMetadataResolver.php(129): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor()                           
 phpstan/phpstan#4 /code/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php(153): PHPStan\Type\Doctrine\ObjectMetadataResolver->getClassMetadata()                     
 phpstan/phpstan#5 /code/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/GetRepositoryDynamicReturnTypeExtension.php(112): PHPStan\Type\Doctrine\GetRepositoryDynamicReturnTypeExtension->getRepositoryClass()  
 phpstan/phpstan#6 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3609): PHPStan\Type\Doctrine\GetRepositoryDynamicReturnTypeExtension->getTypeFromMethodCall()                  
 phpstan/phpstan#7 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1701): PHPStan\Analyser\MutatingScope->methodCallReturnType()                                                  
 phpstan/phpstan#8 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1707): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()                                            
 phpstan/phpstan#9 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(533): PHPStan\Analyser\MutatingScope->resolveType()                                                            
 phpstan/phpstan#10 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1701): PHPStan\Analyser\MutatingScope->getType()                                                              
 phpstan/phpstan#11 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1707): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()                                           
 phpstan/phpstan#12 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(533): PHPStan\Analyser\MutatingScope->resolveType()                                                           
 phpstan/phpstan#13 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(789): PHPStan\Analyser\MutatingScope->getType()                                                               
 phpstan/phpstan#14 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(533): PHPStan\Analyser\MutatingScope->resolveType()                                                           
 phpstan/phpstan#15 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(1380): PHPStan\Analyser\MutatingScope->getType()                                                          
 phpstan/phpstan#16 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(496): PHPStan\Analyser\NodeScopeResolver->findEarlyTerminatingExpr()                                      
 phpstan/phpstan#17 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                               
 phpstan/phpstan#18 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(460): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()                                              
 phpstan/phpstan#19 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                               
 phpstan/phpstan#20 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(544): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()                                              
 phpstan/phpstan#21 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(298): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                               
 phpstan/phpstan#22 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(510): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()                                              
 phpstan/phpstan#23 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(269): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                               
 phpstan/phpstan#24 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(189): PHPStan\Analyser\NodeScopeResolver->processNodes()                                                       
 phpstan/phpstan#25 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(143): PHPStan\Analyser\FileAnalyser->analyseFile()                                                             
 phpstan/phpstan#26 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}()               
 phpstan/phpstan#27 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(110): _PHPStan_daf7d5577\Evenement\EventEmitter->emit()                                             
 phpstan/phpstan#28 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_daf7d5577\Clue\React\NDJson\Decoder->handleData()               
 phpstan/phpstan#29 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62): _PHPStan_daf7d5577\Evenement\EventEmitter->emit()                                                      
 phpstan/phpstan#30 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):                                                                          
 _PHPStan_daf7d5577\React\Stream\Util::_PHPStan_daf7d5577\React\Stream\{closure}()                                                                                                                 
 phpstan/phpstan#31 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154): _PHPStan_daf7d5577\Evenement\EventEmitter->emit()                                     
 phpstan/phpstan#32 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(200): _PHPStan_daf7d5577\React\Stream\DuplexResourceStream->handleData()                    
 phpstan/phpstan#33 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(172): _PHPStan_daf7d5577\React\EventLoop\StreamSelectLoop->waitForStreamActivity()          
 phpstan/phpstan#34 phar:///code/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(106): _PHPStan_daf7d5577\React\EventLoop\StreamSelectLoop->run()                                               
 phpstan/phpstan#35 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259): PHPStan\Command\WorkerCommand->execute()                                                    
 phpstan/phpstan#36 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(848): _PHPStan_daf7d5577\Symfony\Component\Console\Command\Command->run()                             
 phpstan/phpstan#37 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(259): _PHPStan_daf7d5577\Symfony\Component\Console\Application->doRunCommand()                        
 phpstan/phpstan#38 phar:///code/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157): _PHPStan_daf7d5577\Symfony\Component\Console\Application->doRun()                               
 phpstan/phpstan#39 phar:///code/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(94): _PHPStan_daf7d5577\Symfony\Component\Console\Application->run()                                                             
 phpstan/phpstan#40 phar:///code/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(95): _PHPStan_daf7d5577\{closure}()                                                                                              
 phpstan/phpstan#41 /code/vendor/phpstan/phpstan/phpstan(8): require('...')                                                                                                                                       
 phpstan/phpstan#42 {main}

`

h4rvk4 avatar Jan 31 '22 11:01 h4rvk4

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

mergeable[bot] avatar Jan 31 '22 11:01 mergeable[bot]

I had the same issue (but I'm totally new to phpstan and a php noob, so take my words lightly).

Recent versions of phpstan/phpstan-doctrine uses doctrine's ClassMetadataFactory (here, to get infer repositories etc I think). Doctrine's ClassMetadataFactory requires an EntityManagerInterface. I suppose that's where the getConfiguration() on null comes from.

So I followed to doc to provide the object manager of my Symphony 5 application :

parameters:
	doctrine:
		objectManagerLoader: tests/object-manager.php
// tests/object-manager.php

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

(new Dotenv())->bootEnv(__DIR__ . '/../.env');

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();

And it worked for me 🥳

The doc states You can opt in for more advanced analysis by providing the object manager from your own application. but I wonder if it has become a requirement.

I you/anybody can confirm this, we should open an issue on phpstan/phpstan-doctrine

yblatti avatar Feb 01 '22 09:02 yblatti

Just for the completion: here a repo to reproduce the problem: https://github.com/haruka/phpstan_demo

h4rvk4 avatar Mar 07 '22 14:03 h4rvk4

You can work around this problem by configuring objectManagerLoader as said above.

ondrejmirtes avatar Jun 09 '22 10:06 ondrejmirtes

For me it was reworking these annotations in the entity which was flagged by the phpstan:

Original with errors:

#[ORM\Id]
#[ORM\Column(name: "connection_id", type: "integer", nullable: true)]
#[ORM\GeneratedValue(strategy: "SEQUENCE")]
#[ORM\SequenceGenerator(sequenceName: "connection__connection_id_seq", allocationSize:1, initialValue:1)]
private ?int $id = null;

Updated without phpstan errors:

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\SequenceGenerator(sequenceName: "connection__connection_id_seq")]
#[ORM\Column(name: "connection_id", type: "integer")]
private ?int $id = null;

bestak avatar Dec 01 '22 15:12 bestak

Same problem here :

#[ORM\GeneratedValue(strategy: 'SEQUENCE')]

modified to

#[ORM\GeneratedValue]

solved the problem

AlanCrevon avatar Feb 27 '23 10:02 AlanCrevon

We ran into this issue when upgrading doctrine/orm 2.16.3 => 2.17.0.

The error occurred at https://github.com/doctrine/orm/pull/8931/files#diff-3e426fe47b58d2d4b970b1b7a0cbf5999af950e4f84e48dce4bee5061b57ed31R756

Changing the GeneratedValue strategy from AUTO to IDENTITY fixed the problem:

// From
#[ORM\GeneratedValue(strategy: 'AUTO')]
// To
#[ORM\GeneratedValue(strategy: 'IDENTITY')]

Note that we are using MySQL. You might need to use either SEQUENCE or IDENTITY, depending on your database. See https://github.com/greg0ire/doctrine-orm/blob/e89b680a285ea4c694cdaf9edb4490d904c23e54/UPGRADE.md#deprecated-reliance-on-the-non-optimal-defaults-that-come-with-the-auto-identifier-generation-strategy & https://github.com/greg0ire/doctrine-orm/blob/e89b680a285ea4c694cdaf9edb4490d904c23e54/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php#L76-L96

PHPStan output:

 -- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Error                                                                                                                                                                                                                
 -- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
     Internal error: Internal error: Call to a member function getConfiguration() on null while analysing file /var/www/admin/XXX/Comment.php                                            
                                                                                                                                                                                                                          
     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:                                                                                                            
     ## /var/www/admin/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(742)                                                                                                                         
     #0 /var/www/admin/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(625): Doctrine\ORM\Mapping\ClassMetadataFactory->determineIdGeneratorStrategy()                                              
     #1 /var/www/admin/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(156): Doctrine\ORM\Mapping\ClassMetadataFactory->completeIdGeneratorMapping()                                                
     #2 /var/www/admin/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(415): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata()                                             
     #3 /var/www/admin/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(300): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata()                               
     #4 /var/www/admin/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/ObjectMetadataResolver.php(124): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor()                                     
     #5 /var/www/admin/vendor/phpstan/phpstan-doctrine/src/Rules/Doctrine/ORM/PropertiesExtension.php(70): PHPStan\Type\Doctrine\ObjectMetadataResolver->getClassMetadata()                                               
     #6 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassPropertiesNode.php(149): PHPStan\Rules\Doctrine\ORM\PropertiesExtension->isInitialized()                                                  
     #7 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Properties/MissingReadOnlyPropertyAssignRule.php(33): PHPStan\Node\ClassPropertiesNode->getUninitializedProperties()                          
     #8 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(107): PHPStan\Rules\Properties\MissingReadOnlyPropertyAssignRule->processNode()                                           
     #9 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(656): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}()                                                    
     #10 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(388): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                                         
     #11 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(627): PHPStan\Analyser\NodeScopeResolver->processStmtNodes()                                                        
     #12 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(357): PHPStan\Analyser\NodeScopeResolver->processStmtNode()                                                         
     #13 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(166): PHPStan\Analyser\NodeScopeResolver->processNodes()                                                                 
     #14 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(132): PHPStan\Analyser\FileAnalyser->analyseFile()                                                                       
     #15 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}()                         
     #16 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(117): _PHPStan_c6b09fbdf\Evenement\EventEmitter->emit()                                                       
     #17 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_c6b09fbdf\Clue\React\NDJson\Decoder->handleData()                         
     #18 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62): _PHPStan_c6b09fbdf\Evenement\EventEmitter->emit()                                                                
     #19 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_c6b09fbdf\React\Stream\Util::_PHPStan_c6b09fbdf\React\Stream\{closure}()  
     #20 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154): _PHPStan_c6b09fbdf\Evenement\EventEmitter->emit()                                               
     #21 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(201): _PHPStan_c6b09fbdf\React\Stream\DuplexResourceStream->handleData()                              
     #22 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(173): _PHPStan_c6b09fbdf\React\EventLoop\StreamSelectLoop->waitForStreamActivity()                    
     #23 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(98): _PHPStan_c6b09fbdf\React\EventLoop\StreamSelectLoop->run()                                                          
     #24 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259): PHPStan\Command\WorkerCommand->execute()                                                              
     #25 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(870): _PHPStan_c6b09fbdf\Symfony\Component\Console\Command\Command->run()                                       
     #26 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(261): _PHPStan_c6b09fbdf\Symfony\Component\Console\Application->doRunCommand()                                  
     #27 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157): _PHPStan_c6b09fbdf\Symfony\Component\Console\Application->doRun()                                         
     #28 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(124): _PHPStan_c6b09fbdf\Symfony\Component\Console\Application->run()                                                                      
     #29 phar:///var/www/admin/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(125): _PHPStan_c6b09fbdf\{closure}()                                                                                                       
     #30 /var/www/admin/vendor/phpstan/phpstan/phpstan(8): require('...')                                                                                                                                                 
     #31 /var/www/admin/bin/phpstan(117): include('...')                                                                                                                                                                  
     #32 {main}                                                                                                                                                                                                           
     Child process error (exit code 1):                                                                                                                                                                                   
 -- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 


                                                                                                                        
 [ERROR] Found 2 errors                                                                                                 
                                                                                                                        

orlandothoeny avatar Nov 16 '23 12:11 orlandothoeny