Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class MigrateLaravelCommand extends Command |
||
13 | { |
||
14 | protected $listFileMigrations = [ |
||
15 | 'country' => '_create_table_country', |
||
16 | 'regions' => '_create_table_regions', |
||
17 | 'cities' => '_create_table_cities' |
||
18 | ]; |
||
19 | |||
20 | protected $listFilesSeeder = [ |
||
21 | 'CountryTableSeeder' => 'CountryTableSeeder.php', |
||
22 | 'RegionsTableSeeder' => 'RegionsTableSeeder.php', |
||
23 | 'CitiesTableSeeder' => 'CitiesTableSeeder.php' |
||
24 | ]; |
||
25 | |||
26 | protected $formatterStyle; |
||
27 | protected $progressBar; |
||
28 | |||
29 | 2 | public function __construct(OutputFormatterStyle $formatterStyle, ProgressBar $bar) |
|
30 | { |
||
31 | 2 | parent::__construct(); |
|
32 | 2 | $this->formatterStyle = $formatterStyle; |
|
33 | 2 | $this->progressBar = $bar; |
|
34 | 2 | $this->progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%'); |
|
35 | 2 | } |
|
36 | |||
37 | 2 | protected function configure() : void |
|
38 | { |
||
39 | 2 | $this->setName('migrate:laravel')->setHelp('to migrate files to Laravel'); |
|
40 | 2 | } |
|
41 | |||
42 | protected function execute(InputInterface $input, OutputInterface $output) |
||
43 | { |
||
44 | $output->getFormatter()->setStyle('fire', $this->formatterStyle); |
||
45 | |||
46 | $output->writeln([ |
||
47 | '<fire>There is a migration in the project Laravel</fire>', |
||
48 | '' |
||
49 | ]); |
||
50 | |||
51 | $this->progressBar->start(); |
||
52 | |||
53 | $this->moveMigrate( $this->progressBar ); |
||
54 | $this->moveSeeders( $this->progressBar ); |
||
55 | $this->moveConfig( $this->progressBar ); |
||
56 | |||
57 | $this->progressBar->finish(); |
||
58 | $output->writeln(['']); |
||
59 | $output->writeln(['<info>All successfully copied!</info>']); |
||
60 | } |
||
61 | |||
62 | View Code Duplication | protected function moveMigrate(ProgressBar $progress) |
|
76 | |||
77 | View Code Duplication | protected function moveSeeders(ProgressBar $progress) |
|
91 | |||
92 | protected function moveConfig(ProgressBar $progress) : void |
||
101 | |||
102 | /** |
||
103 | * Just create directory |
||
104 | * @param $dir |
||
105 | */ |
||
106 | protected function createDir(string $dir) : void |
||
113 | |||
114 | /** |
||
115 | * Data from stubs file |
||
116 | * @param $nameFile |
||
117 | * @return bool|string |
||
118 | */ |
||
119 | protected function getContent(string $nameFile) : string |
||
123 | |||
124 | /** |
||
125 | * Get date normalize mow |
||
126 | * @return mixed |
||
127 | */ |
||
128 | protected function getDateNormalize() : string |
||
136 | } |
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.