| 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 |  |  | use LIN3S\CS\Error\Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class EsLint implements Checker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use FileFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use ToolAvailability; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public static function check(array $files = [], array $parameters = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         self::isAvailable('eslint'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         self::file($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $excludes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if (true === array_key_exists('eslint_exclude', $parameters)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             foreach ($parameters['eslint_exclude'] as $key => $exclude) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 $excludes[$key] = $parameters['eslint_path'] . '/' . $exclude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $errors = []; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 40 |  | View Code Duplication |         foreach ($files as $file) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             if (false === self::exist($file, $parameters['eslint_path'], 'js') || in_array($file, $excludes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $process = new Process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 sprintf('eslint %s -c %s/.eslint.yml', $file, self::location($parameters)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $parameters['root_directory'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $process->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             if (!$process->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $errors[] = new Error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     sprintf('<error>%s</error>', trim($process->getErrorOutput())), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     sprintf('<error>%s</error>', trim($process->getOutput())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 62 |  | View Code Duplication |     public static function file($parameters) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $yaml = array_replace_recursive( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             Yaml::parse(file_get_contents(__DIR__ . '/../.eslint.yml.dist')), $parameters['eslint_rules'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $location = self::location($parameters) . '/.eslint.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $fileSystem = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $fileSystem->remove($location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $fileSystem->touch($location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             file_put_contents($location, Yaml::dump($yaml)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     private static function location($parameters) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         return $parameters['root_directory'] . '/' . $parameters['eslint_file_location']; | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.