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 |
||
7 | class Logical |
||
8 | { |
||
9 | /** |
||
10 | * TRUE. |
||
11 | * |
||
12 | * Returns the boolean TRUE. |
||
13 | * |
||
14 | * Excel Function: |
||
15 | * =TRUE() |
||
16 | * |
||
17 | * @category Logical Functions |
||
18 | * |
||
19 | * @return bool True |
||
20 | */ |
||
21 | public static function true() |
||
25 | |||
26 | /** |
||
27 | * FALSE. |
||
28 | * |
||
29 | * Returns the boolean FALSE. |
||
30 | * |
||
31 | * Excel Function: |
||
32 | * =FALSE() |
||
33 | * |
||
34 | * @category Logical Functions |
||
35 | * |
||
36 | * @return bool False |
||
37 | */ |
||
38 | public static function false() |
||
42 | |||
43 | /** |
||
44 | * LOGICAL_AND. |
||
45 | * |
||
46 | * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE. |
||
47 | * |
||
48 | * Excel Function: |
||
49 | * =AND(logical1[,logical2[, ...]]) |
||
50 | * |
||
51 | * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays |
||
52 | * or references that contain logical values. |
||
53 | * |
||
54 | * Boolean arguments are treated as True or False as appropriate |
||
55 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
56 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
57 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
58 | * |
||
59 | * @category Logical Functions |
||
60 | * |
||
61 | * @param mixed ...$args Data values |
||
62 | * |
||
63 | * @return bool|string the logical AND of the arguments |
||
64 | */ |
||
65 | public static function logicalAnd(...$args) |
||
99 | |||
100 | /** |
||
101 | * LOGICAL_OR. |
||
102 | * |
||
103 | * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE. |
||
104 | * |
||
105 | * Excel Function: |
||
106 | * =OR(logical1[,logical2[, ...]]) |
||
107 | * |
||
108 | * The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays |
||
109 | * or references that contain logical values. |
||
110 | * |
||
111 | * Boolean arguments are treated as True or False as appropriate |
||
112 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
113 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
114 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
115 | * |
||
116 | * @category Logical Functions |
||
117 | * |
||
118 | * @param mixed $args Data values |
||
119 | * |
||
120 | * @return bool|string the logical OR of the arguments |
||
121 | */ |
||
122 | public static function logicalOr(...$args) |
||
156 | |||
157 | /** |
||
158 | * NOT. |
||
159 | * |
||
160 | * Returns the boolean inverse of the argument. |
||
161 | * |
||
162 | * Excel Function: |
||
163 | * =NOT(logical) |
||
164 | * |
||
165 | * The argument must evaluate to a logical value such as TRUE or FALSE |
||
166 | * |
||
167 | * Boolean arguments are treated as True or False as appropriate |
||
168 | * Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False |
||
169 | * If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds |
||
170 | * the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value |
||
171 | * |
||
172 | * @category Logical Functions |
||
173 | * |
||
174 | * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE |
||
175 | * |
||
176 | * @return bool|string the boolean inverse of the argument |
||
177 | */ |
||
178 | public static function NOT($logical = false) |
||
194 | |||
195 | /** |
||
196 | * STATEMENT_IF. |
||
197 | * |
||
198 | * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE. |
||
199 | * |
||
200 | * Excel Function: |
||
201 | * =IF(condition[,returnIfTrue[,returnIfFalse]]) |
||
202 | * |
||
203 | * Condition is any value or expression that can be evaluated to TRUE or FALSE. |
||
204 | * For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100, |
||
205 | * the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE. |
||
206 | * This argument can use any comparison calculation operator. |
||
207 | * ReturnIfTrue is the value that is returned if condition evaluates to TRUE. |
||
208 | * For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE, |
||
209 | * then the IF function returns the text "Within budget" |
||
210 | * If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use |
||
211 | * the logical value TRUE for this argument. |
||
212 | * ReturnIfTrue can be another formula. |
||
213 | * ReturnIfFalse is the value that is returned if condition evaluates to FALSE. |
||
214 | * For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE, |
||
215 | * then the IF function returns the text "Over budget". |
||
216 | * If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned. |
||
217 | * If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned. |
||
218 | * ReturnIfFalse can be another formula. |
||
219 | * |
||
220 | * @category Logical Functions |
||
221 | * |
||
222 | * @param mixed $condition Condition to evaluate |
||
223 | * @param mixed $returnIfTrue Value to return when condition is true |
||
224 | * @param mixed $returnIfFalse Optional value to return when condition is false |
||
225 | * |
||
226 | * @return mixed The value of returnIfTrue or returnIfFalse determined by condition |
||
227 | */ |
||
228 | public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false) |
||
236 | |||
237 | /** |
||
238 | * IFERROR. |
||
239 | * |
||
240 | * Excel Function: |
||
241 | * =IFERROR(testValue,errorpart) |
||
242 | * |
||
243 | * @category Logical Functions |
||
244 | * |
||
245 | * @param mixed $testValue Value to check, is also the value returned when no error |
||
246 | * @param mixed $errorpart Value to return when testValue is an error condition |
||
247 | * |
||
248 | * @return mixed The value of errorpart or testValue determined by error condition |
||
249 | */ |
||
250 | public static function IFERROR($testValue = '', $errorpart = '') |
||
257 | } |
||
258 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.