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 |
||
11 | class NumberFormat extends Supervisor implements IComparable |
||
12 | { |
||
13 | // Pre-defined formats |
||
14 | const FORMAT_GENERAL = 'General'; |
||
15 | |||
16 | const FORMAT_TEXT = '@'; |
||
17 | |||
18 | const FORMAT_NUMBER = '0'; |
||
19 | const FORMAT_NUMBER_00 = '0.00'; |
||
20 | const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'; |
||
21 | const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'; |
||
22 | |||
23 | const FORMAT_PERCENTAGE = '0%'; |
||
24 | const FORMAT_PERCENTAGE_00 = '0.00%'; |
||
25 | |||
26 | const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'; |
||
27 | const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'; |
||
28 | const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'; |
||
29 | const FORMAT_DATE_DMYSLASH = 'd/m/y'; |
||
30 | const FORMAT_DATE_DMYMINUS = 'd-m-y'; |
||
31 | const FORMAT_DATE_DMMINUS = 'd-m'; |
||
32 | const FORMAT_DATE_MYMINUS = 'm-y'; |
||
33 | const FORMAT_DATE_XLSX14 = 'mm-dd-yy'; |
||
34 | const FORMAT_DATE_XLSX15 = 'd-mmm-yy'; |
||
35 | const FORMAT_DATE_XLSX16 = 'd-mmm'; |
||
36 | const FORMAT_DATE_XLSX17 = 'mmm-yy'; |
||
37 | const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'; |
||
38 | const FORMAT_DATE_DATETIME = 'd/m/y h:mm'; |
||
39 | const FORMAT_DATE_TIME1 = 'h:mm AM/PM'; |
||
40 | const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'; |
||
41 | const FORMAT_DATE_TIME3 = 'h:mm'; |
||
42 | const FORMAT_DATE_TIME4 = 'h:mm:ss'; |
||
43 | const FORMAT_DATE_TIME5 = 'mm:ss'; |
||
44 | const FORMAT_DATE_TIME6 = 'h:mm:ss'; |
||
45 | const FORMAT_DATE_TIME7 = 'i:s.S'; |
||
46 | const FORMAT_DATE_TIME8 = 'h:mm:ss;@'; |
||
47 | const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'; |
||
48 | |||
49 | const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'; |
||
50 | const FORMAT_CURRENCY_USD = '$#,##0_-'; |
||
51 | const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'; |
||
52 | |||
53 | /** |
||
54 | * Excel built-in number formats. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $builtInFormats; |
||
59 | |||
60 | /** |
||
61 | * Excel built-in number formats (flipped, for faster lookups). |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected static $flippedBuiltInFormats; |
||
66 | |||
67 | /** |
||
68 | * Format Code. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $formatCode = self::FORMAT_GENERAL; |
||
73 | |||
74 | /** |
||
75 | * Built-in format Code. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $builtInFormatCode = 0; |
||
80 | |||
81 | /** |
||
82 | * Create a new NumberFormat. |
||
83 | * |
||
84 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
85 | * Leave this value at default unless you understand exactly what |
||
86 | * its ramifications are |
||
87 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
88 | * Leave this value at default unless you understand exactly what |
||
89 | * its ramifications are |
||
90 | */ |
||
91 | 134 | public function __construct($isSupervisor = false, $isConditional = false) |
|
101 | |||
102 | /** |
||
103 | * Get the shared style component for the currently active cell in currently active sheet. |
||
104 | * Only used for style supervisor. |
||
105 | * |
||
106 | * @return NumberFormat |
||
107 | */ |
||
108 | 5 | public function getSharedComponent() |
|
112 | |||
113 | /** |
||
114 | * Build style array from subcomponents. |
||
115 | * |
||
116 | * @param array $array |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | 32 | public function getStyleArray($array) |
|
124 | |||
125 | /** |
||
126 | * Apply styles from array. |
||
127 | * <code> |
||
128 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray( |
||
129 | * array( |
||
130 | * 'formatCode' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE |
||
131 | * ) |
||
132 | * ); |
||
133 | * </code>. |
||
134 | * |
||
135 | * @param array $pStyles Array containing style information |
||
136 | * |
||
137 | * @throws PhpSpreadsheetException |
||
138 | * |
||
139 | * @return NumberFormat |
||
140 | */ |
||
141 | 36 | public function applyFromArray(array $pStyles) |
|
153 | |||
154 | /** |
||
155 | * Get Format Code. |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | 53 | public function getFormatCode() |
|
170 | |||
171 | /** |
||
172 | * Set Format Code. |
||
173 | * |
||
174 | * @param string $pValue see self::FORMAT_* |
||
175 | * |
||
176 | * @return NumberFormat |
||
177 | */ |
||
178 | 63 | View Code Duplication | public function setFormatCode($pValue) |
179 | { |
||
180 | 63 | if ($pValue == '') { |
|
181 | $pValue = self::FORMAT_GENERAL; |
||
182 | } |
||
183 | 63 | if ($this->isSupervisor) { |
|
184 | 32 | $styleArray = $this->getStyleArray(['formatCode' => $pValue]); |
|
185 | 32 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
186 | } else { |
||
187 | 63 | $this->formatCode = $pValue; |
|
188 | 63 | $this->builtInFormatCode = self::builtInFormatCodeIndex($pValue); |
|
189 | } |
||
190 | |||
191 | 63 | return $this; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get Built-In Format Code. |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | 58 | public function getBuiltInFormatCode() |
|
207 | |||
208 | /** |
||
209 | * Set Built-In Format Code. |
||
210 | * |
||
211 | * @param int $pValue |
||
212 | * |
||
213 | * @return NumberFormat |
||
214 | */ |
||
215 | public function setBuiltInFormatCode($pValue) |
||
227 | |||
228 | /** |
||
229 | * Fill built-in format codes. |
||
230 | */ |
||
231 | 78 | private static function fillBuiltInFormatCodes() |
|
314 | |||
315 | /** |
||
316 | * Get built-in format code. |
||
317 | * |
||
318 | * @param int $pIndex |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 61 | public static function builtInFormatCode($pIndex) |
|
337 | |||
338 | /** |
||
339 | * Get built-in format code index. |
||
340 | * |
||
341 | * @param string $formatCode |
||
342 | * |
||
343 | * @return bool|int |
||
344 | */ |
||
345 | 63 | public static function builtInFormatCodeIndex($formatCode) |
|
357 | |||
358 | /** |
||
359 | * Get hash code. |
||
360 | * |
||
361 | * @return string Hash code |
||
362 | */ |
||
363 | 76 | public function getHashCode() |
|
375 | |||
376 | /** |
||
377 | * Search/replace values to convert Excel date/time format masks to PHP format masks. |
||
378 | * |
||
379 | * @var array |
||
380 | */ |
||
381 | private static $dateFormatReplacements = [ |
||
382 | // first remove escapes related to non-format characters |
||
383 | '\\' => '', |
||
384 | // 12-hour suffix |
||
385 | 'am/pm' => 'A', |
||
386 | // 4-digit year |
||
387 | 'e' => 'Y', |
||
388 | 'yyyy' => 'Y', |
||
389 | // 2-digit year |
||
390 | 'yy' => 'y', |
||
391 | // first letter of month - no php equivalent |
||
392 | 'mmmmm' => 'M', |
||
393 | // full month name |
||
394 | 'mmmm' => 'F', |
||
395 | // short month name |
||
396 | 'mmm' => 'M', |
||
397 | // mm is minutes if time, but can also be month w/leading zero |
||
398 | // so we try to identify times be the inclusion of a : separator in the mask |
||
399 | // It isn't perfect, but the best way I know how |
||
400 | ':mm' => ':i', |
||
401 | 'mm:' => 'i:', |
||
402 | // month leading zero |
||
403 | 'mm' => 'm', |
||
404 | // month no leading zero |
||
405 | 'm' => 'n', |
||
406 | // full day of week name |
||
407 | 'dddd' => 'l', |
||
408 | // short day of week name |
||
409 | 'ddd' => 'D', |
||
410 | // days leading zero |
||
411 | 'dd' => 'd', |
||
412 | // days no leading zero |
||
413 | 'd' => 'j', |
||
414 | // seconds |
||
415 | 'ss' => 's', |
||
416 | // fractional seconds - no php equivalent |
||
417 | '.s' => '', |
||
418 | ]; |
||
419 | /** |
||
420 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock). |
||
421 | * |
||
422 | * @var array |
||
423 | */ |
||
424 | private static $dateFormatReplacements24 = [ |
||
425 | 'hh' => 'H', |
||
426 | 'h' => 'G', |
||
427 | ]; |
||
428 | /** |
||
429 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock). |
||
430 | * |
||
431 | * @var array |
||
432 | */ |
||
433 | private static $dateFormatReplacements12 = [ |
||
434 | 'hh' => 'h', |
||
435 | 'h' => 'g', |
||
436 | ]; |
||
437 | |||
438 | 27 | private static function setLowercaseCallback($matches) |
|
442 | |||
443 | 11 | private static function escapeQuotesCallback($matches) |
|
447 | |||
448 | 27 | private static function formatAsDate(&$value, &$format) |
|
482 | |||
483 | 4 | private static function formatAsPercentage(&$value, &$format) |
|
484 | { |
||
485 | 4 | if ($format === self::FORMAT_PERCENTAGE) { |
|
486 | 3 | $value = round((100 * $value), 0) . '%'; |
|
487 | } else { |
||
488 | 1 | if (preg_match('/\.[#0]+/', $format, $m)) { |
|
489 | 1 | $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1); |
|
490 | 1 | $format = str_replace($m[0], $s, $format); |
|
491 | } |
||
492 | 1 | if (preg_match('/^[#0]+/', $format, $m)) { |
|
493 | 1 | $format = str_replace($m[0], strlen($m[0]), $format); |
|
494 | } |
||
495 | 1 | $format = '%' . str_replace('%', 'f%%', $format); |
|
496 | |||
497 | 1 | $value = sprintf($format, 100 * $value); |
|
498 | } |
||
499 | 4 | } |
|
500 | |||
501 | 4 | private static function formatAsFraction(&$value, &$format) |
|
525 | |||
526 | 6 | private static function complexNumberFormatMask($number, $mask) |
|
527 | { |
||
565 | |||
566 | /** |
||
567 | * Convert a value in a pre-defined format to a PHP string. |
||
568 | * |
||
569 | * @param mixed $value Value to format |
||
570 | * @param string $format Format code, see = self::FORMAT_* |
||
571 | * @param array $callBack Callback function for additional formatting of string |
||
572 | * |
||
573 | * @return string Formatted string |
||
574 | */ |
||
575 | 107 | public static function toFormattedString($value, $format, $callBack = null) |
|
743 | } |
||
744 |
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.