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