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 |
||
13 | class MakeJsonApiDemo extends Command |
||
14 | { |
||
15 | /** |
||
16 | * The name and signature of the console command. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $signature = 'make:demo |
||
21 | {--force : Overwrite existing files by default} |
||
22 | {--fake : Make fake directories and files for test}'; |
||
23 | |||
24 | /** |
||
25 | * The console command description. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $description = 'Create JsonApi Demo entities'; |
||
30 | |||
31 | protected $migrations = [ |
||
32 | 'create_likes_table.stub' => 'create_likes_table.php', |
||
33 | 'create_membership_table.stub' => 'create_membership_table.php', |
||
34 | 'create_skills_table.stub' => 'create_skills_table.php', |
||
35 | 'create_teams_table.stub' => 'create_teams_table.php', |
||
36 | 'add_foreign_keys_to_likes_table.stub' => 'add_foreign_keys_to_likes_table.php', |
||
37 | 'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php', |
||
38 | 'add_foreign_keys_to_skills_table.stub' => 'add_foreign_keys_to_skills_table.php', |
||
39 | 'add_foreign_keys_to_teams_table.stub' => 'add_foreign_keys_to_teams_table.php' |
||
40 | ]; |
||
41 | |||
42 | protected $seeds = [ |
||
43 | 'TeamsTableSeeder.stub' => 'TeamsTableSeeder.php', |
||
44 | 'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php', |
||
45 | 'JsonApiSeeder.stub' => 'JsonApiSeeder.php' |
||
46 | ]; |
||
47 | |||
48 | protected $controllers = [ |
||
49 | 'LikesController.stub' => 'LikesController.php', |
||
50 | 'SkillsController.stub' => 'SkillsController.php', |
||
51 | 'TeamsController.stub' => 'TeamsController.php', |
||
52 | 'UsersController.stub' => 'UsersController.php' |
||
53 | ]; |
||
54 | |||
55 | protected $models = [ |
||
56 | 'Like.stub' => 'Like.php', |
||
57 | 'Skill.stub' => 'Skill.php', |
||
58 | 'Team.stub' => 'Team.php' |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $stubs = []; |
||
65 | |||
66 | /** |
||
67 | * Create a new command instance. |
||
68 | * |
||
69 | */ |
||
70 | public function __construct() |
||
74 | |||
75 | /** |
||
76 | * Execute the console command. |
||
77 | */ |
||
78 | public function handle() |
||
98 | |||
99 | /** |
||
100 | * use fake file system for tests |
||
101 | */ |
||
102 | public function setupFake() |
||
114 | |||
115 | /** |
||
116 | * |
||
117 | */ |
||
118 | public function fire() |
||
122 | |||
123 | /** |
||
124 | * |
||
125 | */ |
||
126 | protected function copyJsonApiEntities() |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | */ |
||
137 | View Code Duplication | protected function exportControllers() |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | */ |
||
163 | View Code Duplication | protected function exportModels() |
|
185 | |||
186 | /** |
||
187 | * |
||
188 | */ |
||
189 | protected function exportMigrations() |
||
212 | |||
213 | /** |
||
214 | * |
||
215 | */ |
||
216 | View Code Duplication | protected function exportSeeds() |
|
238 | |||
239 | /** |
||
240 | * @param $src |
||
241 | * @param $dst |
||
242 | */ |
||
243 | protected function recurse_copy($src,$dst) |
||
258 | } |
||
259 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.