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 |
||
20 | class Number implements \Serializable, \JsonSerializable |
||
21 | { |
||
22 | // Regex to find correct numbers |
||
23 | const NUMBER_PATTERN = "#^\d+(\.\d+)?$#"; |
||
24 | |||
25 | // Internal variables |
||
26 | protected $dividend; |
||
27 | |||
28 | protected $divisor; |
||
29 | |||
30 | // --- Object construction ------------ |
||
31 | public function __construct($number = null) |
||
35 | |||
36 | protected function initialize($dividend, $divisor) |
||
42 | |||
43 | // --- Number processor ------------ |
||
44 | |||
45 | protected function retrieve($number) |
||
69 | |||
70 | protected function computeDividend($askedDividend, $askedDivisor, Number $number) |
||
81 | |||
82 | protected function simplify() |
||
89 | |||
90 | // --- Operations ------------ |
||
91 | |||
92 | View Code Duplication | public function add($number) |
|
108 | |||
109 | View Code Duplication | public function sub($number) |
|
125 | |||
126 | View Code Duplication | public function multiply($number) |
|
137 | |||
138 | View Code Duplication | public function divide($number) |
|
149 | |||
150 | // --- Results ------------ |
||
151 | |||
152 | public function getNumber($roundPrecision = 2, $roundMode = PHP_ROUND_HALF_UP) |
||
162 | |||
163 | // --- Accessor ------------ |
||
164 | |||
165 | /** |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getDividend() |
||
172 | |||
173 | /** |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function getDivisor() |
||
180 | |||
181 | // --- Interfaces implementation ------------ |
||
182 | /** |
||
183 | * (PHP 5 >= 5.1.0)<br/> |
||
184 | * String representation of object |
||
185 | * @link http://php.net/manual/en/serializable.serialize.php |
||
186 | * @return string the string representation of the object or null |
||
187 | */ |
||
188 | public function serialize() |
||
192 | |||
193 | /** |
||
194 | * (PHP 5 >= 5.1.0)<br/> |
||
195 | * Constructs the object |
||
196 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
197 | * @param string $serialized <p> |
||
198 | * The string representation of the object. |
||
199 | * </p> |
||
200 | * @return void |
||
201 | */ |
||
202 | public function unserialize($serialized) |
||
206 | |||
207 | /** |
||
208 | * (PHP 5 >= 5.4.0)<br/> |
||
209 | * Specify data which should be serialized to JSON |
||
210 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
211 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
212 | * which is a value of any type other than a resource. |
||
213 | */ |
||
214 | function jsonSerialize() |
||
218 | |||
219 | } |
||
220 |
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.