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 |
||
7 | class Dimensions |
||
8 | { |
||
9 | /** |
||
10 | * @param int $maxwidth |
||
11 | * @param int $maxheight |
||
12 | * @return \stdClass |
||
13 | */ |
||
14 | public function resizeToWithin($width, $height, $maxwidth, $maxheight) |
||
33 | |||
34 | /** |
||
35 | * @param int $width |
||
36 | * @param int $height |
||
37 | * @param int|null $newwidth |
||
38 | * @param int|null $newheight |
||
39 | * @return \stdClass|null |
||
40 | */ |
||
41 | public function resizeTo($width, $height, $newwidth, $newheight) |
||
79 | |||
80 | /** |
||
81 | * @param int $width |
||
82 | * @param int $height |
||
83 | * @param int $newwidth |
||
84 | * @return \stdClass|null |
||
85 | */ |
||
86 | View Code Duplication | private function resizeToWidth($width, $height, $newwidth) |
|
101 | |||
102 | /** |
||
103 | * @param int $width |
||
104 | * @param int $height |
||
105 | * @param int $newheight |
||
106 | * @return \stdClass |
||
107 | */ |
||
108 | View Code Duplication | private function resizeToHeight($width, $height, $newheight) |
|
123 | } |
||
124 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: