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 | 76 | 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) |
|
137 | { |
||
138 | 27 | return ['numberformat' => $array]; |
|
139 | } |
||
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(array $pStyles) |
|
159 | { |
||
160 | 30 | if ($this->isSupervisor) { |
|
161 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
||
162 | } else { |
||
163 | 30 | if (isset($pStyles['code'])) { |
|
164 | 30 | $this->setFormatCode($pStyles['code']); |
|
165 | } |
||
166 | } |
||
167 | |||
168 | 30 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get Format Code. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | 28 | public function getFormatCode() |
|
177 | { |
||
178 | 28 | if ($this->isSupervisor) { |
|
179 | 1 | return $this->getSharedComponent()->getFormatCode(); |
|
180 | } |
||
181 | 28 | if ($this->builtInFormatCode !== false) { |
|
182 | 19 | return self::builtInFormatCode($this->builtInFormatCode); |
|
183 | } |
||
184 | |||
185 | 24 | return $this->formatCode; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Set Format Code. |
||
190 | * |
||
191 | * @param string $pValue see self::FORMAT_* |
||
192 | * |
||
193 | * @return NumberFormat |
||
194 | */ |
||
195 | 40 | public function setFormatCode($pValue) |
|
196 | { |
||
197 | 40 | if ($pValue == '') { |
|
198 | $pValue = self::FORMAT_GENERAL; |
||
199 | } |
||
200 | 40 | if ($this->isSupervisor) { |
|
201 | 27 | $styleArray = $this->getStyleArray(['code' => $pValue]); |
|
202 | 27 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
203 | } else { |
||
204 | 40 | $this->formatCode = $pValue; |
|
205 | 40 | $this->builtInFormatCode = self::builtInFormatCodeIndex($pValue); |
|
206 | } |
||
207 | |||
208 | 40 | return $this; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get Built-In Format Code. |
||
213 | * |
||
214 | * @return int |
||
215 | */ |
||
216 | 57 | public function getBuiltInFormatCode() |
|
224 | |||
225 | /** |
||
226 | * Set Built-In Format Code. |
||
227 | * |
||
228 | * @param int $pValue |
||
229 | * |
||
230 | * @return NumberFormat |
||
231 | */ |
||
232 | public function setBuiltInFormatCode($pValue) |
||
244 | |||
245 | /** |
||
246 | * Fill built-in format codes. |
||
247 | */ |
||
248 | 43 | private static function fillBuiltInFormatCodes() |
|
331 | |||
332 | /** |
||
333 | * Get built-in format code. |
||
334 | * |
||
335 | * @param int $pIndex |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | 27 | public static function builtInFormatCode($pIndex) |
|
354 | |||
355 | /** |
||
356 | * Get built-in format code index. |
||
357 | * |
||
358 | * @param string $formatCode |
||
359 | * |
||
360 | * @return int|bool |
||
361 | */ |
||
362 | 40 | public static function builtInFormatCodeIndex($formatCode) |
|
374 | |||
375 | /** |
||
376 | * Get hash code. |
||
377 | * |
||
378 | * @return string Hash code |
||
379 | */ |
||
380 | 68 | public function getHashCode() |
|
392 | |||
393 | /** |
||
394 | * Search/replace values to convert Excel date/time format masks to PHP format masks. |
||
395 | * |
||
396 | * @var array |
||
397 | */ |
||
398 | private static $dateFormatReplacements = [ |
||
399 | // first remove escapes related to non-format characters |
||
400 | '\\' => '', |
||
401 | // 12-hour suffix |
||
402 | 'am/pm' => 'A', |
||
403 | // 4-digit year |
||
404 | 'e' => 'Y', |
||
405 | 'yyyy' => 'Y', |
||
406 | // 2-digit year |
||
407 | 'yy' => 'y', |
||
408 | // first letter of month - no php equivalent |
||
409 | 'mmmmm' => 'M', |
||
410 | // full month name |
||
411 | 'mmmm' => 'F', |
||
412 | // short month name |
||
413 | 'mmm' => 'M', |
||
414 | // mm is minutes if time, but can also be month w/leading zero |
||
415 | // so we try to identify times be the inclusion of a : separator in the mask |
||
416 | // It isn't perfect, but the best way I know how |
||
417 | ':mm' => ':i', |
||
418 | 'mm:' => 'i:', |
||
419 | // month leading zero |
||
420 | 'mm' => 'm', |
||
421 | // month no leading zero |
||
422 | 'm' => 'n', |
||
423 | // full day of week name |
||
424 | 'dddd' => 'l', |
||
425 | // short day of week name |
||
426 | 'ddd' => 'D', |
||
427 | // days leading zero |
||
428 | 'dd' => 'd', |
||
429 | // days no leading zero |
||
430 | 'd' => 'j', |
||
431 | // seconds |
||
432 | 'ss' => 's', |
||
433 | // fractional seconds - no php equivalent |
||
434 | '.s' => '', |
||
435 | ]; |
||
436 | /** |
||
437 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock). |
||
438 | * |
||
439 | * @var array |
||
440 | */ |
||
441 | private static $dateFormatReplacements24 = [ |
||
442 | 'hh' => 'H', |
||
443 | 'h' => 'G', |
||
444 | ]; |
||
445 | /** |
||
446 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock). |
||
447 | * |
||
448 | * @var array |
||
449 | */ |
||
450 | private static $dateFormatReplacements12 = [ |
||
451 | 'hh' => 'h', |
||
452 | 'h' => 'g', |
||
453 | ]; |
||
454 | |||
455 | 18 | private static function setLowercaseCallback($matches) |
|
459 | |||
460 | 7 | private static function escapeQuotesCallback($matches) |
|
464 | |||
465 | 18 | private static function formatAsDate(&$value, &$format) |
|
466 | { |
||
467 | // strip off first part containing e.g. [$-F800] or [$USD-409] |
||
468 | // general syntax: [$<Currency string>-<language info>] |
||
469 | // language info is in hexadecimal |
||
470 | // strip off chinese part like [DBNum1][$-804] |
||
471 | 18 | $format = preg_replace('/^(\[[0-9A-Za-z]*\])*(\[\$[A-Z]*-[0-9A-F]*\])/i', '', $format); |
|
472 | |||
473 | // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case; |
||
474 | // but we don't want to change any quoted strings |
||
475 | 18 | $format = preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', ['self', 'setLowercaseCallback'], $format); |
|
476 | |||
477 | // Only process the non-quoted blocks for date format characters |
||
478 | 18 | $blocks = explode('"', $format); |
|
479 | 18 | foreach ($blocks as $key => &$block) { |
|
480 | 18 | if ($key % 2 == 0) { |
|
481 | 18 | $block = strtr($block, self::$dateFormatReplacements); |
|
482 | 18 | if (!strpos($block, 'A')) { |
|
483 | // 24-hour time format |
||
484 | 18 | $block = strtr($block, self::$dateFormatReplacements24); |
|
485 | } else { |
||
486 | // 12-hour time format |
||
487 | 18 | $block = strtr($block, self::$dateFormatReplacements12); |
|
488 | } |
||
489 | } |
||
490 | } |
||
491 | 18 | $format = implode('"', $blocks); |
|
492 | |||
493 | // escape any quoted characters so that DateTime format() will render them correctly |
||
494 | 18 | $format = preg_replace_callback('/"(.*)"/U', ['self', 'escapeQuotesCallback'], $format); |
|
495 | |||
496 | 18 | $dateObj = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value); |
|
497 | 18 | $value = $dateObj->format($format); |
|
498 | 18 | } |
|
499 | |||
500 | 3 | private static function formatAsPercentage(&$value, &$format) |
|
517 | |||
518 | 4 | private static function formatAsFraction(&$value, &$format) |
|
542 | |||
543 | 6 | private static function complexNumberFormatMask($number, $mask, $level = 0) |
|
582 | |||
583 | /** |
||
584 | * Convert a value in a pre-defined format to a PHP string. |
||
585 | * |
||
586 | * @param mixed $value Value to format |
||
587 | * @param string $format Format code, see = self::FORMAT_* |
||
588 | * @param array $callBack Callback function for additional formatting of string |
||
589 | * |
||
590 | * @return string Formatted string |
||
591 | */ |
||
592 | 81 | public static function toFormattedString($value, $format, $callBack = null) |
|
762 | } |
||
763 |
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.