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 |
||
8 | class Alignment extends Supervisor implements IComparable |
||
9 | { |
||
10 | /* Horizontal alignment styles */ |
||
11 | const HORIZONTAL_GENERAL = 'general'; |
||
12 | const HORIZONTAL_LEFT = 'left'; |
||
13 | const HORIZONTAL_RIGHT = 'right'; |
||
14 | const HORIZONTAL_CENTER = 'center'; |
||
15 | const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'; |
||
16 | const HORIZONTAL_JUSTIFY = 'justify'; |
||
17 | const HORIZONTAL_FILL = 'fill'; |
||
18 | const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
||
19 | |||
20 | /* Vertical alignment styles */ |
||
21 | const VERTICAL_BOTTOM = 'bottom'; |
||
22 | const VERTICAL_TOP = 'top'; |
||
23 | const VERTICAL_CENTER = 'center'; |
||
24 | const VERTICAL_JUSTIFY = 'justify'; |
||
25 | const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only |
||
26 | |||
27 | /* Read order */ |
||
28 | const READORDER_CONTEXT = 0; |
||
29 | const READORDER_LTR = 1; |
||
30 | const READORDER_RTL = 2; |
||
31 | |||
32 | /** |
||
33 | * Horizontal alignment. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $horizontal = self::HORIZONTAL_GENERAL; |
||
38 | |||
39 | /** |
||
40 | * Vertical alignment. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $vertical = self::VERTICAL_BOTTOM; |
||
45 | |||
46 | /** |
||
47 | * Text rotation. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $textRotation = 0; |
||
52 | |||
53 | /** |
||
54 | * Wrap text. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $wrapText = false; |
||
59 | |||
60 | /** |
||
61 | * Shrink to fit. |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $shrinkToFit = false; |
||
66 | |||
67 | /** |
||
68 | * Indent - only possible with horizontal alignment left and right. |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | protected $indent = 0; |
||
73 | |||
74 | /** |
||
75 | * Read order. |
||
76 | * |
||
77 | * @var int |
||
78 | */ |
||
79 | protected $readOrder = 0; |
||
80 | |||
81 | /** |
||
82 | * Create a new Alignment. |
||
83 | * |
||
84 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
85 | * Leave this value at default unless you understand exactly what |
||
86 | * its ramifications are |
||
87 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
88 | * Leave this value at default unless you understand exactly what |
||
89 | * its ramifications are |
||
90 | */ |
||
91 | 28 | public function __construct($isSupervisor = false, $isConditional = false) |
|
92 | { |
||
93 | // Supervisor? |
||
94 | 28 | parent::__construct($isSupervisor); |
|
95 | |||
96 | 28 | if ($isConditional) { |
|
97 | $this->horizontal = null; |
||
98 | $this->vertical = null; |
||
99 | $this->textRotation = null; |
||
100 | } |
||
101 | 28 | } |
|
102 | |||
103 | /** |
||
104 | * Get the shared style component for the currently active cell in currently active sheet. |
||
105 | * Only used for style supervisor. |
||
106 | * |
||
107 | * @return Alignment |
||
108 | */ |
||
109 | public function getSharedComponent() |
||
113 | |||
114 | /** |
||
115 | * Build style array from subcomponents. |
||
116 | * |
||
117 | * @param array $array |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 1 | public function getStyleArray($array) |
|
125 | |||
126 | /** |
||
127 | * Apply styles from array. |
||
128 | * <code> |
||
129 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray( |
||
130 | * array( |
||
131 | * 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, |
||
132 | * 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, |
||
133 | * 'textRotation' => 0, |
||
134 | * 'wrapText' => TRUE |
||
135 | * ) |
||
136 | * ); |
||
137 | * </code>. |
||
138 | * |
||
139 | * @param array $pStyles Array containing style information |
||
140 | * |
||
141 | * @throws PhpSpreadsheetException |
||
142 | * |
||
143 | * @return Alignment |
||
144 | */ |
||
145 | 2 | public function applyFromArray(array $pStyles) |
|
146 | { |
||
147 | 2 | if ($this->isSupervisor) { |
|
148 | $this->getActiveSheet()->getStyle($this->getSelectedCells()) |
||
149 | ->applyFromArray($this->getStyleArray($pStyles)); |
||
150 | } else { |
||
151 | 2 | if (isset($pStyles['horizontal'])) { |
|
152 | 2 | $this->setHorizontal($pStyles['horizontal']); |
|
153 | } |
||
154 | 2 | if (isset($pStyles['vertical'])) { |
|
155 | 2 | $this->setVertical($pStyles['vertical']); |
|
156 | } |
||
157 | 2 | if (isset($pStyles['textRotation'])) { |
|
158 | $this->setTextRotation($pStyles['textRotation']); |
||
159 | } |
||
160 | 2 | if (isset($pStyles['wrapText'])) { |
|
161 | 2 | $this->setWrapText($pStyles['wrapText']); |
|
162 | } |
||
163 | 2 | if (isset($pStyles['shrinkToFit'])) { |
|
164 | 1 | $this->setShrinkToFit($pStyles['shrinkToFit']); |
|
165 | } |
||
166 | 2 | if (isset($pStyles['indent'])) { |
|
167 | $this->setIndent($pStyles['indent']); |
||
168 | } |
||
169 | 2 | if (isset($pStyles['readOrder'])) { |
|
170 | $this->setReadOrder($pStyles['readOrder']); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | 2 | return $this; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get Horizontal. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 2 | public function getHorizontal() |
|
190 | |||
191 | /** |
||
192 | * Set Horizontal. |
||
193 | * |
||
194 | * @param string $pValue see self::HORIZONTAL_* |
||
195 | * |
||
196 | * @return Alignment |
||
197 | */ |
||
198 | 3 | public function setHorizontal($pValue) |
|
213 | |||
214 | /** |
||
215 | * Get Vertical. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 1 | public function getVertical() |
|
227 | |||
228 | /** |
||
229 | * Set Vertical. |
||
230 | * |
||
231 | * @param string $pValue see self::VERTICAL_* |
||
232 | * |
||
233 | * @return Alignment |
||
234 | */ |
||
235 | 3 | public function setVertical($pValue) |
|
236 | { |
||
237 | 3 | if ($pValue == '') { |
|
238 | $pValue = self::VERTICAL_BOTTOM; |
||
239 | } |
||
240 | |||
241 | 3 | if ($this->isSupervisor) { |
|
242 | 1 | $styleArray = $this->getStyleArray(['vertical' => $pValue]); |
|
243 | 1 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
244 | } else { |
||
245 | 3 | $this->vertical = $pValue; |
|
246 | } |
||
247 | |||
248 | 3 | return $this; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get TextRotation. |
||
253 | * |
||
254 | * @return int |
||
255 | */ |
||
256 | 1 | public function getTextRotation() |
|
264 | |||
265 | /** |
||
266 | * Set TextRotation. |
||
267 | * |
||
268 | * @param int $pValue |
||
269 | * |
||
270 | * @throws PhpSpreadsheetException |
||
271 | * |
||
272 | * @return Alignment |
||
273 | */ |
||
274 | 1 | public function setTextRotation($pValue) |
|
295 | |||
296 | /** |
||
297 | * Get Wrap Text. |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function getWrapText() |
||
302 | { |
||
303 | if ($this->isSupervisor) { |
||
304 | return $this->getSharedComponent()->getWrapText(); |
||
305 | } |
||
306 | |||
307 | return $this->wrapText; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set Wrap Text. |
||
312 | * |
||
313 | * @param bool $pValue |
||
314 | * |
||
315 | * @return Alignment |
||
316 | */ |
||
317 | 3 | public function setWrapText($pValue) |
|
331 | |||
332 | /** |
||
333 | * Get Shrink to fit. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | public function getShrinkToFit() |
||
338 | { |
||
339 | if ($this->isSupervisor) { |
||
340 | return $this->getSharedComponent()->getShrinkToFit(); |
||
341 | } |
||
342 | |||
343 | return $this->shrinkToFit; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Set Shrink to fit. |
||
348 | * |
||
349 | * @param bool $pValue |
||
350 | * |
||
351 | * @return Alignment |
||
352 | */ |
||
353 | 2 | public function setShrinkToFit($pValue) |
|
367 | |||
368 | /** |
||
369 | * Get indent. |
||
370 | * |
||
371 | * @return int |
||
372 | */ |
||
373 | 1 | public function getIndent() |
|
381 | |||
382 | /** |
||
383 | * Set indent. |
||
384 | * |
||
385 | * @param int $pValue |
||
386 | * |
||
387 | * @return Alignment |
||
388 | */ |
||
389 | 1 | public function setIndent($pValue) |
|
407 | |||
408 | /** |
||
409 | * Get read order. |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | public function getReadOrder() |
||
414 | { |
||
415 | if ($this->isSupervisor) { |
||
416 | return $this->getSharedComponent()->getReadOrder(); |
||
417 | } |
||
418 | |||
419 | return $this->readOrder; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Set read order. |
||
424 | * |
||
425 | * @param int $pValue |
||
426 | * |
||
427 | * @return Alignment |
||
428 | */ |
||
429 | 1 | public function setReadOrder($pValue) |
|
443 | |||
444 | /** |
||
445 | * Get hash code. |
||
446 | * |
||
447 | * @return string Hash code |
||
448 | */ |
||
449 | 10 | public function getHashCode() |
|
466 | } |
||
467 |