| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApiClients\Tools\ResourceGenerator\FileGenerators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ApiClients\Tools\ResourceTestUtilities\AbstractEmptyResourceTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ApiClients\Tools\ResourceTestUtilities\AbstractResourceTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpParser\BuilderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | abstract class AbstractEmptyExtendingTestGenerator implements FileGeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * InterfaceGenerator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param array $yaml | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 1 |  |     public function __construct(array $yaml) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |         $this->yaml = $yaml; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 30 | 1 | View Code Duplication |     public function getFilename(): string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $className = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |         $className = 'Empty' . $className; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         $namespace = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         if (count($classChunks) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |         return $this->yaml['tests']['path'] . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             DIRECTORY_SEPARATOR . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             static::NAMESPACE . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             DIRECTORY_SEPARATOR . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 '\\', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                 DIRECTORY_SEPARATOR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                 $namespace . '\\' . $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ) . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             'Test.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     public function generate(): Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $emptyClassChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $emptyBaseClass = array_pop($emptyClassChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $emptyClassName = 'Empty' . $emptyBaseClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $emptyNamespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         if (count($emptyClassChunks) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             $emptyNamespace .= '\\' . implode('\\', $emptyClassChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             $emptyNamespace = str_replace('\\\\', '\\', $emptyNamespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $baseClass = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |         $className = $baseClass . 'Test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $namespace = $this->yaml['tests']['namespace'] . '\\' . static::NAMESPACE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         if (count($classChunks) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         $factory = new BuilderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         $class = $factory->class($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             ->makeFinal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             ->extend('AbstractEmptyResourceTest'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $class->addStmt($factory->method('getSyncAsync') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             ->makePublic() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             ->setReturnType('string') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |             ->addStmt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |                 new Node\Stmt\Return_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                     new Node\Scalar\String_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                         static::NAMESPACE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         $class->addStmt($factory->method('getClass') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             ->makePublic() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             ->setReturnType('string') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             ->addStmt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |                 new Node\Stmt\Return_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |                     new Node\Expr\ClassConstFetch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |                         new Node\Name($emptyClassName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |                         'class' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         return $factory->namespace($namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |             ->addStmt($factory->use(AbstractEmptyResourceTest::class)->as('AbstractEmptyResourceTest')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |             ->addStmt($factory->use($emptyNamespace . '\\' . $emptyClassName)->as($emptyClassName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |             ->addStmt($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             ->getNode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.