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 |
||
7 | class MakeJsonApiDemoRemove extends Command |
||
8 | { |
||
9 | /** |
||
10 | * The name and signature of the console command. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $signature = 'make:demo-remove |
||
15 | {--test : Add files postfix for test}'; |
||
16 | |||
17 | /** |
||
18 | * The console command description. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $description = 'Remove JsonApi Demo entities'; |
||
23 | |||
24 | protected $testPostfix = '-test'; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $controllers = [ |
||
30 | 'LikesController.stub' => 'LikesController.php', |
||
31 | 'SkillsController.stub' => 'SkillsController.php', |
||
32 | 'TeamsController.stub' => 'TeamsController.php', |
||
33 | 'UsersController.stub' => 'UsersController.php' |
||
34 | ]; |
||
35 | |||
36 | protected $models = [ |
||
37 | 'Like.stub' => 'Like.php', |
||
38 | 'Skill.stub' => 'Skill.php', |
||
39 | 'Team.stub' => 'Team.php' |
||
40 | ]; |
||
41 | |||
42 | protected $migrations = [ |
||
43 | 'create_likes_table.stub' => 'create_likes_table.php', |
||
44 | 'create_membership_table.stub' => 'create_membership_table.php', |
||
45 | 'create_skills_table.stub' => 'create_skills_table.php', |
||
46 | 'create_teams_table.stub' => 'create_teams_table.php', |
||
47 | 'add_foreign_keys_to_likes_table.stub' => 'add_foreign_keys_to_likes_table.php', |
||
48 | 'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php', |
||
49 | 'add_foreign_keys_to_skills_table.stub' => 'add_foreign_keys_to_skills_table.php', |
||
50 | 'add_foreign_keys_to_teams_table.stub' => 'add_foreign_keys_to_teams_table.php' |
||
51 | ]; |
||
52 | |||
53 | protected $seeds = [ |
||
54 | 'TeamsTableSeeder.stub' => 'TeamsTableSeeder.php', |
||
55 | 'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php', |
||
56 | 'JsonApiSeeder.stub' => 'JsonApiSeeder.php' |
||
57 | ]; |
||
58 | |||
59 | protected $jsonapiEntities = [ |
||
60 | 'JsonApi/Likes/Hydrator.stub' => 'JsonApi/Likes/Hydrator.php', |
||
61 | 'JsonApi/Likes/Request.stub' => 'JsonApi/Likes/Request.php', |
||
62 | 'JsonApi/Likes/Schema.stub' => 'JsonApi/Likes/Schema.php', |
||
63 | 'JsonApi/Likes/Search.stub' => 'JsonApi/Likes/Search.php', |
||
64 | 'JsonApi/Likes/Validators.stub' => 'JsonApi/Likes/Validators.php', |
||
65 | |||
66 | 'JsonApi/Skills/Hydrator.stub' => 'JsonApi/Skills/Hydrator.php', |
||
67 | 'JsonApi/Skills/Request.stub' => 'JsonApi/Skills/Request.php', |
||
68 | 'JsonApi/Skills/Schema.stub' => 'JsonApi/Skills/Schema.php', |
||
69 | 'JsonApi/Skills/Search.stub' => 'JsonApi/Skills/Search.php', |
||
70 | 'JsonApi/Skills/Validators.stub' => 'JsonApi/Skills/Validators.php', |
||
71 | |||
72 | 'JsonApi/Teams/Hydrator.stub' => 'JsonApi/Teams/Hydrator.php', |
||
73 | 'JsonApi/Teams/Request.stub' => 'JsonApi/Teams/Request.php', |
||
74 | 'JsonApi/Teams/Schema.stub' => 'JsonApi/Teams/Schema.php', |
||
75 | 'JsonApi/Teams/Search.stub' => 'JsonApi/Teams/Search.php', |
||
76 | 'JsonApi/Teams/Validators.stub' => 'JsonApi/Teams/Validators.php', |
||
77 | |||
78 | 'JsonApi/Users/Hydrator.stub' => 'JsonApi/Users/Hydrator.php', |
||
79 | 'JsonApi/Users/Request.stub' => 'JsonApi/Users/Request.php', |
||
80 | 'JsonApi/Users/Schema.stub' => 'JsonApi/Users/Schema.php', |
||
81 | 'JsonApi/Users/Search.stub' => 'JsonApi/Users/Search.php', |
||
82 | 'JsonApi/Users/Validators.stub' => 'JsonApi/Users/Validators.php', |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Create a new command instance. |
||
87 | * |
||
88 | */ |
||
89 | public function __construct() |
||
93 | |||
94 | /** |
||
95 | * Execute the console command. |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function handle() |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | */ |
||
122 | View Code Duplication | protected function setupTest() |
|
140 | |||
141 | /** |
||
142 | * |
||
143 | */ |
||
144 | View Code Duplication | protected function removeModels() |
|
152 | |||
153 | /** |
||
154 | * |
||
155 | */ |
||
156 | View Code Duplication | protected function removeControllers() |
|
164 | |||
165 | /** |
||
166 | * |
||
167 | */ |
||
168 | protected function removeMigrations() |
||
175 | |||
176 | /** |
||
177 | * |
||
178 | */ |
||
179 | protected function removeSeeds() |
||
187 | |||
188 | /** |
||
189 | * |
||
190 | */ |
||
191 | protected function removeJsonApiEntities() |
||
224 | } |
||
225 |
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.