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 |
||
4 | trait MinMaxTrait |
||
5 | { |
||
6 | /** |
||
7 | * @Exclude |
||
8 | * @var int|float|\DateTime|\DateInterval Specifies the lower bounds for numeric values (the value must be greater |
||
9 | * than or equal to this value) |
||
10 | */ |
||
11 | private $minInclusive = null; |
||
12 | /** |
||
13 | * @Exclude |
||
14 | * @var int|float|\DateTime|\DateInterval Specifies the upper bounds for numeric values (the value must be less |
||
15 | * than or equal to this value) |
||
16 | */ |
||
17 | private $maxInclusive = null; |
||
18 | |||
19 | /** |
||
20 | * @param int|float|\DateTime|\DateInterval $v Specifies the upper bounds for numeric values (the value must be |
||
21 | * less than this value) |
||
22 | */ |
||
23 | View Code Duplication | public function setMaxExclusive($v) |
|
24 | { |
||
25 | if (is_int($v)) { |
||
26 | $this->maxInclusive = $v - 1; |
||
27 | } else { |
||
28 | $this->minInclusive = $v - 0.000001; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param int|float|\DateTime|\DateInterval $v Specifies the upper bounds for numeric values |
||
34 | * (the value must be less than or equal to this value) |
||
35 | */ |
||
36 | public function setMaxInclusive($v) |
||
40 | |||
41 | /** |
||
42 | * @param int|float|\DateTime|\DateInterval $v Specifies the lower bounds for numeric values |
||
43 | * (the value must be greater than this value) |
||
44 | */ |
||
45 | View Code Duplication | public function setMinExclusive($v) |
|
53 | |||
54 | /** |
||
55 | * @param int|float|\DateTime|\DateInterval $v Specifies the lower bounds for numeric values |
||
56 | * (the value must be greater than or equal to this value) |
||
57 | */ |
||
58 | public function setMinInclusive($v) |
||
62 | |||
63 | public function checkMinMax($v) |
||
72 | |||
73 | private function checkMin($v) |
||
79 | |||
80 | private function checkMax($v) |
||
86 | } |
||
87 |