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:
Complex classes like MakeJsonApiDemo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MakeJsonApiDemo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class MakeJsonApiDemo extends Command |
||
11 | { |
||
12 | /** |
||
13 | * The name and signature of the console command. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $signature = 'make:demo |
||
18 | {--force : Overwrite existing files by default} |
||
19 | {--test : Add files postfix for test}'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Create JsonApi Demo entities'; |
||
27 | |||
28 | protected $testPostfix = '-test'; |
||
29 | |||
30 | protected $migrations = [ |
||
31 | 'create_likes_table.stub' => 'create_likes_table.php', |
||
32 | 'create_membership_table.stub' => 'create_membership_table.php', |
||
33 | 'create_skills_table.stub' => 'create_skills_table.php', |
||
34 | 'create_teams_table.stub' => 'create_teams_table.php', |
||
35 | 'add_foreign_keys_to_likes_table.stub' => 'add_foreign_keys_to_likes_table.php', |
||
36 | 'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php', |
||
37 | 'add_foreign_keys_to_skills_table.stub' => 'add_foreign_keys_to_skills_table.php', |
||
38 | 'add_foreign_keys_to_teams_table.stub' => 'add_foreign_keys_to_teams_table.php' |
||
39 | ]; |
||
40 | |||
41 | protected $seeds = [ |
||
42 | 'TeamsTableSeeder.stub' => 'TeamsTableSeeder.php', |
||
43 | 'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php', |
||
44 | 'JsonApiSeeder.stub' => 'JsonApiSeeder.php' |
||
45 | ]; |
||
46 | |||
47 | protected $controllers = [ |
||
48 | 'LikesController.stub' => 'LikesController.php', |
||
49 | 'SkillsController.stub' => 'SkillsController.php', |
||
50 | 'TeamsController.stub' => 'TeamsController.php', |
||
51 | 'UsersController.stub' => 'UsersController.php' |
||
52 | ]; |
||
53 | |||
54 | protected $models = [ |
||
55 | 'Like.stub' => 'Like.php', |
||
56 | 'Skill.stub' => 'Skill.php', |
||
57 | 'Team.stub' => 'Team.php' |
||
58 | ]; |
||
59 | |||
60 | protected $jsonapiEntities = [ |
||
61 | 'JsonApi/Likes/Hydrator.php' => 'JsonApi/Likes/Hydrator.php', |
||
62 | 'JsonApi/Likes/Request.php' => 'JsonApi/Likes/Request.php', |
||
63 | 'JsonApi/Likes/Schema.php' => 'JsonApi/Likes/Schema.php', |
||
64 | 'JsonApi/Likes/Search.php' => 'JsonApi/Likes/Search.php', |
||
65 | 'JsonApi/Likes/Validators.php' => 'JsonApi/Likes/Validators.php', |
||
66 | |||
67 | 'JsonApi/Skills/Hydrator.php' => 'JsonApi/Skills/Hydrator.php', |
||
68 | 'JsonApi/Skills/Request.php' => 'JsonApi/Skills/Request.php', |
||
69 | 'JsonApi/Skills/Schema.php' => 'JsonApi/Skills/Schema.php', |
||
70 | 'JsonApi/Skills/Search.php' => 'JsonApi/Skills/Search.php', |
||
71 | 'JsonApi/Skills/Validators.php' => 'JsonApi/Skills/Validators.php', |
||
72 | |||
73 | 'JsonApi/Teams/Hydrator.php' => 'JsonApi/Teams/Hydrator.php', |
||
74 | 'JsonApi/Teams/Request.php' => 'JsonApi/Teams/Request.php', |
||
75 | 'JsonApi/Teams/Schema.php' => 'JsonApi/Teams/Schema.php', |
||
76 | 'JsonApi/Teams/Search.php' => 'JsonApi/Teams/Search.php', |
||
77 | 'JsonApi/Teams/Validators.php' => 'JsonApi/Teams/Validators.php', |
||
78 | |||
79 | 'JsonApi/Users/Hydrator.php' => 'JsonApi/Users/Hydrator.php', |
||
80 | 'JsonApi/Users/Request.php' => 'JsonApi/Users/Request.php', |
||
81 | 'JsonApi/Users/Schema.php' => 'JsonApi/Users/Schema.php', |
||
82 | 'JsonApi/Users/Search.php' => 'JsonApi/Users/Search.php', |
||
83 | 'JsonApi/Users/Validators.php' => 'JsonApi/Users/Validators.php', |
||
84 | ]; |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Create a new command instance. |
||
89 | * |
||
90 | */ |
||
91 | public function __construct() |
||
95 | |||
96 | /** |
||
97 | * Execute the console command. |
||
98 | */ |
||
99 | public function handle() |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | */ |
||
123 | View Code Duplication | protected function setupTest() |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | */ |
||
145 | public function fire() |
||
151 | |||
152 | /** |
||
153 | * |
||
154 | */ |
||
155 | protected function createDirectories() |
||
170 | |||
171 | /** |
||
172 | * |
||
173 | */ |
||
174 | View Code Duplication | protected function copyJsonApiEntities() |
|
189 | |||
190 | /** |
||
191 | * |
||
192 | */ |
||
193 | View Code Duplication | protected function exportControllers() |
|
208 | |||
209 | /** |
||
210 | * |
||
211 | */ |
||
212 | View Code Duplication | protected function exportModels() |
|
227 | |||
228 | /** |
||
229 | * |
||
230 | */ |
||
231 | protected function exportMigrations() |
||
247 | |||
248 | /** |
||
249 | * |
||
250 | */ |
||
251 | View Code Duplication | protected function exportSeeds() |
|
266 | |||
267 | /** |
||
268 | * @param $src |
||
269 | * @param $dst |
||
270 | */ |
||
271 | protected function recurse_copy($src,$dst) |
||
286 | } |
||
287 |
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.