| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Common\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Console\Traits\CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Question\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * GenerateMigrationCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class GenerateMigrationCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use CodeGenerate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Configuration of command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->setName('generate:migration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->setDescription('Generate new migration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->setHelp('php partisan generate:migration</info>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Execute method of command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return int|null|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 39 |  | View Code Duplication |     protected function execute(InputInterface $input, OutputInterface $output) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         $output->writeln(['<comment>Welcome to the migration generator</comment>']); | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         $helper   = $this->getHelper('question'); | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         $question = new Question('<info>Please enter the name of the migration: </info>'); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         $question->setValidator(function($answer) { | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |             if (strlen(trim($answer)) === 0) { | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |                 throw new \RunTimeException('The name of the migration should be not empty'); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |             return $answer; | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         }); | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         $migrationName = $helper->ask($input, $output, $question); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         $baseName      = date('YmdHis').'_'.$migrationName.'.php'; | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         $path          = $this->getPath($baseName, MIGRATIONS_PATH); | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         $placeHolders = [ | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             '<class>', | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             '<tableName>', | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $replacements = [ | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             Helper::underscoreToCamelCase($migrationName, true), | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             strtolower($migrationName), | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         $this->generateCode($placeHolders, $replacements, 'MigrationTemplate.tpl', $path); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         $output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', realpath($path))); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         return; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                        
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.