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 |
||
33 | class NumberFormat extends Supervisor implements IComparable |
||
34 | { |
||
35 | /* Pre-defined formats */ |
||
36 | const FORMAT_GENERAL = 'General'; |
||
37 | |||
38 | const FORMAT_TEXT = '@'; |
||
39 | |||
40 | const FORMAT_NUMBER = '0'; |
||
41 | const FORMAT_NUMBER_00 = '0.00'; |
||
42 | const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'; |
||
43 | const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'; |
||
44 | |||
45 | const FORMAT_PERCENTAGE = '0%'; |
||
46 | const FORMAT_PERCENTAGE_00 = '0.00%'; |
||
47 | |||
48 | const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'; |
||
49 | const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'; |
||
50 | const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'; |
||
51 | const FORMAT_DATE_DMYSLASH = 'd/m/y'; |
||
52 | const FORMAT_DATE_DMYMINUS = 'd-m-y'; |
||
53 | const FORMAT_DATE_DMMINUS = 'd-m'; |
||
54 | const FORMAT_DATE_MYMINUS = 'm-y'; |
||
55 | const FORMAT_DATE_XLSX14 = 'mm-dd-yy'; |
||
56 | const FORMAT_DATE_XLSX15 = 'd-mmm-yy'; |
||
57 | const FORMAT_DATE_XLSX16 = 'd-mmm'; |
||
58 | const FORMAT_DATE_XLSX17 = 'mmm-yy'; |
||
59 | const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'; |
||
60 | const FORMAT_DATE_DATETIME = 'd/m/y h:mm'; |
||
61 | const FORMAT_DATE_TIME1 = 'h:mm AM/PM'; |
||
62 | const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'; |
||
63 | const FORMAT_DATE_TIME3 = 'h:mm'; |
||
64 | const FORMAT_DATE_TIME4 = 'h:mm:ss'; |
||
65 | const FORMAT_DATE_TIME5 = 'mm:ss'; |
||
66 | const FORMAT_DATE_TIME6 = 'h:mm:ss'; |
||
67 | const FORMAT_DATE_TIME7 = 'i:s.S'; |
||
68 | const FORMAT_DATE_TIME8 = 'h:mm:ss;@'; |
||
69 | const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'; |
||
70 | |||
71 | const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'; |
||
72 | const FORMAT_CURRENCY_USD = '$#,##0_-'; |
||
73 | const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'; |
||
74 | |||
75 | /** |
||
76 | * Excel built-in number formats. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected static $builtInFormats; |
||
81 | |||
82 | /** |
||
83 | * Excel built-in number formats (flipped, for faster lookups). |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected static $flippedBuiltInFormats; |
||
88 | |||
89 | /** |
||
90 | * Format Code. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $formatCode = self::FORMAT_GENERAL; |
||
95 | |||
96 | /** |
||
97 | * Built-in format Code. |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $builtInFormatCode = 0; |
||
102 | |||
103 | /** |
||
104 | * Create a new NumberFormat. |
||
105 | * |
||
106 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
107 | * Leave this value at default unless you understand exactly what |
||
108 | * its ramifications are |
||
109 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
110 | * Leave this value at default unless you understand exactly what |
||
111 | * its ramifications are |
||
112 | */ |
||
113 | 76 | public function __construct($isSupervisor = false, $isConditional = false) |
|
114 | { |
||
115 | // Supervisor? |
||
116 | 76 | parent::__construct($isSupervisor); |
|
117 | |||
118 | 76 | if ($isConditional) { |
|
119 | 2 | $this->formatCode = null; |
|
120 | 2 | $this->builtInFormatCode = false; |
|
|
|||
121 | } |
||
122 | 76 | } |
|
123 | |||
124 | /** |
||
125 | * Get the shared style component for the currently active cell in currently active sheet. |
||
126 | * Only used for style supervisor. |
||
127 | * |
||
128 | * @return NumberFormat |
||
129 | */ |
||
130 | 1 | public function getSharedComponent() |
|
131 | { |
||
132 | 1 | return $this->parent->getSharedComponent()->getNumberFormat(); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Build style array from subcomponents. |
||
137 | * |
||
138 | * @param array $array |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 27 | public function getStyleArray($array) |
|
146 | |||
147 | /** |
||
148 | * Apply styles from array. |
||
149 | * <code> |
||
150 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray( |
||
151 | * array( |
||
152 | * 'code' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE |
||
153 | * ) |
||
154 | * ); |
||
155 | * </code>. |
||
156 | * |
||
157 | * @param array $pStyles Array containing style information |
||
158 | * |
||
159 | * @throws PhpSpreadsheetException |
||
160 | * |
||
161 | * @return NumberFormat |
||
162 | */ |
||
163 | 30 | public function applyFromArray(array $pStyles) |
|
175 | |||
176 | /** |
||
177 | * Get Format Code. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 28 | public function getFormatCode() |
|
182 | { |
||
183 | 28 | if ($this->isSupervisor) { |
|
184 | 1 | return $this->getSharedComponent()->getFormatCode(); |
|
185 | } |
||
186 | 28 | if ($this->builtInFormatCode !== false) { |
|
187 | 19 | return self::builtInFormatCode($this->builtInFormatCode); |
|
188 | } |
||
189 | |||
190 | 24 | return $this->formatCode; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Set Format Code. |
||
195 | * |
||
196 | * @param string $pValue see self::FORMAT_* |
||
197 | * |
||
198 | * @return NumberFormat |
||
199 | */ |
||
200 | 40 | public function setFormatCode($pValue) |
|
215 | |||
216 | /** |
||
217 | * Get Built-In Format Code. |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | 57 | public function getBuiltInFormatCode() |
|
222 | { |
||
223 | 57 | if ($this->isSupervisor) { |
|
224 | return $this->getSharedComponent()->getBuiltInFormatCode(); |
||
225 | } |
||
226 | |||
227 | 57 | return $this->builtInFormatCode; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Set Built-In Format Code. |
||
232 | * |
||
233 | * @param int $pValue |
||
234 | * |
||
235 | * @return NumberFormat |
||
236 | */ |
||
237 | public function setBuiltInFormatCode($pValue) |
||
249 | |||
250 | /** |
||
251 | * Fill built-in format codes. |
||
252 | */ |
||
253 | 43 | private static function fillBuiltInFormatCodes() |
|
336 | |||
337 | /** |
||
338 | * Get built-in format code. |
||
339 | * |
||
340 | * @param int $pIndex |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | 27 | public static function builtInFormatCode($pIndex) |
|
345 | { |
||
346 | // Clean parameter |
||
347 | 27 | $pIndex = (int) $pIndex; |
|
348 | |||
349 | // Ensure built-in format codes are available |
||
350 | 27 | self::fillBuiltInFormatCodes(); |
|
351 | |||
352 | // Lookup format code |
||
353 | 27 | if (isset(self::$builtInFormats[$pIndex])) { |
|
354 | 27 | return self::$builtInFormats[$pIndex]; |
|
355 | } |
||
356 | |||
357 | 1 | return ''; |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * Get built-in format code index. |
||
362 | * |
||
363 | * @param string $formatCode |
||
364 | * |
||
365 | * @return int|bool |
||
366 | */ |
||
367 | 40 | public static function builtInFormatCodeIndex($formatCode) |
|
379 | |||
380 | /** |
||
381 | * Get hash code. |
||
382 | * |
||
383 | * @return string Hash code |
||
384 | */ |
||
385 | 68 | public function getHashCode() |
|
397 | |||
398 | /** |
||
399 | * Search/replace values to convert Excel date/time format masks to PHP format masks. |
||
400 | * |
||
401 | * @var array |
||
402 | */ |
||
403 | private static $dateFormatReplacements = [ |
||
404 | // first remove escapes related to non-format characters |
||
405 | '\\' => '', |
||
406 | // 12-hour suffix |
||
407 | 'am/pm' => 'A', |
||
408 | // 4-digit year |
||
409 | 'e' => 'Y', |
||
410 | 'yyyy' => 'Y', |
||
411 | // 2-digit year |
||
412 | 'yy' => 'y', |
||
413 | // first letter of month - no php equivalent |
||
414 | 'mmmmm' => 'M', |
||
415 | // full month name |
||
416 | 'mmmm' => 'F', |
||
417 | // short month name |
||
418 | 'mmm' => 'M', |
||
419 | // mm is minutes if time, but can also be month w/leading zero |
||
420 | // so we try to identify times be the inclusion of a : separator in the mask |
||
421 | // It isn't perfect, but the best way I know how |
||
422 | ':mm' => ':i', |
||
423 | 'mm:' => 'i:', |
||
424 | // month leading zero |
||
425 | 'mm' => 'm', |
||
426 | // month no leading zero |
||
427 | 'm' => 'n', |
||
428 | // full day of week name |
||
429 | 'dddd' => 'l', |
||
430 | // short day of week name |
||
431 | 'ddd' => 'D', |
||
432 | // days leading zero |
||
433 | 'dd' => 'd', |
||
434 | // days no leading zero |
||
435 | 'd' => 'j', |
||
436 | // seconds |
||
437 | 'ss' => 's', |
||
438 | // fractional seconds - no php equivalent |
||
439 | '.s' => '', |
||
440 | ]; |
||
441 | /** |
||
442 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock). |
||
443 | * |
||
444 | * @var array |
||
445 | */ |
||
446 | private static $dateFormatReplacements24 = [ |
||
447 | 'hh' => 'H', |
||
448 | 'h' => 'G', |
||
449 | ]; |
||
450 | /** |
||
451 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock). |
||
452 | * |
||
453 | * @var array |
||
454 | */ |
||
455 | private static $dateFormatReplacements12 = [ |
||
456 | 'hh' => 'h', |
||
457 | 'h' => 'g', |
||
458 | ]; |
||
459 | |||
460 | 18 | private static function setLowercaseCallback($matches) |
|
464 | |||
465 | 7 | private static function escapeQuotesCallback($matches) |
|
469 | |||
470 | 18 | private static function formatAsDate(&$value, &$format) |
|
504 | |||
505 | 3 | private static function formatAsPercentage(&$value, &$format) |
|
522 | |||
523 | 4 | private static function formatAsFraction(&$value, &$format) |
|
547 | |||
548 | 6 | private static function complexNumberFormatMask($number, $mask, $level = 0) |
|
587 | |||
588 | /** |
||
589 | * Convert a value in a pre-defined format to a PHP string. |
||
590 | * |
||
591 | * @param mixed $value Value to format |
||
592 | * @param string $format Format code, see = self::FORMAT_* |
||
593 | * @param array $callBack Callback function for additional formatting of string |
||
594 | * |
||
595 | * @return string Formatted string |
||
596 | */ |
||
597 | 81 | public static function toFormattedString($value, $format, $callBack = null) |
|
767 | } |
||
768 |
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.