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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class PHPExcel_Style implements PHPExcel_IComparable |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Font |
||
| 40 | * |
||
| 41 | * @var PHPExcel_Style_Font |
||
| 42 | */ |
||
| 43 | private $_font; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Fill |
||
| 47 | * |
||
| 48 | * @var PHPExcel_Style_Fill |
||
| 49 | */ |
||
| 50 | private $_fill; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Borders |
||
| 54 | * |
||
| 55 | * @var PHPExcel_Style_Borders |
||
| 56 | */ |
||
| 57 | private $_borders; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Alignment |
||
| 61 | * |
||
| 62 | * @var PHPExcel_Style_Alignment |
||
| 63 | */ |
||
| 64 | private $_alignment; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Number Format |
||
| 68 | * |
||
| 69 | * @var PHPExcel_Style_NumberFormat |
||
| 70 | */ |
||
| 71 | private $_numberFormat; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Conditional styles |
||
| 75 | * |
||
| 76 | * @var PHPExcel_Style_Conditional[] |
||
| 77 | */ |
||
| 78 | private $_conditionalStyles; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Protection |
||
| 82 | * |
||
| 83 | * @var PHPExcel_Style_Protection |
||
| 84 | */ |
||
| 85 | private $_protection; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Style supervisor? |
||
| 89 | * |
||
| 90 | * @var boolean |
||
| 91 | */ |
||
| 92 | private $_isSupervisor; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Parent. Only used for style supervisor |
||
| 96 | * |
||
| 97 | * @var PHPExcel |
||
| 98 | */ |
||
| 99 | private $_parent; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Index of style in collection. Only used for real style. |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | private $_index; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create a new PHPExcel_Style |
||
| 110 | * |
||
| 111 | * @param boolean $isSupervisor |
||
| 112 | */ |
||
| 113 | public function __construct($isSupervisor = false) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Bind parent. Only used for supervisor |
||
| 140 | * |
||
| 141 | * @param PHPExcel $parent |
||
| 142 | * @return PHPExcel_Style |
||
| 143 | */ |
||
| 144 | public function bindParent($parent) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Is this a supervisor or a real style component? |
||
| 152 | * |
||
| 153 | * @return boolean |
||
| 154 | */ |
||
| 155 | public function getIsSupervisor() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the shared style component for the currently active cell in currently active sheet. |
||
| 162 | * Only used for style supervisor |
||
| 163 | * |
||
| 164 | * @return PHPExcel_Style |
||
| 165 | */ |
||
| 166 | public function getSharedComponent() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the currently active sheet. Only used for supervisor |
||
| 182 | * |
||
| 183 | * @return PHPExcel_Worksheet |
||
| 184 | */ |
||
| 185 | public function getActiveSheet() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the currently active cell coordinate in currently active sheet. |
||
| 192 | * Only used for supervisor |
||
| 193 | * |
||
| 194 | * @return string E.g. 'A1' |
||
| 195 | */ |
||
| 196 | public function getSelectedCells() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the currently active cell coordinate in currently active sheet. |
||
| 203 | * Only used for supervisor |
||
| 204 | * |
||
| 205 | * @return string E.g. 'A1' |
||
| 206 | */ |
||
| 207 | public function getActiveCell() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get parent. Only used for style supervisor |
||
| 214 | * |
||
| 215 | * @return PHPExcel |
||
| 216 | */ |
||
| 217 | public function getParent() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Apply styles from array |
||
| 224 | * |
||
| 225 | * <code> |
||
| 226 | * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray( |
||
| 227 | * array( |
||
| 228 | * 'font' => array( |
||
| 229 | * 'name' => 'Arial', |
||
| 230 | * 'bold' => true, |
||
| 231 | * 'italic' => false, |
||
| 232 | * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE, |
||
| 233 | * 'strike' => false, |
||
| 234 | * 'color' => array( |
||
| 235 | * 'rgb' => '808080' |
||
| 236 | * ) |
||
| 237 | * ), |
||
| 238 | * 'borders' => array( |
||
| 239 | * 'bottom' => array( |
||
| 240 | * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT, |
||
| 241 | * 'color' => array( |
||
| 242 | * 'rgb' => '808080' |
||
| 243 | * ) |
||
| 244 | * ), |
||
| 245 | * 'top' => array( |
||
| 246 | * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT, |
||
| 247 | * 'color' => array( |
||
| 248 | * 'rgb' => '808080' |
||
| 249 | * ) |
||
| 250 | * ) |
||
| 251 | * ) |
||
| 252 | * ) |
||
| 253 | * ); |
||
| 254 | * </code> |
||
| 255 | * |
||
| 256 | * @param array $pStyles Array containing style information |
||
| 257 | * @param boolean $pAdvanced Advanced mode for setting borders. |
||
| 258 | * @throws Exception |
||
| 259 | * @return PHPExcel_Style |
||
| 260 | */ |
||
| 261 | public function applyFromArray($pStyles = null, $pAdvanced = true) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get Fill |
||
| 537 | * |
||
| 538 | * @return PHPExcel_Style_Fill |
||
| 539 | */ |
||
| 540 | public function getFill() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get Font |
||
| 546 | * |
||
| 547 | * @return PHPExcel_Style_Font |
||
| 548 | */ |
||
| 549 | public function getFont() { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Set font |
||
| 555 | * |
||
| 556 | * @param PHPExcel_Style_Font $font |
||
| 557 | * @return PHPExcel_Style |
||
| 558 | */ |
||
| 559 | public function setFont(PHPExcel_Style_Font $font) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get Borders |
||
| 567 | * |
||
| 568 | * @return PHPExcel_Style_Borders |
||
| 569 | */ |
||
| 570 | public function getBorders() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get Alignment |
||
| 576 | * |
||
| 577 | * @return PHPExcel_Style_Alignment |
||
| 578 | */ |
||
| 579 | public function getAlignment() { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get Number Format |
||
| 585 | * |
||
| 586 | * @return PHPExcel_Style_NumberFormat |
||
| 587 | */ |
||
| 588 | public function getNumberFormat() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get Conditional Styles. Only used on supervisor. |
||
| 594 | * |
||
| 595 | * @return PHPExcel_Style_Conditional[] |
||
| 596 | */ |
||
| 597 | public function getConditionalStyles() { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Set Conditional Styles. Only used on supervisor. |
||
| 603 | * |
||
| 604 | * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles |
||
| 605 | * @return PHPExcel_Style |
||
| 606 | */ |
||
| 607 | public function setConditionalStyles($pValue = null) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get Protection |
||
| 616 | * |
||
| 617 | * @return PHPExcel_Style_Protection |
||
| 618 | */ |
||
| 619 | public function getProtection() { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get hash code |
||
| 625 | * |
||
| 626 | * @return string Hash code |
||
| 627 | */ |
||
| 628 | public function getHashCode() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get own index in style collection |
||
| 648 | * |
||
| 649 | * @return int |
||
| 650 | */ |
||
| 651 | public function getIndex() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Set own index in style collection |
||
| 658 | * |
||
| 659 | * @param int $pValue |
||
| 660 | */ |
||
| 661 | public function setIndex($pValue) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
| 668 | */ |
||
| 669 | public function __clone() { |
||
| 679 | } |
||
| 680 |