| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PHPSA\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpParser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPSA\Analyzer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPSA\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPSA\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPSA\ConfigurationLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Base Command providing config loading, memory usage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | abstract class AbstractCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param boolean $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return float | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected function getMemoryUsage($type) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         return round(memory_get_usage($type) / 1024 / 1024, 2); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $configFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $configurationDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function loadConfiguration($configFile, $configurationDirectory) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $loader = new ConfigurationLoader(new FileLocator([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             getcwd(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $configurationDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $loadedConfig = $loader->load($configFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return new Configuration( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $loadedConfig[0], // config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             Analyzer\Factory::getPassesConfigurations(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $loadedConfig[1] // path to config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return PhpParser\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function createParser($application) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $parserStr = $application->configuration->getValue('parser', 'prefer-7'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         switch ($parserStr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             case 'prefer-7': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $languageLevel = ParserFactory::PREFER_PHP7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             case 'prefer-5': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $languageLevel = ParserFactory::PREFER_PHP5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             case 'only-7': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $languageLevel = ParserFactory::ONLY_PHP7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             case 'only-5': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $languageLevel = ParserFactory::ONLY_PHP5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $languageLevel = ParserFactory::PREFER_PHP7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return (new ParserFactory())->create($languageLevel, new \PhpParser\Lexer\Emulative([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'usedAttributes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 'comments', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'startLine', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 'endLine', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 'startTokenPos', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'endTokenPos' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                        
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.