Total Complexity | 69 |
Total Lines | 547 |
Duplicated Lines | 0 % |
Coverage | 87.88% |
Changes | 0 |
Complex classes like Font 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.
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 Font, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Font extends Supervisor |
||
8 | { |
||
9 | // Underline types |
||
10 | const UNDERLINE_NONE = 'none'; |
||
11 | const UNDERLINE_DOUBLE = 'double'; |
||
12 | const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting'; |
||
13 | const UNDERLINE_SINGLE = 'single'; |
||
14 | const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting'; |
||
15 | |||
16 | /** |
||
17 | * Font Name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $name = 'Calibri'; |
||
22 | |||
23 | /** |
||
24 | * Font Size. |
||
25 | * |
||
26 | * @var float |
||
27 | */ |
||
28 | protected $size = 11; |
||
29 | |||
30 | /** |
||
31 | * Bold. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $bold = false; |
||
36 | |||
37 | /** |
||
38 | * Italic. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $italic = false; |
||
43 | |||
44 | /** |
||
45 | * Superscript. |
||
46 | * |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected $superscript = false; |
||
50 | |||
51 | /** |
||
52 | * Subscript. |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $subscript = false; |
||
57 | |||
58 | /** |
||
59 | * Underline. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $underline = self::UNDERLINE_NONE; |
||
64 | |||
65 | /** |
||
66 | * Strikethrough. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $strikethrough = false; |
||
71 | |||
72 | /** |
||
73 | * Foreground color. |
||
74 | * |
||
75 | * @var Color |
||
76 | */ |
||
77 | protected $color; |
||
78 | |||
79 | /** |
||
80 | * @var int |
||
81 | */ |
||
82 | public $colorIndex; |
||
83 | |||
84 | /** |
||
85 | * Create a new Font. |
||
86 | * |
||
87 | * @param bool $isSupervisor Flag indicating if this is a supervisor or not |
||
88 | * Leave this value at default unless you understand exactly what |
||
89 | * its ramifications are |
||
90 | * @param bool $isConditional Flag indicating if this is a conditional style or not |
||
91 | * Leave this value at default unless you understand exactly what |
||
92 | * its ramifications are |
||
93 | */ |
||
94 | 201 | public function __construct($isSupervisor = false, $isConditional = false) |
|
95 | { |
||
96 | // Supervisor? |
||
97 | 201 | parent::__construct($isSupervisor); |
|
98 | |||
99 | // Initialise values |
||
100 | 201 | if ($isConditional) { |
|
101 | 3 | $this->name = null; |
|
102 | 3 | $this->size = null; |
|
103 | 3 | $this->bold = null; |
|
104 | 3 | $this->italic = null; |
|
105 | 3 | $this->superscript = null; |
|
106 | 3 | $this->subscript = null; |
|
107 | 3 | $this->underline = null; |
|
108 | 3 | $this->strikethrough = null; |
|
109 | 3 | $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
|
110 | } else { |
||
111 | 201 | $this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
|
112 | } |
||
113 | // bind parent if we are a supervisor |
||
114 | 201 | if ($isSupervisor) { |
|
115 | 197 | $this->color->bindParent($this, 'color'); |
|
|
|||
116 | } |
||
117 | 201 | } |
|
118 | |||
119 | /** |
||
120 | * Get the shared style component for the currently active cell in currently active sheet. |
||
121 | * Only used for style supervisor. |
||
122 | * |
||
123 | * @return Font |
||
124 | */ |
||
125 | 1 | public function getSharedComponent() |
|
126 | { |
||
127 | 1 | return $this->parent->getSharedComponent()->getFont(); |
|
1 ignored issue
–
show
|
|||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Build style array from subcomponents. |
||
132 | * |
||
133 | * @param array $array |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 18 | public function getStyleArray($array) |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Apply styles from array. |
||
144 | * |
||
145 | * <code> |
||
146 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( |
||
147 | * [ |
||
148 | * 'name' => 'Arial', |
||
149 | * 'bold' => TRUE, |
||
150 | * 'italic' => FALSE, |
||
151 | * 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, |
||
152 | * 'strikethrough' => FALSE, |
||
153 | * 'color' => [ |
||
154 | * 'rgb' => '808080' |
||
155 | * ] |
||
156 | * ] |
||
157 | * ); |
||
158 | * </code> |
||
159 | * |
||
160 | * @param array $pStyles Array containing style information |
||
161 | * |
||
162 | * @throws PhpSpreadsheetException |
||
163 | * |
||
164 | * @return Font |
||
165 | */ |
||
166 | 24 | public function applyFromArray(array $pStyles) |
|
167 | { |
||
168 | 24 | if ($this->isSupervisor) { |
|
169 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
||
170 | } else { |
||
171 | 24 | if (isset($pStyles['name'])) { |
|
172 | 15 | $this->setName($pStyles['name']); |
|
173 | } |
||
174 | 24 | if (isset($pStyles['bold'])) { |
|
175 | 22 | $this->setBold($pStyles['bold']); |
|
176 | } |
||
177 | 24 | if (isset($pStyles['italic'])) { |
|
178 | 5 | $this->setItalic($pStyles['italic']); |
|
179 | } |
||
180 | 24 | if (isset($pStyles['superscript'])) { |
|
181 | 1 | $this->setSuperscript($pStyles['superscript']); |
|
182 | } |
||
183 | 24 | if (isset($pStyles['subscript'])) { |
|
184 | 1 | $this->setSubscript($pStyles['subscript']); |
|
185 | } |
||
186 | 24 | if (isset($pStyles['underline'])) { |
|
187 | 15 | $this->setUnderline($pStyles['underline']); |
|
188 | } |
||
189 | 24 | if (isset($pStyles['strikethrough'])) { |
|
190 | 1 | $this->setStrikethrough($pStyles['strikethrough']); |
|
191 | } |
||
192 | 24 | if (isset($pStyles['color'])) { |
|
193 | 17 | $this->getColor()->applyFromArray($pStyles['color']); |
|
194 | } |
||
195 | 24 | if (isset($pStyles['size'])) { |
|
196 | 15 | $this->setSize($pStyles['size']); |
|
197 | } |
||
198 | } |
||
199 | |||
200 | 24 | return $this; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Get Name. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 107 | public function getName() |
|
209 | { |
||
210 | 107 | if ($this->isSupervisor) { |
|
211 | return $this->getSharedComponent()->getName(); |
||
212 | } |
||
213 | |||
214 | 107 | return $this->name; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Set Name. |
||
219 | * |
||
220 | * @param string $pValue |
||
221 | * |
||
222 | * @return Font |
||
223 | */ |
||
224 | 75 | public function setName($pValue) |
|
225 | { |
||
226 | 75 | if ($pValue == '') { |
|
227 | 1 | $pValue = 'Calibri'; |
|
228 | } |
||
229 | 75 | if ($this->isSupervisor) { |
|
230 | 12 | $styleArray = $this->getStyleArray(['name' => $pValue]); |
|
231 | 12 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
232 | } else { |
||
233 | 75 | $this->name = $pValue; |
|
234 | } |
||
235 | |||
236 | 75 | return $this; |
|
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get Size. |
||
241 | * |
||
242 | * @return float |
||
243 | */ |
||
244 | 107 | public function getSize() |
|
245 | { |
||
246 | 107 | if ($this->isSupervisor) { |
|
247 | return $this->getSharedComponent()->getSize(); |
||
248 | } |
||
249 | |||
250 | 107 | return $this->size; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Set Size. |
||
255 | * |
||
256 | * @param float $pValue |
||
257 | * |
||
258 | * @return Font |
||
259 | */ |
||
260 | 77 | public function setSize($pValue) |
|
261 | { |
||
262 | 77 | if ($pValue == '') { |
|
263 | 1 | $pValue = 10; |
|
264 | } |
||
265 | 77 | if ($this->isSupervisor) { |
|
266 | 12 | $styleArray = $this->getStyleArray(['size' => $pValue]); |
|
267 | 12 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
268 | } else { |
||
269 | 77 | $this->size = $pValue; |
|
270 | } |
||
271 | |||
272 | 77 | return $this; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get Bold. |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 107 | public function getBold() |
|
281 | { |
||
282 | 107 | if ($this->isSupervisor) { |
|
283 | return $this->getSharedComponent()->getBold(); |
||
284 | } |
||
285 | |||
286 | 107 | return $this->bold; |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Set Bold. |
||
291 | * |
||
292 | * @param bool $pValue |
||
293 | * |
||
294 | * @return Font |
||
295 | */ |
||
296 | 73 | public function setBold($pValue) |
|
297 | { |
||
298 | 73 | if ($pValue == '') { |
|
299 | 33 | $pValue = false; |
|
300 | } |
||
301 | 73 | if ($this->isSupervisor) { |
|
302 | 18 | $styleArray = $this->getStyleArray(['bold' => $pValue]); |
|
303 | 18 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
304 | } else { |
||
305 | 73 | $this->bold = $pValue; |
|
306 | } |
||
307 | |||
308 | 73 | return $this; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get Italic. |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | 107 | public function getItalic() |
|
317 | { |
||
318 | 107 | if ($this->isSupervisor) { |
|
319 | return $this->getSharedComponent()->getItalic(); |
||
320 | } |
||
321 | |||
322 | 107 | return $this->italic; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * Set Italic. |
||
327 | * |
||
328 | * @param bool $pValue |
||
329 | * |
||
330 | * @return Font |
||
331 | */ |
||
332 | 55 | public function setItalic($pValue) |
|
333 | { |
||
334 | 55 | if ($pValue == '') { |
|
335 | 33 | $pValue = false; |
|
336 | } |
||
337 | 55 | if ($this->isSupervisor) { |
|
338 | 1 | $styleArray = $this->getStyleArray(['italic' => $pValue]); |
|
339 | 1 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
340 | } else { |
||
341 | 55 | $this->italic = $pValue; |
|
342 | } |
||
343 | |||
344 | 55 | return $this; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Get Superscript. |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 100 | public function getSuperscript() |
|
353 | { |
||
354 | 100 | if ($this->isSupervisor) { |
|
355 | return $this->getSharedComponent()->getSuperscript(); |
||
356 | } |
||
357 | |||
358 | 100 | return $this->superscript; |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * Set Superscript. |
||
363 | * |
||
364 | * @param bool $pValue |
||
365 | * |
||
366 | * @return Font |
||
367 | */ |
||
368 | 4 | public function setSuperscript($pValue) |
|
369 | { |
||
370 | 4 | if ($pValue == '') { |
|
371 | $pValue = false; |
||
372 | } |
||
373 | 4 | if ($this->isSupervisor) { |
|
374 | $styleArray = $this->getStyleArray(['superscript' => $pValue]); |
||
375 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
376 | } else { |
||
377 | 4 | $this->superscript = $pValue; |
|
378 | 4 | $this->subscript = !$pValue; |
|
379 | } |
||
380 | |||
381 | 4 | return $this; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * Get Subscript. |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | 100 | public function getSubscript() |
|
390 | { |
||
391 | 100 | if ($this->isSupervisor) { |
|
392 | return $this->getSharedComponent()->getSubscript(); |
||
393 | } |
||
394 | |||
395 | 100 | return $this->subscript; |
|
396 | } |
||
397 | |||
398 | /** |
||
399 | * Set Subscript. |
||
400 | * |
||
401 | * @param bool $pValue |
||
402 | * |
||
403 | * @return Font |
||
404 | */ |
||
405 | 4 | public function setSubscript($pValue) |
|
406 | { |
||
407 | 4 | if ($pValue == '') { |
|
408 | $pValue = false; |
||
409 | } |
||
410 | 4 | if ($this->isSupervisor) { |
|
411 | $styleArray = $this->getStyleArray(['subscript' => $pValue]); |
||
412 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
413 | } else { |
||
414 | 4 | $this->subscript = $pValue; |
|
415 | 4 | $this->superscript = !$pValue; |
|
416 | } |
||
417 | |||
418 | 4 | return $this; |
|
419 | } |
||
420 | |||
421 | /** |
||
422 | * Get Underline. |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | 107 | public function getUnderline() |
|
427 | { |
||
428 | 107 | if ($this->isSupervisor) { |
|
429 | return $this->getSharedComponent()->getUnderline(); |
||
430 | } |
||
431 | |||
432 | 107 | return $this->underline; |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Set Underline. |
||
437 | * |
||
438 | * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type |
||
439 | * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, |
||
440 | * false equates to UNDERLINE_NONE |
||
441 | * |
||
442 | * @return Font |
||
443 | */ |
||
444 | 47 | public function setUnderline($pValue) |
|
445 | { |
||
446 | 47 | if (is_bool($pValue)) { |
|
447 | $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; |
||
448 | 47 | } elseif ($pValue == '') { |
|
449 | $pValue = self::UNDERLINE_NONE; |
||
450 | } |
||
451 | 47 | if ($this->isSupervisor) { |
|
452 | 12 | $styleArray = $this->getStyleArray(['underline' => $pValue]); |
|
453 | 12 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
454 | } else { |
||
455 | 47 | $this->underline = $pValue; |
|
456 | } |
||
457 | |||
458 | 47 | return $this; |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * Get Strikethrough. |
||
463 | * |
||
464 | * @return bool |
||
465 | */ |
||
466 | 100 | public function getStrikethrough() |
|
467 | { |
||
468 | 100 | if ($this->isSupervisor) { |
|
469 | return $this->getSharedComponent()->getStrikethrough(); |
||
470 | } |
||
471 | |||
472 | 100 | return $this->strikethrough; |
|
473 | } |
||
474 | |||
475 | /** |
||
476 | * Set Strikethrough. |
||
477 | * |
||
478 | * @param bool $pValue |
||
479 | * |
||
480 | * @return Font |
||
481 | */ |
||
482 | 32 | public function setStrikethrough($pValue) |
|
483 | { |
||
484 | 32 | if ($pValue == '') { |
|
485 | 31 | $pValue = false; |
|
486 | } |
||
487 | |||
488 | 32 | if ($this->isSupervisor) { |
|
489 | $styleArray = $this->getStyleArray(['strikethrough' => $pValue]); |
||
490 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
491 | } else { |
||
492 | 32 | $this->strikethrough = $pValue; |
|
493 | } |
||
494 | |||
495 | 32 | return $this; |
|
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get Color. |
||
500 | * |
||
501 | * @return Color |
||
502 | */ |
||
503 | 128 | public function getColor() |
|
506 | } |
||
507 | |||
508 | /** |
||
509 | * Set Color. |
||
510 | * |
||
511 | * @param Color $pValue |
||
512 | * |
||
513 | * @throws PhpSpreadsheetException |
||
514 | * |
||
515 | * @return Font |
||
516 | */ |
||
517 | 15 | public function setColor(Color $pValue) |
|
518 | { |
||
519 | // make sure parameter is a real color and not a supervisor |
||
520 | 15 | $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; |
|
521 | |||
522 | 15 | if ($this->isSupervisor) { |
|
523 | 1 | $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
|
524 | 1 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
525 | } else { |
||
526 | 14 | $this->color = $color; |
|
527 | } |
||
528 | |||
529 | 15 | return $this; |
|
530 | } |
||
531 | |||
532 | /** |
||
533 | * Get hash code. |
||
534 | * |
||
535 | * @return string Hash code |
||
536 | */ |
||
537 | 115 | public function getHashCode() |
|
554 | ); |
||
555 | } |
||
556 | } |
||
557 |