Complex classes like Xf 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 Xf, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
61 | class Xf |
||
62 | { |
||
63 | /** |
||
64 | * Style XF or a cell XF ? |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | private $isStyleXf; |
||
69 | |||
70 | /** |
||
71 | * Index to the FONT record. Index 4 does not exist |
||
72 | * @var int |
||
73 | */ |
||
74 | private $fontIndex; |
||
75 | |||
76 | /** |
||
77 | * An index (2 bytes) to a FORMAT record (number format). |
||
78 | * @var int |
||
79 | */ |
||
80 | private $numberFormatIndex; |
||
81 | |||
82 | /** |
||
83 | * 1 bit, apparently not used. |
||
84 | * @var int |
||
85 | */ |
||
86 | private $textJustLast; |
||
87 | |||
88 | /** |
||
89 | * The cell's foreground color. |
||
90 | * @var int |
||
91 | */ |
||
92 | private $foregroundColor; |
||
93 | |||
94 | /** |
||
95 | * The cell's background color. |
||
96 | * @var int |
||
97 | */ |
||
98 | private $backgroundColor; |
||
99 | |||
100 | /** |
||
101 | * Color of the bottom border of the cell. |
||
102 | * @var int |
||
103 | */ |
||
104 | private $bottomBorderColor; |
||
105 | |||
106 | /** |
||
107 | * Color of the top border of the cell. |
||
108 | * @var int |
||
109 | */ |
||
110 | private $topBorderColor; |
||
111 | |||
112 | /** |
||
113 | * Color of the left border of the cell. |
||
114 | * @var int |
||
115 | */ |
||
116 | private $leftBorderColor; |
||
117 | |||
118 | /** |
||
119 | * Color of the right border of the cell. |
||
120 | * @var int |
||
121 | */ |
||
122 | private $rightBorderColor; |
||
123 | |||
124 | /** |
||
125 | * Constructor |
||
126 | * |
||
127 | * @param \PhpOffice\PhpSpreadsheet\Style The XF format |
||
128 | */ |
||
129 | 38 | public function __construct(\PhpOffice\PhpSpreadsheet\Style $style = null) |
|
150 | |||
151 | /** |
||
152 | * Generate an Excel BIFF XF record (style or cell). |
||
153 | * |
||
154 | * @return string The XF record |
||
155 | */ |
||
156 | 38 | public function writeXf() |
|
251 | |||
252 | /** |
||
253 | * Is this a style XF ? |
||
254 | * |
||
255 | * @param bool $value |
||
256 | */ |
||
257 | 38 | public function setIsStyleXf($value) |
|
261 | |||
262 | /** |
||
263 | * Sets the cell's bottom border color |
||
264 | * |
||
265 | * @param int $colorIndex Color index |
||
266 | */ |
||
267 | 38 | public function setBottomColor($colorIndex) |
|
271 | |||
272 | /** |
||
273 | * Sets the cell's top border color |
||
274 | * |
||
275 | * @param int $colorIndex Color index |
||
276 | */ |
||
277 | 38 | public function setTopColor($colorIndex) |
|
281 | |||
282 | /** |
||
283 | * Sets the cell's left border color |
||
284 | * |
||
285 | * @param int $colorIndex Color index |
||
286 | */ |
||
287 | 38 | public function setLeftColor($colorIndex) |
|
291 | |||
292 | /** |
||
293 | * Sets the cell's right border color |
||
294 | * |
||
295 | * @param int $colorIndex Color index |
||
296 | */ |
||
297 | 38 | public function setRightColor($colorIndex) |
|
301 | |||
302 | /** |
||
303 | * Sets the cell's diagonal border color |
||
304 | * |
||
305 | * @param int $colorIndex Color index |
||
306 | */ |
||
307 | 38 | public function setDiagColor($colorIndex) |
|
311 | |||
312 | /** |
||
313 | * Sets the cell's foreground color |
||
314 | * |
||
315 | * @param int $colorIndex Color index |
||
316 | */ |
||
317 | 38 | public function setFgColor($colorIndex) |
|
321 | |||
322 | /** |
||
323 | * Sets the cell's background color |
||
324 | * |
||
325 | * @param int $colorIndex Color index |
||
326 | */ |
||
327 | 38 | public function setBgColor($colorIndex) |
|
331 | |||
332 | /** |
||
333 | * Sets the index to the number format record |
||
334 | * It can be date, time, currency, etc... |
||
335 | * |
||
336 | * @param int $numberFormatIndex Index to format record |
||
337 | */ |
||
338 | 38 | public function setNumberFormatIndex($numberFormatIndex) |
|
342 | |||
343 | /** |
||
344 | * Set the font index. |
||
345 | * |
||
346 | * @param int $value Font index, note that value 4 does not exist |
||
347 | */ |
||
348 | 38 | public function setFontIndex($value) |
|
352 | |||
353 | /** |
||
354 | * Map of BIFF2-BIFF8 codes for border styles |
||
355 | * @static array of int |
||
356 | */ |
||
357 | private static $mapBorderStyles = [ |
||
358 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE => 0x00, |
||
359 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN => 0x01, |
||
360 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM => 0x02, |
||
361 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DASHED => 0x03, |
||
362 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOTTED => 0x04, |
||
363 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK => 0x05, |
||
364 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOUBLE => 0x06, |
||
365 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_HAIR => 0x07, |
||
366 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUMDASHED => 0x08, |
||
367 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DASHDOT => 0x09, |
||
368 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUMDASHDOT => 0x0A, |
||
369 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DASHDOTDOT => 0x0B, |
||
370 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUMDASHDOTDOT => 0x0C, |
||
371 | \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_SLANTDASHDOT => 0x0D, |
||
372 | ]; |
||
373 | |||
374 | /** |
||
375 | * Map border style |
||
376 | * |
||
377 | * @param string $borderStyle |
||
378 | * @return int |
||
379 | */ |
||
380 | 38 | private static function mapBorderStyle($borderStyle) |
|
388 | |||
389 | /** |
||
390 | * Map of BIFF2-BIFF8 codes for fill types |
||
391 | * @static array of int |
||
392 | */ |
||
393 | private static $mapFillTypes = [ |
||
394 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_NONE => 0x00, |
||
395 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID => 0x01, |
||
396 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_MEDIUMGRAY => 0x02, |
||
397 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKGRAY => 0x03, |
||
398 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTGRAY => 0x04, |
||
399 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKHORIZONTAL => 0x05, |
||
400 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKVERTICAL => 0x06, |
||
401 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKDOWN => 0x07, |
||
402 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKUP => 0x08, |
||
403 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKGRID => 0x09, |
||
404 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_DARKTRELLIS => 0x0A, |
||
405 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTHORIZONTAL => 0x0B, |
||
406 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTVERTICAL => 0x0C, |
||
407 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTDOWN => 0x0D, |
||
408 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTUP => 0x0E, |
||
409 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTGRID => 0x0F, |
||
410 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTTRELLIS => 0x10, |
||
411 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_GRAY125 => 0x11, |
||
412 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_GRAY0625 => 0x12, |
||
413 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR => 0x00, // does not exist in BIFF8 |
||
414 | \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_PATH => 0x00, // does not exist in BIFF8 |
||
415 | ]; |
||
416 | |||
417 | /** |
||
418 | * Map fill type |
||
419 | * |
||
420 | * @param string $fillType |
||
421 | * @return int |
||
422 | */ |
||
423 | 38 | private static function mapFillType($fillType) |
|
431 | |||
432 | /** |
||
433 | * Map of BIFF2-BIFF8 codes for horizontal alignment |
||
434 | * @static array of int |
||
435 | */ |
||
436 | private static $mapHAlignments = [ |
||
437 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_GENERAL => 0, |
||
438 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT => 1, |
||
439 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER => 2, |
||
440 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT => 3, |
||
441 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_FILL => 4, |
||
442 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY => 5, |
||
443 | \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER_CONTINUOUS => 6, |
||
444 | ]; |
||
445 | |||
446 | /** |
||
447 | * Map to BIFF2-BIFF8 codes for horizontal alignment |
||
448 | * |
||
449 | * @param string $hAlign |
||
450 | * @return int |
||
451 | */ |
||
452 | 38 | private function mapHAlign($hAlign) |
|
460 | |||
461 | /** |
||
462 | * Map of BIFF2-BIFF8 codes for vertical alignment |
||
463 | * @static array of int |
||
464 | */ |
||
465 | private static $mapVAlignments = [ |
||
466 | \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP => 0, |
||
467 | \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER => 1, |
||
468 | \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM => 2, |
||
469 | \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY => 3, |
||
470 | ]; |
||
471 | |||
472 | /** |
||
473 | * Map to BIFF2-BIFF8 codes for vertical alignment |
||
474 | * |
||
475 | * @param string $vAlign |
||
476 | * @return int |
||
477 | */ |
||
478 | 38 | private static function mapVAlign($vAlign) |
|
486 | |||
487 | /** |
||
488 | * Map to BIFF8 codes for text rotation angle |
||
489 | * |
||
490 | * @param int $textRotation |
||
491 | * @return int |
||
492 | */ |
||
493 | 38 | private static function mapTextRotation($textRotation) |
|
503 | |||
504 | /** |
||
505 | * Map locked |
||
506 | * |
||
507 | * @param string |
||
508 | * @return int |
||
509 | */ |
||
510 | 38 | private static function mapLocked($locked) |
|
523 | |||
524 | /** |
||
525 | * Map hidden |
||
526 | * |
||
527 | * @param string |
||
528 | * @return int |
||
529 | */ |
||
530 | 38 | private static function mapHidden($hidden) |
|
543 | } |
||
544 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: