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 PHPExcel_Style_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 PHPExcel_Style_NumberFormat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable |
||
| 37 | { |
||
| 38 | /* Pre-defined formats */ |
||
| 39 | const FORMAT_GENERAL = 'General'; |
||
| 40 | |||
| 41 | const FORMAT_TEXT = '@'; |
||
| 42 | |||
| 43 | const FORMAT_NUMBER = '0'; |
||
| 44 | const FORMAT_NUMBER_00 = '0.00'; |
||
| 45 | const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'; |
||
| 46 | const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'; |
||
| 47 | |||
| 48 | const FORMAT_PERCENTAGE = '0%'; |
||
| 49 | const FORMAT_PERCENTAGE_00 = '0.00%'; |
||
| 50 | |||
| 51 | const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'; |
||
| 52 | const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'; |
||
| 53 | const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'; |
||
| 54 | const FORMAT_DATE_DMYSLASH = 'd/m/y'; |
||
| 55 | const FORMAT_DATE_DMYMINUS = 'd-m-y'; |
||
| 56 | const FORMAT_DATE_DMMINUS = 'd-m'; |
||
| 57 | const FORMAT_DATE_MYMINUS = 'm-y'; |
||
| 58 | const FORMAT_DATE_XLSX14 = 'mm-dd-yy'; |
||
| 59 | const FORMAT_DATE_XLSX15 = 'd-mmm-yy'; |
||
| 60 | const FORMAT_DATE_XLSX16 = 'd-mmm'; |
||
| 61 | const FORMAT_DATE_XLSX17 = 'mmm-yy'; |
||
| 62 | const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'; |
||
| 63 | const FORMAT_DATE_DATETIME = 'd/m/y h:mm'; |
||
| 64 | const FORMAT_DATE_TIME1 = 'h:mm AM/PM'; |
||
| 65 | const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'; |
||
| 66 | const FORMAT_DATE_TIME3 = 'h:mm'; |
||
| 67 | const FORMAT_DATE_TIME4 = 'h:mm:ss'; |
||
| 68 | const FORMAT_DATE_TIME5 = 'mm:ss'; |
||
| 69 | const FORMAT_DATE_TIME6 = 'h:mm:ss'; |
||
| 70 | const FORMAT_DATE_TIME7 = 'i:s.S'; |
||
| 71 | const FORMAT_DATE_TIME8 = 'h:mm:ss;@'; |
||
| 72 | const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'; |
||
| 73 | |||
| 74 | const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'; |
||
| 75 | const FORMAT_CURRENCY_USD = '$#,##0_-'; |
||
| 76 | const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Excel built-in number formats |
||
| 80 | * |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private static $_builtInFormats; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Excel built-in number formats (flipped, for faster lookups) |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private static $_flippedBuiltInFormats; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Format Code |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Built-in format Code |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private $_builtInFormatCode = 0; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Parent Borders |
||
| 108 | * |
||
| 109 | * @var _parentPropertyName string |
||
| 110 | */ |
||
| 111 | private $_parentPropertyName; |
||
|
|
|||
| 112 | |||
| 113 | /** |
||
| 114 | * Supervisor? |
||
| 115 | * |
||
| 116 | * @var boolean |
||
| 117 | */ |
||
| 118 | private $_isSupervisor; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Parent. Only used for supervisor |
||
| 122 | * |
||
| 123 | * @var PHPExcel_Style |
||
| 124 | */ |
||
| 125 | private $_parent; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Create a new PHPExcel_Style_NumberFormat |
||
| 129 | * |
||
| 130 | * @param boolean $isSupervisor Flag indicating if this is a supervisor or not |
||
| 131 | */ |
||
| 132 | public function __construct($isSupervisor = false) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Bind parent. Only used for supervisor |
||
| 140 | * |
||
| 141 | * @param PHPExcel_Style $parent |
||
| 142 | * @return PHPExcel_Style_NumberFormat |
||
| 143 | */ |
||
| 144 | public function bindParent($parent) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Is this a supervisor or a real style component? |
||
| 151 | * |
||
| 152 | * @return boolean |
||
| 153 | */ |
||
| 154 | public function getIsSupervisor() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the shared style component for the currently active cell in currently active sheet. |
||
| 161 | * Only used for style supervisor |
||
| 162 | * |
||
| 163 | * @return PHPExcel_Style_NumberFormat |
||
| 164 | */ |
||
| 165 | public function getSharedComponent() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the currently active sheet. Only used for supervisor |
||
| 172 | * |
||
| 173 | * @return PHPExcel_Worksheet |
||
| 174 | */ |
||
| 175 | public function getActiveSheet() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the currently active cell coordinate in currently active sheet. |
||
| 182 | * Only used for supervisor |
||
| 183 | * |
||
| 184 | * @return string E.g. 'A1' |
||
| 185 | */ |
||
| 186 | public function getSelectedCells() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the currently active cell coordinate in currently active sheet. |
||
| 193 | * Only used for supervisor |
||
| 194 | * |
||
| 195 | * @return string E.g. 'A1' |
||
| 196 | */ |
||
| 197 | public function getActiveCell() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Build style array from subcomponents |
||
| 204 | * |
||
| 205 | * @param array $array |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function getStyleArray($array) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Apply styles from array |
||
| 215 | * |
||
| 216 | * <code> |
||
| 217 | * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray( |
||
| 218 | * array( |
||
| 219 | * 'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE |
||
| 220 | * ) |
||
| 221 | * ); |
||
| 222 | * </code> |
||
| 223 | * |
||
| 224 | * @param array $pStyles Array containing style information |
||
| 225 | * @throws Exception |
||
| 226 | * @return PHPExcel_Style_NumberFormat |
||
| 227 | */ |
||
| 228 | public function applyFromArray($pStyles = null) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get Format Code |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getFormatCode() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set Format Code |
||
| 263 | * |
||
| 264 | * @param string $pValue |
||
| 265 | * @return PHPExcel_Style_NumberFormat |
||
| 266 | */ |
||
| 267 | public function setFormatCode($pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get Built-In Format Code |
||
| 284 | * |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function getBuiltInFormatCode() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set Built-In Format Code |
||
| 297 | * |
||
| 298 | * @param int $pValue |
||
| 299 | * @return PHPExcel_Style_NumberFormat |
||
| 300 | */ |
||
| 301 | public function setBuiltInFormatCode($pValue = 0) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Fill built-in format codes |
||
| 316 | */ |
||
| 317 | private static function fillBuiltInFormatCodes() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get built-in format code |
||
| 381 | * |
||
| 382 | * @param int $pIndex |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public static function builtInFormatCode($pIndex) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get built-in format code index |
||
| 403 | * |
||
| 404 | * @param string $formatCode |
||
| 405 | * @return int|boolean |
||
| 406 | */ |
||
| 407 | public static function builtInFormatCodeIndex($formatCode) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get hash code |
||
| 422 | * |
||
| 423 | * @return string Hash code |
||
| 424 | */ |
||
| 425 | public function getHashCode() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
| 439 | */ |
||
| 440 | public function __clone() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Search/replace values to convert Excel date/time format masks to PHP format masks |
||
| 454 | * |
||
| 455 | * @var array |
||
| 456 | */ |
||
| 457 | private static $_dateFormatReplacements = array( |
||
| 458 | // first remove escapes related to non-format characters |
||
| 459 | '\\' => '', |
||
| 460 | // 12-hour suffix |
||
| 461 | 'am/pm' => 'A', |
||
| 462 | // 4-digit year |
||
| 463 | 'yyyy' => 'Y', |
||
| 464 | // 2-digit year |
||
| 465 | 'yy' => 'y', |
||
| 466 | // first letter of month - no php equivalent |
||
| 467 | 'mmmmm' => 'M', |
||
| 468 | // full month name |
||
| 469 | 'mmmm' => 'F', |
||
| 470 | // short month name |
||
| 471 | 'mmm' => 'M', |
||
| 472 | // mm is minutes if time or month w/leading zero |
||
| 473 | ':mm' => ':i', |
||
| 474 | // month leading zero |
||
| 475 | 'mm' => 'm', |
||
| 476 | // month no leading zero |
||
| 477 | 'm' => 'n', |
||
| 478 | // full day of week name |
||
| 479 | 'dddd' => 'l', |
||
| 480 | // short day of week name |
||
| 481 | 'ddd' => 'D', |
||
| 482 | // days leading zero |
||
| 483 | 'dd' => 'd', |
||
| 484 | // days no leading zero |
||
| 485 | 'd' => 'j', |
||
| 486 | // seconds |
||
| 487 | 'ss' => 's', |
||
| 488 | // fractional seconds - no php equivalent |
||
| 489 | '.s' => '' |
||
| 490 | ); |
||
| 491 | /** |
||
| 492 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock) |
||
| 493 | * |
||
| 494 | * @var array |
||
| 495 | */ |
||
| 496 | private static $_dateFormatReplacements24 = array( |
||
| 497 | 'hh' => 'H', |
||
| 498 | 'h' => 'G' |
||
| 499 | ); |
||
| 500 | /** |
||
| 501 | * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock) |
||
| 502 | * |
||
| 503 | * @var array |
||
| 504 | */ |
||
| 505 | private static $_dateFormatReplacements12 = array( |
||
| 506 | 'hh' => 'h', |
||
| 507 | 'h' => 'g' |
||
| 508 | ); |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Convert a value in a pre-defined format to a PHP string |
||
| 512 | * |
||
| 513 | * @param mixed $value Value to format |
||
| 514 | * @param string $format Format code |
||
| 515 | * @param array $callBack Callback function for additional formatting of string |
||
| 516 | * @return string Formatted string |
||
| 517 | */ |
||
| 518 | public static function toFormattedString($value = '', $format = '', $callBack = null) |
||
| 730 | |||
| 731 | } |
||
| 732 |
This check marks private properties in classes that are never used. Those properties can be removed.