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 NumberFormat 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 NumberFormat, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class NumberFormat extends Supervisor implements \PhpOffice\PhpSpreadsheet\IComparable |
||
28 | { |
||
29 | /* Pre-defined formats */ |
||
30 | const FORMAT_GENERAL = 'General'; |
||
31 | |||
32 | const FORMAT_TEXT = '@'; |
||
33 | |||
34 | const FORMAT_NUMBER = '0'; |
||
35 | const FORMAT_NUMBER_00 = '0.00'; |
||
36 | const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'; |
||
37 | const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'; |
||
38 | |||
39 | const FORMAT_PERCENTAGE = '0%'; |
||
40 | const FORMAT_PERCENTAGE_00 = '0.00%'; |
||
41 | |||
42 | const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'; |
||
43 | const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'; |
||
44 | const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'; |
||
45 | const FORMAT_DATE_DMYSLASH = 'd/m/y'; |
||
46 | const FORMAT_DATE_DMYMINUS = 'd-m-y'; |
||
47 | const FORMAT_DATE_DMMINUS = 'd-m'; |
||
48 | const FORMAT_DATE_MYMINUS = 'm-y'; |
||
49 | const FORMAT_DATE_XLSX14 = 'mm-dd-yy'; |
||
50 | const FORMAT_DATE_XLSX15 = 'd-mmm-yy'; |
||
51 | const FORMAT_DATE_XLSX16 = 'd-mmm'; |
||
52 | const FORMAT_DATE_XLSX17 = 'mmm-yy'; |
||
53 | const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'; |
||
54 | const FORMAT_DATE_DATETIME = 'd/m/y h:mm'; |
||
55 | const FORMAT_DATE_TIME1 = 'h:mm AM/PM'; |
||
56 | const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'; |
||
57 | const FORMAT_DATE_TIME3 = 'h:mm'; |
||
58 | const FORMAT_DATE_TIME4 = 'h:mm:ss'; |
||
59 | const FORMAT_DATE_TIME5 = 'mm:ss'; |
||
60 | const FORMAT_DATE_TIME6 = 'h:mm:ss'; |
||
61 | const FORMAT_DATE_TIME7 = 'i:s.S'; |
||
62 | const FORMAT_DATE_TIME8 = 'h:mm:ss;@'; |
||
63 | const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'; |
||
64 | |||
65 | const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'; |
||
66 | const FORMAT_CURRENCY_USD = '$#,##0_-'; |
||
67 | const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'; |
||
68 | |||
69 | /** |
||
70 | * Excel built-in number formats. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected static $builtInFormats; |
||
75 | |||
76 | /** |
||
77 | * Excel built-in number formats (flipped, for faster lookups). |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected static $flippedBuiltInFormats; |
||
82 | |||
83 | /** |
||
84 | * Format Code. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $formatCode = self::FORMAT_GENERAL; |
||
89 | |||
90 | /** |
||
91 | * Built-in format Code. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $builtInFormatCode = 0; |
||
96 | |||
97 | /** |
||
98 | * Create a new NumberFormat. |
||
99 | * |
||
100 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
101 | * Leave this value at default unless you understand exactly what |
||
102 | * its ramifications are |
||
103 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
104 | * Leave this value at default unless you understand exactly what |
||
105 | * its ramifications are |
||
106 | */ |
||
107 | 73 | public function __construct($isSupervisor = false, $isConditional = false) |
|
117 | |||
118 | /** |
||
119 | * Get the shared style component for the currently active cell in currently active sheet. |
||
120 | * Only used for style supervisor. |
||
121 | * |
||
122 | * @return NumberFormat |
||
123 | */ |
||
124 | 1 | public function getSharedComponent() |
|
128 | |||
129 | /** |
||
130 | * Build style array from subcomponents. |
||
131 | * |
||
132 | * @param array $array |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 27 | public function getStyleArray($array) |
|
140 | |||
141 | /** |
||
142 | * Apply styles from array. |
||
143 | * |
||
144 | * <code> |
||
145 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray( |
||
146 | * array( |
||
147 | * 'code' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE |
||
148 | * ) |
||
149 | * ); |
||
150 | * </code> |
||
151 | * |
||
152 | * @param array $pStyles Array containing style information |
||
153 | * |
||
154 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
155 | * |
||
156 | * @return NumberFormat |
||
157 | */ |
||
158 | 30 | public function applyFromArray($pStyles = null) |
|
174 | |||
175 | /** |
||
176 | * Get Format Code. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 27 | public function getFormatCode() |
|
191 | |||
192 | /** |
||
193 | * Set Format Code. |
||
194 | * |
||
195 | * @param string $pValue |
||
196 | * |
||
197 | * @return NumberFormat |
||
198 | */ |
||
199 | 40 | View Code Duplication | public function setFormatCode($pValue = self::FORMAT_GENERAL) |
214 | |||
215 | /** |
||
216 | * Get Built-In Format Code. |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | 58 | public function getBuiltInFormatCode() |
|
228 | |||
229 | /** |
||
230 | * Set Built-In Format Code. |
||
231 | * |
||
232 | * @param int $pValue |
||
233 | * |
||
234 | * @return NumberFormat |
||
235 | */ |
||
236 | public function setBuiltInFormatCode($pValue = 0) |
||
248 | |||
249 | /** |
||
250 | * Fill built-in format codes. |
||
251 | */ |
||
252 | 42 | private static function fillBuiltInFormatCodes() |
|
335 | |||
336 | /** |
||
337 | * Get built-in format code. |
||
338 | * |
||
339 | * @param int $pIndex |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 26 | public static function builtInFormatCode($pIndex) |
|
358 | |||
359 | /** |
||
360 | * Get built-in format code index. |
||
361 | * |
||
362 | * @param string $formatCode |
||
363 | * |
||
364 | * @return int|bool |
||
365 | */ |
||
366 | 40 | public static function builtInFormatCodeIndex($formatCode) |
|
378 | |||
379 | /** |
||
380 | * Get hash code. |
||
381 | * |
||
382 | * @return string Hash code |
||
383 | */ |
||
384 | 69 | public function getHashCode() |
|
396 | |||
397 | /** |
||
398 | * Search/replace values to convert Excel date/time format masks to PHP format masks. |
||
399 | * |
||
400 | * @var array |
||
401 | */ |
||
402 | private static $dateFormatReplacements = [ |
||
403 | // first remove escapes related to non-format characters |
||
404 | '\\' => '', |
||
405 | // 12-hour suffix |
||
406 | 'am/pm' => 'A', |
||
407 | // 4-digit year |
||
408 | 'e' => 'Y', |
||
409 | 'yyyy' => 'Y', |
||
410 | // 2-digit year |
||
411 | 'yy' => 'y', |
||
412 | // first letter of month - no php equivalent |
||
413 | 'mmmmm' => 'M', |
||
414 | // full month name |
||
415 | 'mmmm' => 'F', |
||
416 | // short month name |
||
417 | 'mmm' => 'M', |
||
418 | // mm is minutes if time, but can also be month w/leading zero |
||
419 | // so we try to identify times be the inclusion of a : separator in the mask |
||
420 | // It isn't perfect, but the best way I know how |
||
421 | ':mm' => ':i', |
||
422 | 'mm:' => 'i:', |
||
423 | // month leading zero |
||
424 | 'mm' => 'm', |
||
425 | // month no leading zero |
||
426 | 'm' => 'n', |
||
427 | // full day of week name |
||
428 | 'dddd' => 'l', |
||
429 | // short day of week name |
||
430 | 'ddd' => 'D', |
||
431 | // days leading zero |
||
432 | 'dd' => 'd', |
||
433 | // days no leading zero |
||
434 | 'd' => 'j', |
||
435 | // seconds |
||
436 | 'ss' => 's', |
||
437 | // fractional seconds - no php equivalent |
||
438 | '.s' => '', |
||
439 | ]; |
||
440 | /** |
||
441 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock). |
||
442 | * |
||
443 | * @var array |
||
444 | */ |
||
445 | private static $dateFormatReplacements24 = [ |
||
446 | 'hh' => 'H', |
||
447 | 'h' => 'G', |
||
448 | ]; |
||
449 | /** |
||
450 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock). |
||
451 | * |
||
452 | * @var array |
||
453 | */ |
||
454 | private static $dateFormatReplacements12 = [ |
||
455 | 'hh' => 'h', |
||
456 | 'h' => 'g', |
||
457 | ]; |
||
458 | |||
459 | 18 | private static function setLowercaseCallback($matches) |
|
463 | |||
464 | 7 | private static function escapeQuotesCallback($matches) |
|
468 | |||
469 | 18 | private static function formatAsDate(&$value, &$format) |
|
470 | { |
||
471 | // strip off first part containing e.g. [$-F800] or [$USD-409] |
||
472 | // general syntax: [$<Currency string>-<language info>] |
||
473 | // language info is in hexadecimal |
||
474 | // strip off chinese part like [DBNum1][$-804] |
||
475 | 18 | $format = preg_replace('/^(\[[0-9A-Za-z]*\])*(\[\$[A-Z]*-[0-9A-F]*\])/i', '', $format); |
|
476 | |||
477 | // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case; |
||
478 | // but we don't want to change any quoted strings |
||
479 | 18 | $format = preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', ['self', 'setLowercaseCallback'], $format); |
|
480 | |||
481 | // Only process the non-quoted blocks for date format characters |
||
482 | 18 | $blocks = explode('"', $format); |
|
483 | 18 | foreach ($blocks as $key => &$block) { |
|
484 | 18 | if ($key % 2 == 0) { |
|
485 | 18 | $block = strtr($block, self::$dateFormatReplacements); |
|
486 | 18 | if (!strpos($block, 'A')) { |
|
487 | // 24-hour time format |
||
488 | 18 | $block = strtr($block, self::$dateFormatReplacements24); |
|
489 | } else { |
||
490 | // 12-hour time format |
||
491 | $block = strtr($block, self::$dateFormatReplacements12); |
||
492 | } |
||
493 | } |
||
494 | } |
||
495 | 18 | $format = implode('"', $blocks); |
|
496 | |||
497 | // escape any quoted characters so that DateTime format() will render them correctly |
||
498 | 18 | $format = preg_replace_callback('/"(.*)"/U', ['self', 'escapeQuotesCallback'], $format); |
|
499 | |||
500 | 18 | $dateObj = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value); |
|
501 | 18 | $value = $dateObj->format($format); |
|
502 | 18 | } |
|
503 | |||
504 | 3 | private static function formatAsPercentage(&$value, &$format) |
|
521 | |||
522 | 4 | private static function formatAsFraction(&$value, &$format) |
|
546 | |||
547 | 6 | private static function complexNumberFormatMask($number, $mask, $level = 0) |
|
586 | |||
587 | /** |
||
588 | * Convert a value in a pre-defined format to a PHP string. |
||
589 | * |
||
590 | * @param mixed $value Value to format |
||
591 | * @param string $format Format code |
||
592 | * @param array $callBack Callback function for additional formatting of string |
||
593 | * |
||
594 | * @return string Formatted string |
||
595 | */ |
||
596 | 80 | public static function toFormattedString($value = '0', $format = self::FORMAT_GENERAL, $callBack = null) |
|
766 | } |
||
767 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.