| 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\Builder\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpParser\Builder\Property; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PhpParser\BuilderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function ApiClients\Tools\ResourceGenerator\exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | final class EmptyBaseClassGenerator implements FileGeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var BuilderFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $docBlock = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $uses = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         EmptyResourceInterface::class => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * InterfaceGenerator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $yaml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |     public function __construct(array $yaml) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         $this->yaml = $yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $this->factory = new BuilderFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |     public function getFilename(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $className = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         $className = 'Empty' . $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $namespace = ''; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 57 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         return $this->yaml['src']['path'] . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             DIRECTORY_SEPARATOR . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                 '\\', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 DIRECTORY_SEPARATOR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |                 $namespace . '\\' . $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ) . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             '.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |     public function generate(): Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $className = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         $namespace = $this->yaml['src']['namespace']; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 80 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         $class = $this->factory->class('Empty' . $className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             ->implement($className . 'Interface') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             ->implement('EmptyResourceInterface') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             ->makeAbstract(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         $stmt = $this->factory->namespace($namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         foreach ($this->yaml['properties'] as $name => $details) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             $stmt = $this->processProperty($class, $stmt, $name, $details); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         ksort($this->uses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |         foreach ($this->uses as $useClass => $bool) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $stmt = $stmt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |                 ->addStmt($this->factory->use($useClass)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         return $stmt->addStmt($class)->getNode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param \PhpParser\Builder\Class_ $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |     protected function processProperty($class, $stmt, $name, $details) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         if (is_string($details)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |             if (exists($details)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |                 $this->uses[$details] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |             $methodName = Inflector::camelize($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             $class->addStmt($this->createMethod($details, $name, $methodName, $details)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             return $stmt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         if (exists($details['type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             $this->uses[$details['type']] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         $methodName = Inflector::camelize($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |         if (isset($details['method'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |             $methodName = $details['method']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         $class->addStmt($this->createMethod($details['type'], $name, $methodName, $details)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |         return $stmt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 134 |  | View Code Duplication |     protected function createProperty(string $type, string $name, $details): Property | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         $property = $this->factory->property($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             ->makeProtected() | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |             ->setDocComment("/**\r\n * @var " . $type . "\r\n */") | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         if (isset($details['default'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |             $property->setDefault($details['default']); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         return $property; | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |     protected function createMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         string $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         string $name, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         string $methodName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $details | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     ): Method { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $stmts = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |             new Node\Stmt\Return_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |                 new Node\Expr\ConstFetch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |                     new Node\Name('null') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |         return $this->factory->method($methodName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |             ->makePublic() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |             ->setReturnType($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |             ->setDocComment('/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |                               * @return ' . $type . ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 1 |  |                               */') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |             ->addStmts($stmts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                        
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.