| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace uuf6429\ElderBrother\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use uuf6429\ElderBrother\Change\FileList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @deprecated Use PhpCsFixer class instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class PhpCsFixerOld extends ActionAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var FileList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $binFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $configFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $addAutomatically; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Runs all the provided files through PHP-CS-Fixer, fixing any code style issues. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param FileList    $files            The files to check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string|null $binFile          (Optional, default is from vendor) File path to PHP-CS-Fixer binary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string|null $configFile       (Optional, default is project root) File path to PHP-CS-Fixer config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param bool        $addAutomatically (Optional, default is true) Whether to add modified files to commit or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(FileList $files, $binFile = null, $configFile = null, $addAutomatically = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->files = $files; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->binFile = $binFile ?: $this->getBinFile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->configFile = $configFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->addAutomatically = $addAutomatically; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function getName() | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return 'PHP Code Style Fixer (PhpCsFixerOld)'; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function isSupported() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (!file_exists($this->binFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->logger->warning( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     'PHP-CS-Fixer could not be found in: %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $this->binFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $files = $this->files->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (empty($files)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $progress = $this->createProgressBar($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $progress->start(count($files)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $failed = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $progress->setMessage('Checking <info>' . $file . '</info>...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $process = new Process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     'php -f %s -- fix %s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     escapeshellarg($this->binFile), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     escapeshellarg($file), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     $this->configFile ? ('--config-file=' . escapeshellarg($this->configFile)) : '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $process->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             switch ($process->getExitCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 case 0: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     // file has been changed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     if ($this->addAutomatically) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                         exec('git add ' . escapeshellarg($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 case 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     // file not changed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     // some sort of error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     $failed[$file] = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $progress->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if (count($failed)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $message = 'PhpCsFixerOld failed for the following file(s):'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             foreach ($failed as $file => $result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 $message .= PHP_EOL . '- ' . $file . ':'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 $message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             throw new \RuntimeException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $progress->setMessage('Finished.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $progress->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     protected function getBinFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return PROJECT_ROOT . 'vendor' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             . DIRECTORY_SEPARATOR . 'friendsofphp' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             . DIRECTORY_SEPARATOR . 'php-cs-fixer' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             . DIRECTORY_SEPARATOR . 'php-cs-fixer'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |