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 |
||
8 | class ViewMakeCommand extends GeneratorCommand |
||
9 | { |
||
10 | /** |
||
11 | * The console command name. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $name = 'generate:view'; |
||
16 | |||
17 | /** |
||
18 | * The console command description. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $description = 'Create a new view'; |
||
23 | |||
24 | /** |
||
25 | * The type of class being generated. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $type = 'View'; |
||
30 | |||
31 | /** |
||
32 | * Get the stub file for the generator. |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | 1 | protected function getStub() |
|
40 | |||
41 | /** |
||
42 | * Get the default namespace for the class. |
||
43 | * |
||
44 | * @param string $rootNamespace |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | 1 | protected function getDefaultNamespace($rootNamespace) |
|
52 | |||
53 | /** |
||
54 | * Build the class with the given name. |
||
55 | * |
||
56 | * @param string $name |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 1 | protected function buildClass($name) |
|
80 | |||
81 | /** |
||
82 | * Get the destination class path. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | protected function getPath($name) |
|
97 | |||
98 | /** |
||
99 | * view. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | protected function view() |
|
107 | |||
108 | /** |
||
109 | * Get the console command options. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 1 | View Code Duplication | protected function getOptions() |
120 | } |
||
121 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.