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 |
||
9 | class MakeJsonApiDemo extends Command |
||
10 | { |
||
11 | /** |
||
12 | * The name and signature of the console command. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $signature = 'make:demo |
||
17 | {--force : Overwrite existing files by default}'; |
||
18 | |||
19 | /** |
||
20 | * The console command description. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description = 'Create JsonApi Demo entities'; |
||
25 | |||
26 | protected $migrations = [ |
||
27 | 'create_likes_table.stub' => 'create_likes_table.php', |
||
28 | 'create_membership_table.stub' => 'create_membership_table.php', |
||
29 | 'create_skills_table.stub' => 'create_skills_table.php', |
||
30 | 'create_teams_table.stub' => 'create_teams_table.php', |
||
31 | 'add_foreign_keys_to_likes_table.stub' => 'add_foreign_keys_to_likes_table.php', |
||
32 | 'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php', |
||
33 | 'add_foreign_keys_to_skills_table.stub' => 'add_foreign_keys_to_skills_table.php', |
||
34 | 'add_foreign_keys_to_teams_table.stub' => 'add_foreign_keys_to_teams_table.php' |
||
35 | ]; |
||
36 | |||
37 | protected $seeds = [ |
||
38 | 'TeamsTableSeeder.stub' => 'TeamsTableSeeder.php', |
||
39 | 'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php', |
||
40 | 'JsonApiSeeder.stub' => 'JsonApiSeeder.php' |
||
41 | ]; |
||
42 | |||
43 | protected $controllers = [ |
||
44 | 'LikesController.stub' => 'LikesController.php', |
||
45 | 'SkillsController.stub' => 'SkillsController.php', |
||
46 | 'TeamsController.stub' => 'TeamsController.php', |
||
47 | 'UsersController.stub' => 'UsersController.php' |
||
48 | ]; |
||
49 | |||
50 | protected $models = [ |
||
51 | 'Like.stub' => 'Like.php', |
||
52 | 'Skill.stub' => 'Skill.php', |
||
53 | 'Team.stub' => 'Team.php' |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Create a new command instance. |
||
58 | * |
||
59 | */ |
||
60 | public function __construct() |
||
64 | |||
65 | /** |
||
66 | * Execute the console command. |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function handle() |
||
83 | |||
84 | /** |
||
85 | * |
||
86 | */ |
||
87 | public function fire() |
||
91 | |||
92 | /** |
||
93 | * |
||
94 | */ |
||
95 | protected function copyJsonApiEntities() |
||
99 | |||
100 | /** |
||
101 | * |
||
102 | */ |
||
103 | View Code Duplication | protected function exportControllers() |
|
118 | |||
119 | /** |
||
120 | * |
||
121 | */ |
||
122 | View Code Duplication | protected function exportModels() |
|
137 | |||
138 | /** |
||
139 | * |
||
140 | */ |
||
141 | protected function exportMigrations() |
||
157 | |||
158 | /** |
||
159 | * |
||
160 | */ |
||
161 | View Code Duplication | protected function exportSeeds() |
|
176 | |||
177 | /** |
||
178 | * @param $src |
||
179 | * @param $dst |
||
180 | */ |
||
181 | protected function recurse_copy($src,$dst) |
||
196 | } |
||
197 |
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.