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 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 Style, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Style extends Supervisor implements IComparable |
||
10 | { |
||
11 | /** |
||
12 | * Font. |
||
13 | * |
||
14 | * @var Font |
||
15 | */ |
||
16 | protected $font; |
||
17 | |||
18 | /** |
||
19 | * Fill. |
||
20 | * |
||
21 | * @var Fill |
||
22 | */ |
||
23 | protected $fill; |
||
24 | |||
25 | /** |
||
26 | * Borders. |
||
27 | * |
||
28 | * @var Borders |
||
29 | */ |
||
30 | protected $borders; |
||
31 | |||
32 | /** |
||
33 | * Alignment. |
||
34 | * |
||
35 | * @var Alignment |
||
36 | */ |
||
37 | protected $alignment; |
||
38 | |||
39 | /** |
||
40 | * Number Format. |
||
41 | * |
||
42 | * @var NumberFormat |
||
43 | */ |
||
44 | protected $numberFormat; |
||
45 | |||
46 | /** |
||
47 | * Conditional styles. |
||
48 | * |
||
49 | * @var Conditional[] |
||
50 | */ |
||
51 | protected $conditionalStyles; |
||
52 | |||
53 | /** |
||
54 | * Protection. |
||
55 | * |
||
56 | * @var Protection |
||
57 | */ |
||
58 | protected $protection; |
||
59 | |||
60 | /** |
||
61 | * Index of style in collection. Only used for real style. |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $index; |
||
66 | |||
67 | /** |
||
68 | * Use Quote Prefix when displaying in cell editor. Only used for real style. |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $quotePrefix = false; |
||
73 | |||
74 | /** |
||
75 | * Create a new Style. |
||
76 | * |
||
77 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
78 | * Leave this value at default unless you understand exactly what |
||
79 | * its ramifications are |
||
80 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
81 | * Leave this value at default unless you understand exactly what |
||
82 | * its ramifications are |
||
83 | */ |
||
84 | 134 | public function __construct($isSupervisor = false, $isConditional = false) |
|
108 | |||
109 | /** |
||
110 | * Get the shared style component for the currently active cell in currently active sheet. |
||
111 | * Only used for style supervisor. |
||
112 | * |
||
113 | * @return Style |
||
114 | */ |
||
115 | 7 | public function getSharedComponent() |
|
128 | |||
129 | /** |
||
130 | * Get parent. Only used for style supervisor. |
||
131 | * |
||
132 | * @return Spreadsheet |
||
133 | */ |
||
134 | public function getParent() |
||
138 | |||
139 | /** |
||
140 | * Build style array from subcomponents. |
||
141 | * |
||
142 | * @param array $array |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getStyleArray($array) |
||
150 | |||
151 | /** |
||
152 | * Apply styles from array. |
||
153 | * |
||
154 | * <code> |
||
155 | * $spreadsheet->getActiveSheet()->getStyle('B2')->applyFromArray( |
||
156 | * array( |
||
157 | * 'font' => array( |
||
158 | * 'name' => 'Arial', |
||
159 | * 'bold' => true, |
||
160 | * 'italic' => false, |
||
161 | * 'underline' => Font::UNDERLINE_DOUBLE, |
||
162 | * 'strikethrough' => false, |
||
163 | * 'color' => array( |
||
164 | * 'rgb' => '808080' |
||
165 | * ) |
||
166 | * ), |
||
167 | * 'borders' => array( |
||
168 | * 'bottom' => array( |
||
169 | * 'borderStyle' => Border::BORDER_DASHDOT, |
||
170 | * 'color' => array( |
||
171 | * 'rgb' => '808080' |
||
172 | * ) |
||
173 | * ), |
||
174 | * 'top' => array( |
||
175 | * 'borderStyle' => Border::BORDER_DASHDOT, |
||
176 | * 'color' => array( |
||
177 | * 'rgb' => '808080' |
||
178 | * ) |
||
179 | * ) |
||
180 | * ), |
||
181 | * 'quotePrefix' => true |
||
182 | * ) |
||
183 | * ); |
||
184 | * </code> |
||
185 | * |
||
186 | * @param array $pStyles Array containing style information |
||
187 | * @param bool $pAdvanced advanced mode for setting borders |
||
188 | * |
||
189 | * @throws Exception |
||
190 | * |
||
191 | * @return Style |
||
192 | */ |
||
193 | 47 | public function applyFromArray(array $pStyles, $pAdvanced = true) |
|
458 | |||
459 | /** |
||
460 | * Get Fill. |
||
461 | * |
||
462 | * @return Fill |
||
463 | */ |
||
464 | 83 | public function getFill() |
|
468 | |||
469 | /** |
||
470 | * Get Font. |
||
471 | * |
||
472 | * @return Font |
||
473 | */ |
||
474 | 75 | public function getFont() |
|
478 | |||
479 | /** |
||
480 | * Set font. |
||
481 | * |
||
482 | * @param Font $font |
||
483 | * |
||
484 | * @return Style |
||
485 | */ |
||
486 | 17 | public function setFont(Font $font) |
|
492 | |||
493 | /** |
||
494 | * Get Borders. |
||
495 | * |
||
496 | * @return Borders |
||
497 | */ |
||
498 | 81 | public function getBorders() |
|
502 | |||
503 | /** |
||
504 | * Get Alignment. |
||
505 | * |
||
506 | * @return Alignment |
||
507 | */ |
||
508 | 81 | public function getAlignment() |
|
512 | |||
513 | /** |
||
514 | * Get Number Format. |
||
515 | * |
||
516 | * @return NumberFormat |
||
517 | */ |
||
518 | 106 | public function getNumberFormat() |
|
522 | |||
523 | /** |
||
524 | * Get Conditional Styles. Only used on supervisor. |
||
525 | * |
||
526 | * @return Conditional[] |
||
527 | */ |
||
528 | 2 | public function getConditionalStyles() |
|
532 | |||
533 | /** |
||
534 | * Set Conditional Styles. Only used on supervisor. |
||
535 | * |
||
536 | * @param Conditional[] $pValue Array of conditional styles |
||
537 | * |
||
538 | * @return Style |
||
539 | */ |
||
540 | 2 | public function setConditionalStyles(array $pValue) |
|
546 | |||
547 | /** |
||
548 | * Get Protection. |
||
549 | * |
||
550 | * @return Protection |
||
551 | */ |
||
552 | 75 | public function getProtection() |
|
556 | |||
557 | /** |
||
558 | * Get quote prefix. |
||
559 | * |
||
560 | * @return bool |
||
561 | */ |
||
562 | 56 | public function getQuotePrefix() |
|
570 | |||
571 | /** |
||
572 | * Set quote prefix. |
||
573 | * |
||
574 | * @param bool $pValue |
||
575 | */ |
||
576 | 13 | public function setQuotePrefix($pValue) |
|
590 | |||
591 | /** |
||
592 | * Get hash code. |
||
593 | * |
||
594 | * @return string Hash code |
||
595 | */ |
||
596 | 76 | public function getHashCode() |
|
615 | |||
616 | /** |
||
617 | * Get own index in style collection. |
||
618 | * |
||
619 | * @return int |
||
620 | */ |
||
621 | 49 | public function getIndex() |
|
625 | |||
626 | /** |
||
627 | * Set own index in style collection. |
||
628 | * |
||
629 | * @param int $pValue |
||
630 | */ |
||
631 | 134 | public function setIndex($pValue) |
|
635 | } |
||
636 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.