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 Functions 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 Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class Functions |
||
40 | { |
||
41 | /** constants */ |
||
42 | const COMPATIBILITY_EXCEL = 'Excel'; |
||
43 | const COMPATIBILITY_GNUMERIC = 'Gnumeric'; |
||
44 | const COMPATIBILITY_OPENOFFICE = 'OpenOfficeCalc'; |
||
45 | |||
46 | const RETURNDATE_PHP_NUMERIC = 'P'; |
||
47 | const RETURNDATE_PHP_OBJECT = 'O'; |
||
48 | const RETURNDATE_EXCEL = 'E'; |
||
49 | |||
50 | /** |
||
51 | * Compatibility mode to use for error checking and responses |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected static $compatibilityMode = self::COMPATIBILITY_EXCEL; |
||
56 | |||
57 | /** |
||
58 | * Data Type to use when returning date values |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected static $returnDateType = self::RETURNDATE_EXCEL; |
||
63 | |||
64 | /** |
||
65 | * List of error codes |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected static $errorCodes = [ |
||
70 | 'null' => '#NULL!', |
||
71 | 'divisionbyzero' => '#DIV/0!', |
||
72 | 'value' => '#VALUE!', |
||
73 | 'reference' => '#REF!', |
||
74 | 'name' => '#NAME?', |
||
75 | 'num' => '#NUM!', |
||
76 | 'na' => '#N/A', |
||
77 | 'gettingdata' => '#GETTING_DATA', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Set the Compatibility Mode |
||
82 | * |
||
83 | * @category Function Configuration |
||
84 | * @param string $compatibilityMode Compatibility Mode |
||
85 | * Permitted values are: |
||
86 | * Functions::COMPATIBILITY_EXCEL 'Excel' |
||
87 | * Functions::COMPATIBILITY_GNUMERIC 'Gnumeric' |
||
88 | * Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc' |
||
89 | * @return bool (Success or Failure) |
||
90 | */ |
||
91 | public static function setCompatibilityMode($compatibilityMode) |
||
104 | 2974 | ||
105 | 2974 | /** |
|
106 | * Return the current Compatibility Mode |
||
107 | * |
||
108 | * @category Function Configuration |
||
109 | * @return string Compatibility Mode |
||
110 | * Possible Return values are: |
||
111 | * Functions::COMPATIBILITY_EXCEL 'Excel' |
||
112 | * Functions::COMPATIBILITY_GNUMERIC 'Gnumeric' |
||
113 | * Functions::COMPATIBILITY_OPENOFFICE 'OpenOfficeCalc' |
||
114 | */ |
||
115 | public static function getCompatibilityMode() |
||
119 | |||
120 | /** |
||
121 | * Set the Return Date Format used by functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object) |
||
122 | 441 | * |
|
123 | * @category Function Configuration |
||
124 | 441 | * @param string $returnDateType Return Date Format |
|
125 | * Permitted values are: |
||
126 | * Functions::RETURNDATE_PHP_NUMERIC 'P' |
||
127 | * Functions::RETURNDATE_PHP_OBJECT 'O' |
||
128 | * Functions::RETURNDATE_EXCEL 'E' |
||
129 | * @return bool Success or failure |
||
130 | */ |
||
131 | public static function setReturnDateType($returnDateType) |
||
144 | 165 | ||
145 | /** |
||
146 | 165 | * Return the current Return Date Format for functions that return a date/time (Excel, PHP Serialized Numeric or PHP Object) |
|
147 | 165 | * |
|
148 | * @category Function Configuration |
||
149 | * @return string Return Date Format |
||
150 | * Possible Return values are: |
||
151 | * Functions::RETURNDATE_PHP_NUMERIC 'P' |
||
152 | * Functions::RETURNDATE_PHP_OBJECT 'O' |
||
153 | * Functions::RETURNDATE_EXCEL 'E' |
||
154 | */ |
||
155 | public static function getReturnDateType() |
||
159 | |||
160 | /** |
||
161 | * DUMMY |
||
162 | * |
||
163 | * @category Error Returns |
||
164 | 328 | * @return string #Not Yet Implemented |
|
165 | */ |
||
166 | 328 | public static function DUMMY() |
|
170 | |||
171 | /** |
||
172 | * DIV0 |
||
173 | * |
||
174 | * @category Error Returns |
||
175 | * @return string #Not Yet Implemented |
||
176 | */ |
||
177 | 1 | public static function DIV0() |
|
181 | |||
182 | /** |
||
183 | * NA |
||
184 | * |
||
185 | * Excel Function: |
||
186 | * =NA() |
||
187 | * |
||
188 | * Returns the error value #N/A |
||
189 | * #N/A is the error value that means "no value is available." |
||
190 | 10 | * |
|
191 | * @category Logical Functions |
||
192 | 10 | * @return string #N/A! |
|
193 | */ |
||
194 | public static function NA() |
||
198 | |||
199 | /** |
||
200 | * NaN |
||
201 | * |
||
202 | * Returns the error value #NUM! |
||
203 | * |
||
204 | * @category Error Returns |
||
205 | * @return string #NUM! |
||
206 | */ |
||
207 | public static function NAN() |
||
211 | 36 | ||
212 | /** |
||
213 | * NAME |
||
214 | * |
||
215 | * Returns the error value #NAME? |
||
216 | * |
||
217 | * @category Error Returns |
||
218 | * @return string #NAME? |
||
219 | */ |
||
220 | public static function NAME() |
||
224 | 163 | ||
225 | /** |
||
226 | 163 | * REF |
|
227 | * |
||
228 | * Returns the error value #REF! |
||
229 | * |
||
230 | * @category Error Returns |
||
231 | * @return string #REF! |
||
232 | */ |
||
233 | public static function REF() |
||
237 | |||
238 | /** |
||
239 | 5 | * NULL |
|
240 | * |
||
241 | 5 | * Returns the error value #NULL! |
|
242 | * |
||
243 | * @category Error Returns |
||
244 | * @return string #NULL! |
||
245 | */ |
||
246 | public static function null() |
||
250 | |||
251 | /** |
||
252 | * VALUE |
||
253 | * |
||
254 | 1 | * Returns the error value #VALUE! |
|
255 | * |
||
256 | 1 | * @category Error Returns |
|
257 | * @return string #VALUE! |
||
258 | */ |
||
259 | public static function VALUE() |
||
263 | |||
264 | public static function isMatrixValue($idx) |
||
268 | |||
269 | 1 | public static function isValue($idx) |
|
273 | |||
274 | public static function isCellValue($idx) |
||
278 | |||
279 | public static function ifCondition($condition) |
||
303 | |||
304 | 3 | /** |
|
305 | * ERROR_TYPE |
||
306 | * |
||
307 | * @param mixed $value Value to check |
||
308 | 5 | * @return bool |
|
309 | */ |
||
310 | 5 | public static function errorType($value = '') |
|
324 | 4 | ||
325 | 4 | /** |
|
326 | * IS_BLANK |
||
327 | * |
||
328 | 5 | * @param mixed $value Value to check |
|
329 | * @return bool |
||
330 | */ |
||
331 | public static function isBlank($value = null) |
||
339 | |||
340 | 14 | /** |
|
341 | * IS_ERR |
||
342 | 14 | * |
|
343 | 14 | * @param mixed $value Value to check |
|
344 | 14 | * @return bool |
|
345 | 7 | */ |
|
346 | public static function isErr($value = '') |
||
352 | |||
353 | /** |
||
354 | * IS_ERROR |
||
355 | * |
||
356 | * @param mixed $value Value to check |
||
357 | * @return bool |
||
358 | */ |
||
359 | 16 | public static function isError($value = '') |
|
369 | |||
370 | /** |
||
371 | * IS_NA |
||
372 | * |
||
373 | * @param mixed $value Value to check |
||
374 | * @return bool |
||
375 | 16 | */ |
|
376 | public static function isNa($value = '') |
||
382 | |||
383 | /** |
||
384 | * IS_EVEN |
||
385 | * |
||
386 | * @param mixed $value Value to check |
||
387 | * @return bool |
||
388 | */ |
||
389 | 56 | View Code Duplication | public static function isEven($value = null) |
401 | |||
402 | /** |
||
403 | * IS_ODD |
||
404 | * |
||
405 | * @param mixed $value Value to check |
||
406 | 18 | * @return bool |
|
407 | */ |
||
408 | 18 | View Code Duplication | public static function isOdd($value = null) |
420 | 20 | ||
421 | /** |
||
422 | 20 | * IS_NUMBER |
|
423 | * |
||
424 | 20 | * @param mixed $value Value to check |
|
425 | 2 | * @return bool |
|
426 | 18 | */ |
|
427 | 7 | public static function isNumber($value = null) |
|
437 | |||
438 | /** |
||
439 | * IS_LOGICAL |
||
440 | 20 | * |
|
441 | * @param mixed $value Value to check |
||
442 | 20 | * @return bool |
|
443 | */ |
||
444 | 20 | public static function isLogical($value = null) |
|
450 | 11 | ||
451 | /** |
||
452 | * IS_TEXT |
||
453 | * |
||
454 | * @param mixed $value Value to check |
||
455 | * @return bool |
||
456 | */ |
||
457 | public static function isText($value = null) |
||
463 | |||
464 | 16 | /** |
|
465 | 8 | * IS_NONTEXT |
|
466 | * |
||
467 | 8 | * @param mixed $value Value to check |
|
468 | * @return bool |
||
469 | */ |
||
470 | public static function isNonText($value = null) |
||
474 | |||
475 | /** |
||
476 | * VERSION |
||
477 | 16 | * |
|
478 | * @return string Version information |
||
479 | 16 | */ |
|
480 | public static function VERSION() |
||
484 | |||
485 | /** |
||
486 | * N |
||
487 | * |
||
488 | * Returns a value converted to a number |
||
489 | * |
||
490 | * @param value The value you want converted |
||
491 | 32 | * @return number N converts values listed in the following table |
|
492 | * If value is or refers to N returns |
||
493 | 32 | * A number That number |
|
494 | * A date The serial number of that date |
||
495 | 32 | * TRUE 1 |
|
496 | * FALSE 0 |
||
497 | * An error value The error value |
||
498 | * Anything else 0 |
||
499 | */ |
||
500 | public static function n($value = null) |
||
523 | |||
524 | /** |
||
525 | * TYPE |
||
526 | * |
||
527 | * Returns a number that identifies the type of a value |
||
528 | * |
||
529 | * @param value The value you want tested |
||
530 | * @return number N converts values listed in the following table |
||
531 | * If value is or refers to N returns |
||
532 | * A number 1 |
||
533 | * Text 2 |
||
534 | * Logical Value 4 |
||
535 | * An error value 16 |
||
536 | * Array or Matrix 64 |
||
537 | 20 | */ |
|
538 | public static function TYPE($value = null) |
||
574 | |||
575 | 16 | /** |
|
576 | * Convert a multi-dimensional array to a simple 1-dimensional array |
||
577 | 16 | * |
|
578 | 16 | * @param array $array Array to be flattened |
|
579 | 3 | * @return array Flattened array |
|
580 | 3 | */ |
|
581 | public static function flattenArray($array) |
||
606 | |||
607 | /** |
||
608 | * Convert a multi-dimensional array to a simple 1-dimensional array, but retain an element of indexing |
||
609 | * |
||
610 | * @param array $array Array to be flattened |
||
611 | * @return array Flattened array |
||
612 | */ |
||
613 | public static function flattenArrayIndexed($array) |
||
638 | |||
639 | /** |
||
640 | 234 | * Convert an array to a single scalar value by extracting the first element |
|
641 | * |
||
642 | * @param mixed $value Array or scalar value |
||
643 | * @return mixed |
||
644 | */ |
||
645 | public static function flattenSingleValue($value = '') |
||
653 | 11 | } |
|
654 | |||
714 |
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.