rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.28k stars 669 forks source link

System error: "Scope not available on "PhpParser\Node\Stmt\ClassMethod" node, but is required by a refactorWithScope() method of "Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector" rule. Fix scope refresh on changed nodes first #8624

Closed PeterHuefner closed 1 week ago

PeterHuefner commented 1 week ago

Dear rector-Team, you have made an awesome tool. It is really great! by using it I get an error on a file inside the library simplesamlphp:

[ERROR] Could not process                                                                                              
         "/var/www/html/protected/components/simplesaml/simplesaml/vendor/webmozart/assert/src/Mixin.php" file, due to: 
         "System error: "Scope not available on "PhpParser\Node\Stmt\ClassMethod" node, but is required by a            
         refactorWithScope() method of "Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector" rule. Fix scope
         refresh on changed nodes first"                                                                                

         Stack trace:                                                                                                   
         #0 vendor/rector/rector/src/Rector/AbstractRector.php(136): Rector\Rector\AbstractScopeAwareRector->refactor() 
         #1 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(176):                          
         Rector\Rector\AbstractRector->enterNode()                                                                      
         #2 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):                          
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #3 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):                          
         PhpParser\NodeTraverser->traverseNode()                                                                        
         #4 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):                          
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #5 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):                          
         PhpParser\NodeTraverser->traverseNode()                                                                        
         #6 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(85):                           
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #7 vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php(41):                               
         PhpParser\NodeTraverser->traverse()                                                                            
         #8 vendor/rector/rector/src/Application/FileProcessor.php(111):                                                
         Rector\PhpParser\NodeTraverser\RectorNodeTraverser->traverse()                                                 
         #9 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(184):                                     
         Rector\Application\FileProcessor->processFile()                                                                
         #10 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(161):                                    
         Rector\Application\ApplicationFileProcessor->processFile()                                                     
         #11 vendor/rector/rector/src/Console/Command/WorkerCommand.php(118):                                           
         Rector\Application\ApplicationFileProcessor->processFiles()                                                    
         #12 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         Rector\Console\Command\WorkerCommand->Rector\Console\Command\{closure}()                                       
         #13 vendor/rector/rector/vendor/clue/ndjson-react/src/Decoder.php(117):                                        
         RectorPrefix202404\Evenement\EventEmitter->emit()                                                              
         #14 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202404\Clue\React\NDJson\Decoder->handleData()                                                     
         #15 vendor/rector/rector/vendor/react/stream/src/Util.php(62):                                                 
         RectorPrefix202404\Evenement\EventEmitter->emit()                                                              
         #16 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202404\React\Stream\Util::RectorPrefix202404\React\Stream\{closure}()                              
         #17 vendor/rector/rector/vendor/react/stream/src/DuplexResourceStream.php(154):                                
         RectorPrefix202404\Evenement\EventEmitter->emit()                                                              
         #18 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(201):                                
         RectorPrefix202404\React\Stream\DuplexResourceStream->handleData()                                             
         #19 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(173):                                
         RectorPrefix202404\React\EventLoop\StreamSelectLoop->waitForStreamActivity()                                   
         #20 vendor/rector/rector/src/Console/Command/WorkerCommand.php(89):                                            
         RectorPrefix202404\React\EventLoop\StreamSelectLoop->run()                                                     
         #21 vendor/rector/rector/vendor/symfony/console/Command/Command.php(327):                                      
         Rector\Console\Command\WorkerCommand->execute()                                                                
         #22 vendor/rector/rector/vendor/symfony/console/Application.php(960):                                          
         RectorPrefix202404\Symfony\Component\Console\Command\Command->run()                                      
         #23 vendor/rector/rector/vendor/symfony/console/Application.php(333):                                          
         RectorPrefix202404\Symfony\Component\Console\Application->doRunCommand()                                       
         #24 vendor/rector/rector/src/Console/ConsoleApplication.php(53):                                               
         RectorPrefix202404\Symfony\Component\Console\Application->doRun()                                              
         #25 vendor/rector/rector/vendor/symfony/console/Application.php(216):                                          
         Rector\Console\ConsoleApplication->doRun()                                                                     
         #26 vendor/rector/rector/bin/rector.php(130): RectorPrefix202404\Symfony\Component\Console\Application->run()  
         #27 vendor/rector/rector/bin/rector(5): require_once('...')                                                    
         #28 vendor/bin/rector(119): include('...')                                                                     
         #29 {main}". On line: 25

The Mixin.php.txt is attachted as txt, cause of file type restrictions.

Other files inside the simplesamlphp library are parsed with out errors.

I used rector 1.04, PHP 8.3.2. I would appreciate some help.

Thank you for creating rector and making it available to the community!

samsonasik commented 1 week ago

please provide code example and config at https://getrector.com/demo

You need to invest time to narrow down rules half by half until it found the minimal rule(s) that cause it, and the expected output.

Read this post: https://tomasvotruba.com/blog/2021/02/01/effective-debug-tricks-narrow-scoping/

samsonasik commented 1 week ago

Reproduced on interface Mixin with Webmozart\Assert namespace on ReturnTypeFromStrictConstantReturnRector

https://getrector.com/demo/e6b2e871-1b41-4897-8864-2f9aaad48de1

if webmozart/assert is located in vendor directory, rector should not run type declaration fix on it, as it may cause error, see this warning:

https://github.com/rectorphp/rector/blob/efb3c476c700fdd3a3a61819c4d1a6fdda91e741/src/Application/ApplicationFileProcessor.php#L105

samsonasik commented 1 week ago

Looking at your error, you're running on vendor/ directory, which should not, while it seems on non-root directory (as package), but that a vendor/ from composer.

If you want to apply it, you should apply it per-project basis, and only apply on "source" project that matter, excluding third party part, otherwise, you can get autoload jugling and crash.

samsonasik commented 2 days ago

While there is no longer Mixin interface on latest webmozart/assert, the issue is reproduced at :

should be fixed at:

due to Internal error. on PHPStan enterClass() on MutatingScope.

Still, same suggestion: you should apply it per-project basis, and only apply on "source" project that matter, excluding third party part, otherwise, you can get autoload jugling and crash.