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:
Complex classes like Logical often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Logical, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Logical |
||
28 | { |
||
29 | /** |
||
30 | * TRUE |
||
31 | * |
||
32 | * Returns the boolean TRUE. |
||
33 | * |
||
34 | * Excel Function: |
||
35 | * =TRUE() |
||
36 | * |
||
37 | * @category Logical Functions |
||
38 | * @return bool True |
||
39 | */ |
||
40 | public static function true() |
||
44 | |||
45 | /** |
||
46 | * FALSE |
||
47 | * |
||
48 | * Returns the boolean FALSE. |
||
49 | * |
||
50 | * Excel Function: |
||
51 | * =FALSE() |
||
52 | * |
||
53 | * @category Logical Functions |
||
54 | * @return bool False |
||
55 | */ |
||
56 | public static function false() |
||
60 | |||
61 | 1 | /** |
|
62 | * LOGICAL_AND |
||
63 | * |
||
64 | * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE. |
||
65 | * |
||
66 | * Excel Function: |
||
67 | * =AND(logical1[,logical2[, ...]]) |
||
68 | * |
||
69 | * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays |
||
70 | * or references that contain logical values. |
||
71 | * |
||
72 | * Boolean arguments are treated as True or False as appropriate |
||
73 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
74 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
75 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
76 | * |
||
77 | * @category Logical Functions |
||
78 | * @param mixed $arg,... Data values |
||
|
|||
79 | * @return bool The logical AND of the arguments. |
||
80 | */ |
||
81 | public static function logicalAnd() |
||
115 | 1 | ||
116 | /** |
||
117 | 18 | * LOGICAL_OR |
|
118 | * |
||
119 | * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE. |
||
120 | * |
||
121 | * Excel Function: |
||
122 | * =OR(logical1[,logical2[, ...]]) |
||
123 | * |
||
124 | * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays |
||
125 | * or references that contain logical values. |
||
126 | * |
||
127 | * Boolean arguments are treated as True or False as appropriate |
||
128 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
129 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
130 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
131 | * |
||
132 | * @category Logical Functions |
||
133 | * @param mixed $arg,... Data values |
||
134 | * @return bool The logical OR of the arguments. |
||
135 | */ |
||
136 | public static function logicalOr() |
||
170 | 19 | ||
171 | 1 | /** |
|
172 | * NOT |
||
173 | 18 | * |
|
174 | * Returns the boolean inverse of the argument. |
||
175 | * |
||
176 | * Excel Function: |
||
177 | * =NOT(logical) |
||
178 | * |
||
179 | * The argument must evaluate to a logical value such as TRUE or FALSE |
||
180 | * |
||
181 | * Boolean arguments are treated as True or False as appropriate |
||
182 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
183 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
184 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
185 | * |
||
186 | * @category Logical Functions |
||
187 | * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE |
||
188 | * @return bool The boolean inverse of the argument. |
||
189 | */ |
||
190 | public static function NOT($logical = false) |
||
206 | |||
207 | 8 | /** |
|
208 | * STATEMENT_IF |
||
209 | * |
||
210 | * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE. |
||
211 | 10 | * |
|
212 | * Excel Function: |
||
213 | * =IF(condition[,returnIfTrue[,returnIfFalse]]) |
||
214 | * |
||
215 | * Condition is any value or expression that can be evaluated to TRUE or FALSE. |
||
216 | * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100, |
||
217 | * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE. |
||
218 | * This argument can use any comparison calculation operator. |
||
219 | * ReturnIfTrue is the value that is returned if condition evaluates to TRUE. |
||
220 | * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE, |
||
221 | * then the IF function returns the text "Within budget" |
||
222 | * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use |
||
223 | * the logical value TRUE for this argument. |
||
224 | * ReturnIfTrue can be another formula. |
||
225 | * ReturnIfFalse is the value that is returned if condition evaluates to FALSE. |
||
226 | * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE, |
||
227 | * then the IF function returns the text "Over budget". |
||
228 | * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned. |
||
229 | * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned. |
||
230 | * ReturnIfFalse can be another formula. |
||
231 | * |
||
232 | * @category Logical Functions |
||
233 | * @param mixed $condition Condition to evaluate |
||
234 | * @param mixed $returnIfTrue Value to return when condition is true |
||
235 | * @param mixed $returnIfFalse Optional value to return when condition is false |
||
236 | * @return mixed The value of returnIfTrue or returnIfFalse determined by condition |
||
237 | */ |
||
238 | public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false) |
||
246 | 15 | ||
247 | /** |
||
248 | 15 | * IFERROR |
|
249 | 15 | * |
|
250 | 15 | * Excel Function: |
|
251 | * =IFERROR(testValue,errorpart) |
||
252 | 15 | * |
|
253 | * @category Logical Functions |
||
254 | * @param mixed $testValue Value to check, is also the value returned when no error |
||
255 | * @param mixed $errorpart Value to return when testValue is an error condition |
||
256 | * @return mixed The value of errorpart or testValue determined by error condition |
||
257 | */ |
||
258 | public static function IFERROR($testValue = '', $errorpart = '') |
||
265 | } |
||
266 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.