Complex classes like Alignment 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 Alignment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Alignment extends Supervisor implements IComparable |
||
31 | { |
||
32 | /* Horizontal alignment styles */ |
||
33 | const HORIZONTAL_GENERAL = 'general'; |
||
34 | const HORIZONTAL_LEFT = 'left'; |
||
35 | const HORIZONTAL_RIGHT = 'right'; |
||
36 | const HORIZONTAL_CENTER = 'center'; |
||
37 | const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'; |
||
38 | const HORIZONTAL_JUSTIFY = 'justify'; |
||
39 | const HORIZONTAL_FILL = 'fill'; |
||
40 | const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
||
41 | |||
42 | /* Vertical alignment styles */ |
||
43 | const VERTICAL_BOTTOM = 'bottom'; |
||
44 | const VERTICAL_TOP = 'top'; |
||
45 | const VERTICAL_CENTER = 'center'; |
||
46 | const VERTICAL_JUSTIFY = 'justify'; |
||
47 | const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
||
48 | |||
49 | /* Read order */ |
||
50 | const READORDER_CONTEXT = 0; |
||
51 | const READORDER_LTR = 1; |
||
52 | const READORDER_RTL = 2; |
||
53 | |||
54 | /** |
||
55 | * Horizontal alignment. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $horizontal = self::HORIZONTAL_GENERAL; |
||
60 | |||
61 | /** |
||
62 | * Vertical alignment. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $vertical = self::VERTICAL_BOTTOM; |
||
67 | |||
68 | /** |
||
69 | * Text rotation. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $textRotation = 0; |
||
74 | |||
75 | /** |
||
76 | * Wrap text. |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected $wrapText = false; |
||
81 | |||
82 | /** |
||
83 | * Shrink to fit. |
||
84 | * |
||
85 | * @var bool |
||
86 | */ |
||
87 | protected $shrinkToFit = false; |
||
88 | |||
89 | /** |
||
90 | * Indent - only possible with horizontal alignment left and right. |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | protected $indent = 0; |
||
95 | |||
96 | /** |
||
97 | * Read order. |
||
98 | * |
||
99 | * @var int |
||
100 | */ |
||
101 | protected $readOrder = 0; |
||
102 | |||
103 | /** |
||
104 | * Create a new Alignment. |
||
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 | 80 | public function __construct($isSupervisor = false, $isConditional = false) |
|
114 | { |
||
115 | // Supervisor? |
||
116 | 80 | parent::__construct($isSupervisor); |
|
117 | |||
118 | 80 | if ($isConditional) { |
|
119 | 2 | $this->horizontal = null; |
|
120 | 2 | $this->vertical = null; |
|
121 | 2 | $this->textRotation = null; |
|
122 | } |
||
123 | 80 | } |
|
124 | |||
125 | /** |
||
126 | * Get the shared style component for the currently active cell in currently active sheet. |
||
127 | * Only used for style supervisor. |
||
128 | * |
||
129 | * @return Alignment |
||
130 | */ |
||
131 | public function getSharedComponent() |
||
132 | { |
||
133 | return $this->parent->getSharedComponent()->getAlignment(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Build style array from subcomponents. |
||
138 | * |
||
139 | * @param array $array |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | 19 | public function getStyleArray($array) |
|
144 | { |
||
145 | 19 | return ['alignment' => $array]; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Apply styles from array. |
||
150 | * <code> |
||
151 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray( |
||
152 | * array( |
||
153 | * 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, |
||
154 | * 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, |
||
155 | * 'textRotation' => 0, |
||
156 | * 'wrapText' => TRUE |
||
157 | * ) |
||
158 | * ); |
||
159 | * </code>. |
||
160 | * |
||
161 | * @param array $pStyles Array containing style information |
||
162 | * |
||
163 | * @throws PhpSpreadsheetException |
||
164 | * |
||
165 | * @return Alignment |
||
166 | */ |
||
167 | 21 | public function applyFromArray(array $pStyles) |
|
198 | |||
199 | /** |
||
200 | * Get Horizontal. |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | 62 | public function getHorizontal() |
|
212 | |||
213 | /** |
||
214 | * Set Horizontal. |
||
215 | * |
||
216 | * @param string $pValue see self::HORIZONTAL_* |
||
217 | * |
||
218 | * @return Alignment |
||
219 | */ |
||
220 | 23 | public function setHorizontal($pValue) |
|
235 | |||
236 | /** |
||
237 | * Get Vertical. |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | 62 | public function getVertical() |
|
249 | |||
250 | /** |
||
251 | * Set Vertical. |
||
252 | * |
||
253 | * @param string $pValue see self::VERTICAL_* |
||
254 | * |
||
255 | * @return Alignment |
||
256 | */ |
||
257 | 23 | public function setVertical($pValue) |
|
272 | |||
273 | /** |
||
274 | * Get TextRotation. |
||
275 | * |
||
276 | * @return int |
||
277 | */ |
||
278 | 62 | public function getTextRotation() |
|
286 | |||
287 | /** |
||
288 | * Set TextRotation. |
||
289 | * |
||
290 | * @param int $pValue |
||
291 | * |
||
292 | * @throws PhpSpreadsheetException |
||
293 | * |
||
294 | * @return Alignment |
||
295 | */ |
||
296 | 12 | public function setTextRotation($pValue) |
|
317 | |||
318 | /** |
||
319 | * Get Wrap Text. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 58 | public function getWrapText() |
|
331 | |||
332 | /** |
||
333 | * Set Wrap Text. |
||
334 | * |
||
335 | * @param bool $pValue |
||
336 | * |
||
337 | * @return Alignment |
||
338 | */ |
||
339 | 30 | public function setWrapText($pValue) |
|
353 | |||
354 | /** |
||
355 | * Get Shrink to fit. |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | 58 | public function getShrinkToFit() |
|
367 | |||
368 | /** |
||
369 | * Set Shrink to fit. |
||
370 | * |
||
371 | * @param bool $pValue |
||
372 | * |
||
373 | * @return Alignment |
||
374 | */ |
||
375 | 22 | public function setShrinkToFit($pValue) |
|
389 | |||
390 | /** |
||
391 | * Get indent. |
||
392 | * |
||
393 | * @return int |
||
394 | */ |
||
395 | 62 | public function getIndent() |
|
403 | |||
404 | /** |
||
405 | * Set indent. |
||
406 | * |
||
407 | * @param int $pValue |
||
408 | * |
||
409 | * @return Alignment |
||
410 | */ |
||
411 | 13 | public function setIndent($pValue) |
|
429 | |||
430 | /** |
||
431 | * Get read order. |
||
432 | * |
||
433 | * @return int |
||
434 | */ |
||
435 | 56 | public function getReadOrder() |
|
436 | { |
||
437 | 56 | if ($this->isSupervisor) { |
|
438 | return $this->getSharedComponent()->getReadOrder(); |
||
439 | } |
||
440 | |||
441 | 56 | return $this->readOrder; |
|
442 | } |
||
443 | |||
444 | /** |
||
445 | * Set read order. |
||
446 | * |
||
447 | * @param int $pValue |
||
448 | * |
||
449 | * @return Alignment |
||
450 | */ |
||
451 | 8 | public function setReadOrder($pValue) |
|
452 | { |
||
453 | 8 | if ($pValue < 0 || $pValue > 2) { |
|
454 | $pValue = 0; |
||
455 | } |
||
456 | 8 | if ($this->isSupervisor) { |
|
457 | $styleArray = $this->getStyleArray(['readOrder' => $pValue]); |
||
458 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
459 | } else { |
||
460 | 8 | $this->readOrder = $pValue; |
|
461 | } |
||
462 | |||
463 | 8 | return $this; |
|
464 | } |
||
465 | |||
466 | /** |
||
467 | * Get hash code. |
||
468 | * |
||
469 | * @return string Hash code |
||
470 | */ |
||
471 | 70 | public function getHashCode() |
|
488 | } |
||
489 |