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 |
||
18 | class Parameter implements Interfaces\Parameter |
||
19 | { |
||
20 | /** |
||
21 | * The parameter type |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $sType; |
||
26 | |||
27 | /** |
||
28 | * The parameter value |
||
29 | * |
||
30 | * @var mixed |
||
31 | */ |
||
32 | protected $xValue; |
||
33 | |||
34 | /** |
||
35 | * The constructor. |
||
36 | * |
||
37 | * @param string $sType The parameter type |
||
38 | * @param mixed $xValue The parameter value |
||
39 | */ |
||
40 | public function __construct($sType, $xValue) |
||
45 | |||
46 | /** |
||
47 | * Get the parameter type |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getType() |
||
55 | |||
56 | /** |
||
57 | * Get the parameter value |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function getValue() |
||
65 | |||
66 | /** |
||
67 | * Set the parameter value |
||
68 | * |
||
69 | * @param mixed $xValue The parameter value |
||
70 | * |
||
71 | * @return void |
||
72 | */ |
||
73 | public function setValue($xValue) |
||
77 | |||
78 | /** |
||
79 | * Create a Parameter instance using the given value |
||
80 | * |
||
81 | * @param mixed $xValue The parameter value |
||
82 | * |
||
83 | * @return Parameter |
||
84 | */ |
||
85 | public static function make($xValue) |
||
108 | |||
109 | /** |
||
110 | * Generate the javascript code. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getScript() |
||
159 | |||
160 | /** |
||
161 | * Magic function to generate the jQuery call. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function __toString() |
||
169 | |||
170 | /** |
||
171 | * Generate the jQuery call, when converting the response into json. |
||
172 | * |
||
173 | * This is a method of the JsonSerializable interface. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function jsonSerialize() |
||
181 | } |
||
182 |
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.