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 |
||
14 | class DatePickerField extends Field |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | const TYPE = 'date-picker'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $date_format = 'Y-m-d'; |
||
25 | |||
26 | /** |
||
27 | * @var null|string |
||
28 | */ |
||
29 | protected $date_min = null; |
||
30 | |||
31 | /** |
||
32 | * @var null|string |
||
33 | */ |
||
34 | protected $date_max = null; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function getViewName() |
||
43 | |||
44 | /** |
||
45 | * @param string $format |
||
46 | * @return self |
||
47 | */ |
||
48 | public function withDateFormat($format) |
||
54 | |||
55 | /** |
||
56 | * @param string|Carbon $date |
||
57 | * @return self |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | View Code Duplication | public function withMinDate($date) |
|
77 | |||
78 | /** |
||
79 | * @param string|Carbon $date |
||
80 | * @return self |
||
81 | * @throws InvalidArgumentException |
||
82 | */ |
||
83 | View Code Duplication | public function withMaxDate($date) |
|
100 | |||
101 | /** |
||
102 | * @param void |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getViewVariables() |
||
116 | |||
117 | /** |
||
118 | * @param Validator $validator |
||
119 | * @return Validator |
||
120 | */ |
||
121 | public function beforeValidation(Validator $validator) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function getCss() |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function getScripts() |
||
171 | |||
172 | /** |
||
173 | * Convert a PHP date format to a JS date format. |
||
174 | * |
||
175 | * @param string $format |
||
176 | * @return string |
||
177 | */ |
||
178 | public static function dateFormatJs($format) |
||
200 | } |
||
201 |
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.