| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApiClients\Tools\ResourceGenerator\FileGenerators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\Common\Inflector\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpParser\BuilderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | final class SyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const NAMESPACE = 'Sync'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |     public function generate(): Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |         $classChunks = explode('\\', $this->yaml['class']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |         $className = array_pop($classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         $interfaceName = $className . 'Interface'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 24 | 1 | View Code Duplication |         if (count($classChunks) > 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |             $namespace .= '\\' . implode('\\', $classChunks); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |             $namespace = str_replace('\\\\', '\\', $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |         $baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         $interfaceFQName = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'] . 'Interface'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $factory = new BuilderFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $class = $factory->class($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |             ->extend('Base' . $className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         $class->addStmt($factory->method('refresh') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             ->makePublic() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |             ->setReturnType($className) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |             ->addStmt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |                 new Node\Stmt\Return_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |                     new Node\Expr\MethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |                         new Node\Expr\Variable('this'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |                         'wait', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                             new Node\Expr\MethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                                 $this->callCommandBus(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                                 'then', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                                 [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |                                    $this->createRefreshClosure($className, $interfaceName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         return $factory->namespace($namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             ->addStmt($factory->use(BuildAsyncFromSyncCommand::class)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             ->addStmt($factory->use($baseClass)->as('Base' . $className)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             ->addStmt($factory->use($interfaceFQName)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             ->addStmt($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             ->getNode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     protected function callCommandBus(): Node\Expr\MethodCall | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         return new Node\Expr\MethodCall( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             new Node\Expr\Variable('this'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             'handleCommand', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                 $this->createCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |     protected function createCommand(): Node\Expr\New_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         return new Node\Expr\New_( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             new Node\Name('BuildAsyncFromSyncCommand'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |                 new Node\Expr\ClassConstFetch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |                     new Node\Name('self'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |                     'HYDRATE_CLASS' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |                 new Node\Expr\Variable('this'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 | 1 |  |     protected function createRefreshClosure(string $className, string $interfaceName): Node\Expr\Closure | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 93 | 1 |  |         return new Node\Expr\Closure( | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |             [ | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |                 'params' => [ | 
            
                                                        
            
                                    
            
            
                | 96 | 1 |  |                     new Node\Param( | 
            
                                                        
            
                                    
            
            
                | 97 | 1 |  |                         Inflector::camelize($className), | 
            
                                                        
            
                                    
            
            
                | 98 | 1 |  |                         null, | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |                         $interfaceName | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |                     ) | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |                 ], | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |                 'stmts' => [ | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |                     new Node\Stmt\Return_( | 
            
                                                        
            
                                    
            
            
                | 104 | 1 |  |                         new Node\Expr\MethodCall( | 
            
                                                        
            
                                    
            
            
                | 105 | 1 |  |                             new Node\Expr\Variable( | 
            
                                                        
            
                                    
            
            
                | 106 | 1 |  |                                 Inflector::camelize($className) | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |                             ), | 
            
                                                        
            
                                    
            
            
                | 108 | 1 |  |                             'refresh' | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |                         ) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |                     ) | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |                 ], | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |             ] | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                        
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.