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 |
||
12 | class DateTime extends EditableColumn implements ColumnEditableInterface |
||
13 | { |
||
14 | use DatePicker, DateFormat; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $format = 'Y-m-d H:i:s'; |
||
20 | // protected $format = 'YYYY-MM-DD'; |
||
|
|||
21 | |||
22 | protected $type = 'combodate'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $timezone; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $seconds = false; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $view = 'column.editable.datetime'; |
||
38 | |||
39 | /** |
||
40 | * Text constructor. |
||
41 | * |
||
42 | * @param $name |
||
43 | * @param $label |
||
44 | */ |
||
45 | public function __construct($name, $label = null) |
||
49 | |||
50 | /** |
||
51 | * @return array |
||
52 | */ |
||
53 | public function toArray() |
||
71 | |||
72 | /** |
||
73 | * @param string $date |
||
74 | * |
||
75 | * @return null|string |
||
76 | */ |
||
77 | View Code Duplication | protected function getFormatedDate($date) |
|
89 | |||
90 | /** |
||
91 | * @return $this|NamedFormElement|mixed|null|string |
||
92 | */ |
||
93 | public function getValueFromModel() |
||
100 | |||
101 | /** |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function hasSeconds() |
||
108 | |||
109 | /** |
||
110 | * @param bool $seconds |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setSeconds($seconds) |
||
120 | |||
121 | /** |
||
122 | * @param mixed $value |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | View Code Duplication | public function setModelAttribute($value) |
|
135 | |||
136 | /** |
||
137 | * @param Request $request |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function save(Request $request) |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getPickerFormat() |
||
167 | |||
168 | /** |
||
169 | * @return $this |
||
170 | * |
||
171 | * SMELLS This function does more than it says. |
||
172 | */ |
||
173 | public function setCurrentDate() |
||
179 | } |
||
180 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.