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 |
||
10 | class Comparator |
||
11 | { |
||
12 | /** |
||
13 | * The version is equal to another. |
||
14 | */ |
||
15 | const EQUAL_TO = 0; |
||
16 | |||
17 | /** |
||
18 | * The version is greater than another. |
||
19 | */ |
||
20 | const GREATER_THAN = 1; |
||
21 | |||
22 | /** |
||
23 | * The version is less than another. |
||
24 | */ |
||
25 | const LESS_THAN = -1; |
||
26 | |||
27 | /** |
||
28 | * Compares one version with another. |
||
29 | * |
||
30 | * @param Version $left The left version to compare. |
||
31 | * @param Version $right The right version to compare. |
||
32 | * |
||
33 | * @return integer Returns Comparator::EQUAL_TO if the two versions are |
||
34 | * equal. If the left version is less than the right |
||
35 | * version, Comparator::LESS_THAN is returned. If the left |
||
36 | * version is greater than the right version, |
||
37 | * Comparator::GREATER_THAN is returned. |
||
38 | */ |
||
39 | public static function compareTo(Version $left, Version $right) |
||
63 | |||
64 | /** |
||
65 | * Checks if the left version is equal to the right. |
||
66 | * |
||
67 | * @param Version $left The left version to compare. |
||
68 | * @param Version $right The right version to compare. |
||
69 | * |
||
70 | * @return boolean TRUE if the left version is equal to the right, FALSE |
||
71 | * if not. |
||
72 | */ |
||
73 | public static function isEqualTo(Version $left, Version $right) |
||
77 | |||
78 | /** |
||
79 | * Checks if the left version is greater than the right. |
||
80 | * |
||
81 | * @param Version $left The left version to compare. |
||
82 | * @param Version $right The right version to compare. |
||
83 | * |
||
84 | * @return boolean TRUE if the left version is greater than the right, |
||
85 | * FALSE if not. |
||
86 | */ |
||
87 | public static function isGreaterThan(Version $left, Version $right) |
||
91 | |||
92 | /** |
||
93 | * Checks if the left version is less than the right. |
||
94 | * |
||
95 | * @param Version $left The left version to compare. |
||
96 | * @param Version $right The right version to compare. |
||
97 | * |
||
98 | * @return boolean TRUE if the left version is less than the right, |
||
99 | * FALSE if not. |
||
100 | */ |
||
101 | public static function isLessThan(Version $left, Version $right) |
||
105 | |||
106 | /** |
||
107 | * Compares the identifier components of the left and right versions. |
||
108 | * |
||
109 | * @param array $left The left identifiers. |
||
110 | * @param array $right The right identifiers. |
||
111 | * |
||
112 | * @return integer Returns Comparator::EQUAL_TO if the two identifiers are |
||
113 | * equal. If the left identifiers is less than the right |
||
114 | * identifiers, Comparator::LESS_THAN is returned. If the |
||
115 | * left identifiers is greater than the right identifiers, |
||
116 | * Comparator::GREATER_THAN is returned. |
||
117 | */ |
||
118 | public static function compareIdentifiers(array $left, array $right) |
||
174 | } |
||
175 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.