Total Complexity | 69 |
Total Lines | 545 |
Duplicated Lines | 23.85 % |
Coverage | 85.45% |
Changes | 0 |
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 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 | 137 | public function __construct($isSupervisor = false, $isConditional = false) |
|
95 | { |
||
96 | // Supervisor? |
||
97 | 137 | parent::__construct($isSupervisor); |
|
98 | |||
99 | // Initialise values |
||
100 | 137 | if ($isConditional) { |
|
101 | 2 | $this->name = null; |
|
102 | 2 | $this->size = null; |
|
103 | 2 | $this->bold = null; |
|
104 | 2 | $this->italic = null; |
|
105 | 2 | $this->superscript = null; |
|
106 | 2 | $this->subscript = null; |
|
107 | 2 | $this->underline = null; |
|
108 | 2 | $this->strikethrough = null; |
|
109 | 2 | $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
|
110 | } else { |
||
111 | 137 | $this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
|
112 | } |
||
113 | // bind parent if we are a supervisor |
||
114 | 137 | if ($isSupervisor) { |
|
115 | 137 | $this->color->bindParent($this, 'color'); |
|
|
|||
116 | } |
||
117 | 137 | } |
|
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 | public function getSharedComponent() |
||
126 | { |
||
127 | return $this->parent->getSharedComponent()->getFont(); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Build style array from subcomponents. |
||
132 | * |
||
133 | * @param array $array |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 19 | public function getStyleArray($array) |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Apply styles from array. |
||
144 | * <code> |
||
145 | * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( |
||
146 | * array( |
||
147 | * 'name' => 'Arial', |
||
148 | * 'bold' => TRUE, |
||
149 | * 'italic' => FALSE, |
||
150 | * 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, |
||
151 | * 'strikethrough' => FALSE, |
||
152 | * 'color' => array( |
||
153 | * 'rgb' => '808080' |
||
154 | * ) |
||
155 | * ) |
||
156 | * ); |
||
157 | * </code>. |
||
158 | * |
||
159 | * @param array $pStyles Array containing style information |
||
160 | * |
||
161 | * @throws PhpSpreadsheetException |
||
162 | * |
||
163 | * @return Font |
||
164 | */ |
||
165 | 24 | public function applyFromArray(array $pStyles) |
|
166 | { |
||
167 | 24 | if ($this->isSupervisor) { |
|
168 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
||
169 | } else { |
||
170 | 24 | if (isset($pStyles['name'])) { |
|
171 | 16 | $this->setName($pStyles['name']); |
|
172 | } |
||
173 | 24 | if (isset($pStyles['bold'])) { |
|
174 | 23 | $this->setBold($pStyles['bold']); |
|
175 | } |
||
176 | 24 | if (isset($pStyles['italic'])) { |
|
177 | 5 | $this->setItalic($pStyles['italic']); |
|
178 | } |
||
179 | 24 | if (isset($pStyles['superscript'])) { |
|
180 | 1 | $this->setSuperscript($pStyles['superscript']); |
|
181 | } |
||
182 | 24 | if (isset($pStyles['subscript'])) { |
|
183 | 1 | $this->setSubscript($pStyles['subscript']); |
|
184 | } |
||
185 | 24 | if (isset($pStyles['underline'])) { |
|
186 | 16 | $this->setUnderline($pStyles['underline']); |
|
187 | } |
||
188 | 24 | if (isset($pStyles['strikethrough'])) { |
|
189 | 1 | $this->setStrikethrough($pStyles['strikethrough']); |
|
190 | } |
||
191 | 24 | if (isset($pStyles['color'])) { |
|
192 | 17 | $this->getColor()->applyFromArray($pStyles['color']); |
|
193 | } |
||
194 | 24 | if (isset($pStyles['size'])) { |
|
195 | 16 | $this->setSize($pStyles['size']); |
|
196 | } |
||
197 | } |
||
198 | |||
199 | 24 | return $this; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Get Name. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | 64 | public function getName() |
|
208 | { |
||
209 | 64 | if ($this->isSupervisor) { |
|
210 | return $this->getSharedComponent()->getName(); |
||
211 | } |
||
212 | |||
213 | 64 | return $this->name; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set Name. |
||
218 | * |
||
219 | * @param string $pValue |
||
220 | * |
||
221 | * @return Font |
||
222 | */ |
||
223 | 44 | View Code Duplication | public function setName($pValue) |
224 | { |
||
225 | 44 | if ($pValue == '') { |
|
226 | $pValue = 'Calibri'; |
||
227 | } |
||
228 | 44 | if ($this->isSupervisor) { |
|
229 | 13 | $styleArray = $this->getStyleArray(['name' => $pValue]); |
|
230 | 13 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
231 | } else { |
||
232 | 44 | $this->name = $pValue; |
|
233 | } |
||
234 | |||
235 | 44 | return $this; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get Size. |
||
240 | * |
||
241 | * @return float |
||
242 | */ |
||
243 | 64 | public function getSize() |
|
244 | { |
||
245 | 64 | if ($this->isSupervisor) { |
|
246 | return $this->getSharedComponent()->getSize(); |
||
247 | } |
||
248 | |||
249 | 64 | return $this->size; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * Set Size. |
||
254 | * |
||
255 | * @param float $pValue |
||
256 | * |
||
257 | * @return Font |
||
258 | */ |
||
259 | 46 | View Code Duplication | public function setSize($pValue) |
272 | } |
||
273 | |||
274 | /** |
||
275 | * Get Bold. |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | 64 | public function getBold() |
|
280 | { |
||
281 | 64 | if ($this->isSupervisor) { |
|
282 | return $this->getSharedComponent()->getBold(); |
||
283 | } |
||
284 | |||
285 | 64 | return $this->bold; |
|
286 | } |
||
287 | |||
288 | /** |
||
289 | * Set Bold. |
||
290 | * |
||
291 | * @param bool $pValue |
||
292 | * |
||
293 | * @return Font |
||
294 | */ |
||
295 | 48 | View Code Duplication | public function setBold($pValue) |
296 | { |
||
297 | 48 | if ($pValue == '') { |
|
298 | 8 | $pValue = false; |
|
299 | } |
||
300 | 48 | if ($this->isSupervisor) { |
|
301 | 19 | $styleArray = $this->getStyleArray(['bold' => $pValue]); |
|
302 | 19 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
303 | } else { |
||
304 | 48 | $this->bold = $pValue; |
|
305 | } |
||
306 | |||
307 | 48 | return $this; |
|
308 | } |
||
309 | |||
310 | /** |
||
311 | * Get Italic. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | 64 | public function getItalic() |
|
316 | { |
||
317 | 64 | if ($this->isSupervisor) { |
|
318 | return $this->getSharedComponent()->getItalic(); |
||
319 | } |
||
320 | |||
321 | 64 | return $this->italic; |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Set Italic. |
||
326 | * |
||
327 | * @param bool $pValue |
||
328 | * |
||
329 | * @return Font |
||
330 | */ |
||
331 | 30 | View Code Duplication | public function setItalic($pValue) |
332 | { |
||
333 | 30 | if ($pValue == '') { |
|
334 | 8 | $pValue = false; |
|
335 | } |
||
336 | 30 | if ($this->isSupervisor) { |
|
337 | 1 | $styleArray = $this->getStyleArray(['italic' => $pValue]); |
|
338 | 1 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
339 | } else { |
||
340 | 30 | $this->italic = $pValue; |
|
341 | } |
||
342 | |||
343 | 30 | return $this; |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * Get Superscript. |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | 62 | public function getSuperscript() |
|
352 | { |
||
353 | 62 | if ($this->isSupervisor) { |
|
354 | return $this->getSharedComponent()->getSuperscript(); |
||
355 | } |
||
356 | |||
357 | 62 | return $this->superscript; |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * Set Superscript. |
||
362 | * |
||
363 | * @param bool $pValue |
||
364 | * |
||
365 | * @return Font |
||
366 | */ |
||
367 | 4 | View Code Duplication | public function setSuperscript($pValue) |
368 | { |
||
369 | 4 | if ($pValue == '') { |
|
370 | $pValue = false; |
||
371 | } |
||
372 | 4 | if ($this->isSupervisor) { |
|
373 | $styleArray = $this->getStyleArray(['superscript' => $pValue]); |
||
374 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
375 | } else { |
||
376 | 4 | $this->superscript = $pValue; |
|
377 | 4 | $this->subscript = !$pValue; |
|
378 | } |
||
379 | |||
380 | 4 | return $this; |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get Subscript. |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | 62 | public function getSubscript() |
|
389 | { |
||
390 | 62 | if ($this->isSupervisor) { |
|
391 | return $this->getSharedComponent()->getSubscript(); |
||
392 | } |
||
393 | |||
394 | 62 | return $this->subscript; |
|
395 | } |
||
396 | |||
397 | /** |
||
398 | * Set Subscript. |
||
399 | * |
||
400 | * @param bool $pValue |
||
401 | * |
||
402 | * @return Font |
||
403 | */ |
||
404 | 4 | View Code Duplication | public function setSubscript($pValue) |
405 | { |
||
406 | 4 | if ($pValue == '') { |
|
407 | $pValue = false; |
||
408 | } |
||
409 | 4 | if ($this->isSupervisor) { |
|
410 | $styleArray = $this->getStyleArray(['subscript' => $pValue]); |
||
411 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
||
412 | } else { |
||
413 | 4 | $this->subscript = $pValue; |
|
414 | 4 | $this->superscript = !$pValue; |
|
415 | } |
||
416 | |||
417 | 4 | return $this; |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * Get Underline. |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | 64 | public function getUnderline() |
|
426 | { |
||
427 | 64 | if ($this->isSupervisor) { |
|
428 | return $this->getSharedComponent()->getUnderline(); |
||
429 | } |
||
430 | |||
431 | 64 | return $this->underline; |
|
432 | } |
||
433 | |||
434 | /** |
||
435 | * Set Underline. |
||
436 | * |
||
437 | * @param bool|string $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type |
||
438 | * If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, |
||
439 | * false equates to UNDERLINE_NONE |
||
440 | * |
||
441 | * @return Font |
||
442 | */ |
||
443 | 23 | View Code Duplication | public function setUnderline($pValue) |
444 | { |
||
445 | 23 | if (is_bool($pValue)) { |
|
446 | $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; |
||
447 | 23 | } elseif ($pValue == '') { |
|
448 | $pValue = self::UNDERLINE_NONE; |
||
449 | } |
||
450 | 23 | if ($this->isSupervisor) { |
|
451 | 13 | $styleArray = $this->getStyleArray(['underline' => $pValue]); |
|
452 | 13 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
453 | } else { |
||
454 | 23 | $this->underline = $pValue; |
|
455 | } |
||
456 | |||
457 | 23 | return $this; |
|
458 | } |
||
459 | |||
460 | /** |
||
461 | * Get Strikethrough. |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | 62 | public function getStrikethrough() |
|
466 | { |
||
467 | 62 | if ($this->isSupervisor) { |
|
468 | return $this->getSharedComponent()->getStrikethrough(); |
||
469 | } |
||
470 | |||
471 | 62 | return $this->strikethrough; |
|
472 | } |
||
473 | |||
474 | /** |
||
475 | * Set Strikethrough. |
||
476 | * |
||
477 | * @param bool $pValue |
||
478 | * |
||
479 | * @return Font |
||
480 | */ |
||
481 | 7 | View Code Duplication | public function setStrikethrough($pValue) |
494 | } |
||
495 | |||
496 | /** |
||
497 | * Get Color. |
||
498 | * |
||
499 | * @return Color |
||
500 | */ |
||
501 | 82 | public function getColor() |
|
502 | { |
||
503 | 82 | return $this->color; |
|
504 | } |
||
505 | |||
506 | /** |
||
507 | * Set Color. |
||
508 | * |
||
509 | * @param Color $pValue |
||
510 | * |
||
511 | * @throws PhpSpreadsheetException |
||
512 | * |
||
513 | * @return Font |
||
514 | */ |
||
515 | 16 | View Code Duplication | public function setColor(Color $pValue) |
516 | { |
||
517 | // make sure parameter is a real color and not a supervisor |
||
518 | 16 | $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; |
|
519 | |||
520 | 16 | if ($this->isSupervisor) { |
|
521 | 1 | $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
|
522 | 1 | $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
|
523 | } else { |
||
524 | 15 | $this->color = $color; |
|
525 | } |
||
526 | |||
527 | 16 | return $this; |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * Get hash code. |
||
532 | * |
||
533 | * @return string Hash code |
||
534 | */ |
||
535 | 77 | public function getHashCode() |
|
552 | ); |
||
553 | } |
||
554 | } |
||
555 |