| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the CS library. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) 2015-present LIN3S <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace LIN3S\CS\Checker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Jon Torrado <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class PhpCsFixer extends Checker | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public static function check(array $files = [], array $parameters = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         foreach ($files as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             if (false === self::exist($file, $parameters['phpcsfixer_path'], 'php')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             // Exec PHP function is used because php-cs-fixer uses Symfony Process component inside | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             // ProcessBuilder fails when is launched from another ProcessBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             $commandLine = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 'php', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |                 'vendor/friendsofphp/php-cs-fixer/php-cs-fixer', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                 'fix', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 $file, | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 '--config=.php_cs', | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             exec(implode(' ', $commandLine)); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public static function files(array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self::phpCsConfigFile($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         self::phpspecCsConfigFile($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private static function phpCsConfigFile(array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self::configFile('.php_cs', $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private static function phpspecCsConfigFile(array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         self::configFile('.phpspec_cs', $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private static function configFile($fileName, array $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $file = file_get_contents(__DIR__ . '/../' . $fileName . '.dist'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $file = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             '$$CHANGE-FOR-YOUR-AWESOME-NAME CHANGE-FOR-TYPE$$', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $parameters['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $file = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             '$$CHANGE-FOR-YEAR$$', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $parameters['year'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             file_put_contents(self::location($parameters) . '/' . $fileName, $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private static function location($parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $parameters['root_directory'] . '/' . $parameters['phpcsfixer_file_location']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |