rectorphp/rector-symfony

Error when running AddRouteAnnotationRector

hildebro opened this issue · 5 comments

This happens on quite a few routes in my project, but not on all of them

[ERROR] Could not process "/path/to/Controller.php" file, due to:    
         "System error: "Warning: Undefined array key 1"                                                                
                                                                                                                        
         Stack trace:                                                                                                   
         #0                                                                                                             
         vendor/rector/rector/vendor/rector/rector-symfony/rules/Configs/Rector/ClassMethod/AddRouteAnnotationRector.php
         (188):                                                                                                         
         Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser->createArrayFromValues()     
         #1                                                                                                             
         vendor/rector/rector/vendor/rector/rector-symfony/rules/Configs/Rector/ClassMethod/AddRouteAnnotationRector.php
         (151): Rector\Symfony\Configs\Rector\ClassMethod\AddRouteAnnotationRector->createCurlyQuoted()                 
         #2                                                                                                             
         vendor/rector/rector/vendor/rector/rector-symfony/rules/Configs/Rector/ClassMethod/AddRouteAnnotationRector.php
         (84): Rector\Symfony\Configs\Rector\ClassMethod\AddRouteAnnotationRector->createRouteItems()                   
         #3 vendor/rector/rector/src/Rector/AbstractRector.php(184):                                                    
         Rector\Symfony\Configs\Rector\ClassMethod\AddRouteAnnotationRector->refactor()                                 
         #4 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(176):                          
         Rector\Core\Rector\AbstractRector->enterNode()                                                                 
         #5 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(105):                          
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #6 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(196):                          
         PhpParser\NodeTraverser->traverseNode()                                                                        
         #7 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(85):                           
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #8 vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php(41):                               
         PhpParser\NodeTraverser->traverse()                                                                            
         #9 vendor/rector/rector/src/Application/FileProcessor.php(44):                                                 
         Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser->traverse()                                            
         #10 vendor/rector/rector/src/Application/FileProcessor/PhpFileProcessor.php(100):                              
         Rector\Core\Application\FileProcessor->refactor()                                                              
         #11 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(154):                                    
         Rector\Core\Application\FileProcessor\PhpFileProcessor->process()                                              
         #12 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(131):                                    
         Rector\Core\Application\ApplicationFileProcessor->processFile()                                                
         #13 vendor/rector/rector/packages/Parallel/WorkerRunner.php(56):                                               
         Rector\Core\Application\ApplicationFileProcessor->processFiles()                                               
         #14 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         Rector\Parallel\WorkerRunner->Rector\Parallel\{closure}()                                                      
         #15 vendor/rector/rector/vendor/clue/ndjson-react/src/Decoder.php(117):                                        
         RectorPrefix202309\Evenement\EventEmitter->emit()                                                              
         #16 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202309\Clue\React\NDJson\Decoder->handleData()                                                     
         #17 vendor/rector/rector/vendor/react/stream/src/Util.php(62):                                                 
         RectorPrefix202309\Evenement\EventEmitter->emit()                                                              
         #18 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202309\React\Stream\Util::RectorPrefix202309\React\Stream\{closure}()                              
         #19 vendor/rector/rector/vendor/react/stream/src/DuplexResourceStream.php(154):                                
         RectorPrefix202309\Evenement\EventEmitter->emit()                                                              
         #20 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(201):                                
         RectorPrefix202309\React\Stream\DuplexResourceStream->handleData()                                             
         #21 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(173):                                
         RectorPrefix202309\React\EventLoop\StreamSelectLoop->waitForStreamActivity()                                   
         #22 vendor/rector/rector/src/Console/Command/WorkerCommand.php(73):                                            
         RectorPrefix202309\React\EventLoop\StreamSelectLoop->run()                                                     
         #23 vendor/rector/rector/vendor/symfony/console/Command/Command.php(327):                                      
         Rector\Core\Console\Command\WorkerCommand->execute()                                                           
         #24 vendor/rector/rector/vendor/symfony/console/Application.php(944):                                          
         RectorPrefix202309\Symfony\Component\Console\Command\Command->run()                                            
         #25 vendor/rector/rector/vendor/symfony/console/Application.php(326):                                          
         RectorPrefix202309\Symfony\Component\Console\Application->doRunCommand()                                       
         #26 vendor/rector/rector/src/Console/ConsoleApplication.php(61):                                               
         RectorPrefix202309\Symfony\Component\Console\Application->doRun()                                              
         #27 vendor/rector/rector/vendor/symfony/console/Application.php(212):                                          
         Rector\Core\Console\ConsoleApplication->doRun()                                                                
         #28 vendor/rector/rector/bin/rector.php(132): RectorPrefix202309\Symfony\Component\Console\Application->run()  
         #29 vendor/rector/rector/bin/rector(5): require_once('...')                                                    
         #30 vendor/bin/rector(119): include('...')                                                                     
         #31 {main}". On line: 78 

Hi, could you provode reproducible code and config at https://getrector.com/demo? Thank you.

I can't really provide that for this case, but I can give more context:

I run the rule on a folder with just controllers in it. The very first controller causes this exception:

[ERROR] Could not process "/path/to/Controller1.php" file, due to:
         "System error: "Environment variable not found: "SENTRY_RELEASE"."

every consecutive error after that one is the System error: Warning: Undefined array key 1 I mentioned in the description. If I just remove that first controller. the SENTRY_RELEASE error happens on the 2nd controller instead.

In the running project, the variable SENTRY_RELEASE is set via my .env file that lies in my project root directory. When I pass the variable manually to the command via ❯ SENTRY_RELEASE=some@release vendor/bin/rector process path/to/Controllers/, it then complains about another variable from my .env file. So apparently rector doesn't source the environment files like a normal symfony instance would do.

You need to register SENTRY_RELEASE env to a php file and load in a bootstrap file, eg:

$rectorConfig->bootstrapFiles([
        __DIR__ . '/environments.php',
    ]);

see https://getrector.com/documentation/static-reflection-and-autoload

So I ran composer dump-env prod to generate a php file for this purpose. It resulted in a file like this:

<?php

// This file was generated by running "composer dump-env prod"

return array (
  'APP_ENV' => 'prod',
  'APP_SECRET' => '',
  'SENTRY_RELEASE' => 'some@release',
  ...
);

I then included it via

 $rectorConfig->bootstrapFiles([
        __DIR__ . '/.env.local.php'
    ]);

But still SENTRY_RELEASE can't be found. I also tried to change the env file to look like this:

<?php

const APP_ENV = 'prod';
const APP_SECRET = '';
const SENTRY_RELEASE = 'some@release';
...

Because I wasn't sure that an array is what rector expects. But the const approach didn't work either. Do I need to provide a different format for bootstrapFiles?