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 Time implements \JsonSerializable |
||
9 | { |
||
10 | /** |
||
11 | * The hours part of the time. |
||
12 | * |
||
13 | * @var integer |
||
14 | */ |
||
15 | protected $hours; |
||
16 | |||
17 | /** |
||
18 | * The minutes part of the time. |
||
19 | * |
||
20 | * @var integer |
||
21 | */ |
||
22 | protected $minutes = 0; |
||
23 | |||
24 | /** |
||
25 | * The seconds part of the time. |
||
26 | * |
||
27 | * @var integer |
||
28 | */ |
||
29 | protected $seconds = 0; |
||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | * |
||
34 | * @param integer $hours The hours. |
||
35 | * @param integer $minutes The minutes. |
||
36 | * @param integer $seconds The seconds. |
||
37 | */ |
||
38 | public function __construct($hours, $minutes = 0, $seconds = 0) |
||
44 | |||
45 | /** |
||
46 | * Checks if this time is before or equal to an other time. |
||
47 | * |
||
48 | * @param Time $other The time to compare it against. |
||
49 | * @return boolean |
||
50 | */ |
||
51 | public function isBeforeOrEqual(Time $other) |
||
55 | |||
56 | /** |
||
57 | * Checks if this time is after or equal to an other time. |
||
58 | * |
||
59 | * @param Time $other The time to compare it against. |
||
60 | * @return boolean |
||
61 | */ |
||
62 | public function isAfterOrEqual(Time $other) |
||
66 | |||
67 | /** |
||
68 | * Check if this time is equal to another time. |
||
69 | * |
||
70 | * @param Time $other The time to compare it against. |
||
71 | * @return boolean |
||
72 | */ |
||
73 | public function isEqual(Time $other) |
||
77 | |||
78 | /** |
||
79 | * Get the integer representation of the time. |
||
80 | * |
||
81 | * @return integer |
||
82 | */ |
||
83 | public function toInteger() |
||
87 | |||
88 | /** |
||
89 | * Set the hours. |
||
90 | * |
||
91 | * @param integer $hours The hours. |
||
92 | * @return Time |
||
93 | */ |
||
94 | View Code Duplication | public function setHours($hours) |
|
104 | |||
105 | /** |
||
106 | * Get the hours. |
||
107 | * |
||
108 | * @return integer |
||
109 | */ |
||
110 | public function getHours() |
||
114 | |||
115 | /** |
||
116 | * Set the minutes. |
||
117 | * |
||
118 | * @param integer $minutes The minutes |
||
119 | * @return Time |
||
120 | */ |
||
121 | View Code Duplication | public function setMinutes($minutes) |
|
131 | |||
132 | /** |
||
133 | * Get the minutes. |
||
134 | * |
||
135 | * @return integer |
||
136 | */ |
||
137 | public function getMinutes() |
||
141 | |||
142 | /** |
||
143 | * Set the seconds. |
||
144 | * |
||
145 | * @param integer $seconds The seconds. |
||
146 | * @return Time |
||
147 | */ |
||
148 | View Code Duplication | public function setSeconds($seconds) |
|
158 | |||
159 | /** |
||
160 | * Get the seconds. |
||
161 | * |
||
162 | * @return integer |
||
163 | */ |
||
164 | public function getSeconds() |
||
168 | |||
169 | /** |
||
170 | * Check if the time elements are valid. |
||
171 | * |
||
172 | * @param integer $hours The hours. |
||
173 | * @param integer $minutes The minutes. |
||
174 | * @param integer $seconds The seconds. |
||
175 | * @return boolean |
||
176 | * @throws \InvalidArgumentException If the elements are not valid. |
||
177 | */ |
||
178 | private function timeElementsAreValid($hours, $minutes, $seconds) |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function jsonSerialize() |
||
206 | |||
207 | /** |
||
208 | * Returns a string representation of the time. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function __toString() |
||
216 | } |
||
217 |
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.