Skip to content

Error in ServerRequest attributes return type #183

@BobMali

Description

@BobMali

Hi there,

first of all i want to thank you for your great work in the open source community!

I am using the following code in a TYPO3 hook:

$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
if (!$request instanceof ServerRequestInterface) {
    return;
}

// Check if the current module is the file list module
$route = $request->getAttribute('route');
if ($route?->getOption('_identifier') !== 'media_management') {
    return;
}

However, this produces the following error in phpstan:

⚠️ Result is incomplete because of severe errors. ⚠️
Fix these errors first and then re-run PHPStan
to get all reported errors.

When using phpstan in verbose mode i get the following stack trace:

Stack trace

/var/www/html/Source/vendor/saschaegerer/phpstan-typo3/src/Type/RequestDynamicReturnTypeExtension.php(57)

 #0 phar:///var/www/html/Source/bin/phpstan/src/Analyser/MutatingScope.php(3831):                                                                                                                              
 SaschaEgerer\PhpstanTypo3\Type\RequestDynamicReturnTypeExtension->getTypeFromMethodCall(Object(PHPStan\Reflection\ResolvedMethodReflection), Object(PhpParser\Node\Expr\MethodCall),                          
 Object(PHPStan\Analyser\MutatingScope))                                                                                                                                                                       
 #1 phar:///var/www/html/Source/bin/phpstan/src/Analyser/MutatingScope.php(1491): PHPStan\Analyser\MutatingScope->methodCallReturnType(Object(PHPStan\Type\ObjectType), 'getAttribute',                        
 Object(PhpParser\Node\Expr\MethodCall))                                                                                                                                                                       
 #2 phar:///var/www/html/Source/bin/phpstan/src/Analyser/MutatingScope.php(1497): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()                                                                 
 #3 phar:///var/www/html/Source/bin/phpstan/src/Analyser/MutatingScope.php(546): PHPStan\Analyser\MutatingScope->resolveType('$request->getAt...', Object(PhpParser\Node\Expr\MethodCall))                     
 #4 phar:///var/www/html/Source/bin/phpstan/src/Rules/PhpDoc/VarTagTypeRuleHelper.php(80): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))                                     
 #5 phar:///var/www/html/Source/bin/phpstan/src/Rules/PhpDoc/VarTagTypeRuleHelper.php(49): PHPStan\Rules\PhpDoc\VarTagTypeRuleHelper->checkExprType(Object(PHPStan\Analyser\MutatingScope),                    
 Object(PhpParser\Node\Expr\MethodCall), Object(PHPStan\Type\ObjectType))                                                                                                                                      
 #6 phar:///var/www/html/Source/bin/phpstan/src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php(136): PHPStan\Rules\PhpDoc\VarTagTypeRuleHelper->checkVarType(Object(PHPStan\Analyser\MutatingScope),           
 Object(PhpParser\Node\Expr\Variable), Object(PhpParser\Node\Expr\MethodCall), Array, Array)                                                                                                                   
 #7 phar:///var/www/html/Source/bin/phpstan/src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php(213):                                                                                                           
 PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule->processAssign(Object(PHPStan\Analyser\MutatingScope), Object(PhpParser\Node\Expr\Variable), Object(PhpParser\Node\Expr\MethodCall), Array)                
 #8 phar:///var/www/html/Source/bin/phpstan/src/Rules/PhpDoc/WrongVariableNameInVarTagRule.php(76):                                                                                                            
 PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule->processExpression(Object(PHPStan\Analyser\MutatingScope), Object(PhpParser\Node\Expr\Assign), Array)                                                      
 #9 phar:///var/www/html/Source/bin/phpstan/src/Analyser/FileAnalyser.php(103): PHPStan\Rules\PhpDoc\WrongVariableNameInVarTagRule->processNode(Object(PhpParser\Node\Stmt\Expression),                        
 Object(PHPStan\Analyser\MutatingScope))                                                                                                                                                                       
 #10 phar:///var/www/html/Source/bin/phpstan/src/Node/ClassStatementsGatherer.php(116): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Expression),                      
 Object(PHPStan\Analyser\MutatingScope))                                                                                                                                                                       
 #11 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(648): PHPStan\Node\ClassStatementsGatherer->__invoke(Object(PhpParser\Node\Stmt\Expression),                                   
 Object(PHPStan\Analyser\MutatingScope))                                                                                                                                                                       
 #12 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(520): PHPStan\Analyser\NodeScopeResolver::PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Expression),                   
 Object(PHPStan\Analyser\MutatingScope))                                                                                                                                                                       
 #13 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(450): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Expression),                              
 Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))                                                                                                           
 #14 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(647): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\ClassMethod), Array,                     
 Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))                                                                                                           
 #15 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(450): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\ClassMethod),                             
 Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))                                                                              
 #16 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(787): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Class_), Array,                          
 Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))                                                                              
 #17 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(450): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_),                                  
 Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))                                                                                                           
 #18 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(754): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Namespace_), Array,                      
 Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))                                                                                                           
 #19 phar:///var/www/html/Source/bin/phpstan/src/Analyser/NodeScopeResolver.php(398): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Namespace_),                              
 Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))                                                                                                           
 #20 phar:///var/www/html/Source/bin/phpstan/src/Analyser/FileAnalyser.php(162): PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))              
 #21 phar:///var/www/html/Source/bin/phpstan/src/Command/WorkerCommand.php(136): PHPStan\Analyser\FileAnalyser->analyseFile('/var/www/html/S...', Array, Object(PHPStan\Rules\LazyRegistry),                   
 Object(PHPStan\Collectors\Registry), NULL)                                                                                                                                                                    
 #22 phar:///var/www/html/Source/bin/phpstan/vendor/evenement/evenement/src/EventEmitterTrait.php(111): PHPStan\Command\WorkerCommand::PHPStan\Command\{closure}(Array)                                        
 #23 phar:///var/www/html/Source/bin/phpstan/vendor/clue/ndjson-react/src/Decoder.php(117): _PHPStan_a54cdb067\Evenement\EventEmitter->emit('data', Array)                                                     
 #24 phar:///var/www/html/Source/bin/phpstan/vendor/evenement/evenement/src/EventEmitterTrait.php(111): _PHPStan_a54cdb067\Clue\React\NDJson\Decoder->handleData(Array)                                        
 #25 phar:///var/www/html/Source/bin/phpstan/vendor/react/stream/src/Util.php(62): _PHPStan_a54cdb067\Evenement\EventEmitter->emit('data', Array)                                                              
 #26 phar:///var/www/html/Source/bin/phpstan/vendor/evenement/evenement/src/EventEmitterTrait.php(111): _PHPStan_a54cdb067\React\Stream\Util::_PHPStan_a54cdb067\React\Stream\{closure}('{"action":"anal...')  
 #27 phar:///var/www/html/Source/bin/phpstan/vendor/react/stream/src/DuplexResourceStream.php(168): _PHPStan_a54cdb067\Evenement\EventEmitter->emit('data', Array)                                             
 #28 phar:///var/www/html/Source/bin/phpstan/vendor/react/event-loop/src/StreamSelectLoop.php(201): _PHPStan_a54cdb067\React\Stream\DuplexResourceStream->handleData(Resource id #13268)                       
 #29 phar:///var/www/html/Source/bin/phpstan/vendor/react/event-loop/src/StreamSelectLoop.php(173): _PHPStan_a54cdb067\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)                           
 #30 phar:///var/www/html/Source/bin/phpstan/src/Command/WorkerCommand.php(96): _PHPStan_a54cdb067\React\EventLoop\StreamSelectLoop->run()                                                                     
 #31 phar:///var/www/html/Source/bin/phpstan/vendor/symfony/console/Command/Command.php(259): PHPStan\Command\WorkerCommand->execute(Object(_PHPStan_a54cdb067\Symfony\Component\Console\Input\ArgvInput),     
 Object(_PHPStan_a54cdb067\Symfony\Component\Console\Output\ConsoleOutput))                                                                                                                                    
 #32 phar:///var/www/html/Source/bin/phpstan/vendor/symfony/console/Application.php(870):                                                                                                                      
 _PHPStan_a54cdb067\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_a54cdb067\Symfony\Component\Console\Input\ArgvInput),                                                                       
 Object(_PHPStan_a54cdb067\Symfony\Component\Console\Output\ConsoleOutput))                                                                                                                                    
 #33 phar:///var/www/html/Source/bin/phpstan/vendor/symfony/console/Application.php(261): _PHPStan_a54cdb067\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\WorkerCommand),        
 Object(_PHPStan_a54cdb067\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_a54cdb067\Symfony\Component\Console\Output\ConsoleOutput))                                                              
 #34 phar:///var/www/html/Source/bin/phpstan/vendor/symfony/console/Application.php(157):                                                                                                                      
 _PHPStan_a54cdb067\Symfony\Component\Console\Application->doRun(Object(_PHPStan_a54cdb067\Symfony\Component\Console\Input\ArgvInput),                                                                         
 Object(_PHPStan_a54cdb067\Symfony\Component\Console\Output\ConsoleOutput))                                                                                                                                    
 #35 phar:///var/www/html/Source/bin/phpstan/bin/phpstan(114): _PHPStan_a54cdb067\Symfony\Component\Console\Application->run()                                                                                 
 #36 phar:///var/www/html/Source/bin/phpstan/bin/phpstan(115): _PHPStan_a54cdb067\{closure}()                                                                                                                  
 #37 /var/www/html/Source/bin/phpstan(6): require('phar:///var/www...')                                                                                                                                        
 #38 {main}

When i comment out this code block the error is gone:

$route = $request->getAttribute('route');
if ($route?->getOption('_identifier') !== 'media_management') {
    return;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions