| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApiClients\Tools\ResourceGenerator\FileGenerators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ApiClients\Foundation\Resource\EmptyResourceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\Common\Inflector\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpParser\BuilderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function ApiClients\Tools\ResourceGenerator\exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | abstract class AbstractEmptyExtendingClassGenerator implements FileGeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * InterfaceGenerator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param array $yaml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |     public function __construct(array $yaml) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |         $this->yaml = $yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |     public function getFilename(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 1 |  |         $className = array_pop($classChunks); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |         $className = 'Empty' . $className; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |         $namespace = ''; | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 37 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |         return $this->yaml['src']['path'] . | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |             DIRECTORY_SEPARATOR . | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             static::NAMESPACE . | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             DIRECTORY_SEPARATOR . | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |             str_replace( | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |                 '\\', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |                 DIRECTORY_SEPARATOR, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |                 $namespace . '\\' . $className | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             ) . | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             '.php' | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     public function generate(): Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $baseClass = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $className = 'Empty' . $baseClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $baseNamespace = $this->yaml['src']['namespace']; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             $baseNamespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             $baseNamespace = str_replace('\\\\', '\\', $baseNamespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 68 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         $factory = new BuilderFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $stmt = $factory->namespace($namespace)-> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             addStmt($factory->use($baseNamespace . '\\' . $className)->as('Base' . $className))-> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             addStmt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                 $factory->class($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |                 ->extend('Base' . $className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $stmt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             ->getNode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                        
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.