| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Capsule\Manager as Capsule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Question\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Common\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use App\Console\Traits\CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * GenerateModelCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class GenerateModelCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Configuration of command | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             ->setName('generate:model') | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             ->setDescription('Command for generate model') | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Execute method of command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $output->writeln(['<comment>Welcome to the model generator</comment>']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $helper    = $this->getHelper('question'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $question  = new Question('<info>Please enter table name: </info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $tableName = $helper->ask($input, $output, $question); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $table = Capsule::schema()->getColumnListing($tableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (count($table) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $output->writeln([sprintf('<comment>Not found table %s</comment>', $tableName)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $columns = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         foreach ($table as $columnName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $columnType = Capsule::schema()->getColumnType($tableName, $columnName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $columns[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'name' => $columnName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'type' => $columnType !== 'datetime' ? $columnType : '\Carbon\Carbon', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $modelName = substr($tableName, 0, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $className = Helper::underscoreToCamelCase($modelName, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $baseName  = $className.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $path      = $this->getPath($baseName, MODELS_PATH); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $placeHolders = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             '<class>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             '<tableName>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             '<phpdoc>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             '<fillable>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $replacements = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $className, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             strtolower($tableName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->generatePhpDoc($columns), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->generateFillable($columns), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->generateCode($placeHolders, $replacements, 'ModelTemplate.tpl', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $output->writeln(sprintf('Generated new model class to "<info>%s</info>"', realpath($path))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param array $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |     private function generatePhpDoc($columns) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $phpdoc = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $phpdoc[] = sprintf(" * @property %s\t$%s", $column['type'], $column['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return implode("\n", $phpdoc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param array $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 105 |  | View Code Duplication |     private function generateFillable($columns) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $fillable = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         foreach ($columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $fillable[] = sprintf("        '%s',", $column['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return implode("\n", $fillable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
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.