Complex classes like ExcelWriter 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 ExcelWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ExcelWriter |
||
19 | { |
||
20 | /** @var array */ |
||
21 | protected $sheets = []; |
||
22 | /** @var array */ |
||
23 | protected $sharedStrings = []; |
||
24 | /** @var int */ |
||
25 | protected $sharedStringCount = 0; |
||
26 | /** @var array */ |
||
27 | protected $tempFiles = []; |
||
28 | /** @var array */ |
||
29 | protected $cellFormats = []; |
||
30 | /** @var array */ |
||
31 | protected $cellTypes = []; |
||
32 | /** @var string */ |
||
33 | protected $currentSheet = ''; |
||
34 | /** @var null */ |
||
35 | protected $tmpDir = null; |
||
36 | /** @var null */ |
||
37 | protected $fileName = null; |
||
38 | /** @var Core */ |
||
39 | protected $core; |
||
40 | /** @var App */ |
||
41 | protected $app; |
||
42 | /** @var Workbook */ |
||
43 | protected $workbook; |
||
44 | /** @var SheetXml */ |
||
45 | protected $sheetXml; |
||
46 | |||
47 | /** |
||
48 | * ExcelWriter constructor. |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | public function __construct() |
||
52 | { |
||
53 | if (!class_exists('ZipArchive')) { |
||
54 | throw new \Exception('ZipArchive not found'); |
||
55 | } |
||
56 | if (!ini_get('date.timezone')) { |
||
57 | date_default_timezone_set('UTC'); |
||
58 | } |
||
59 | $this->addCellFormat($cell_format = 'GENERAL'); |
||
60 | $this->core = new Core(); |
||
61 | $this->app = new App(); |
||
62 | $this->workbook = new Workbook(); |
||
63 | $this->sheetXml = new SheetXml(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $author |
||
68 | */ |
||
69 | public function setAuthor($author) |
||
70 | { |
||
71 | $this->core->setAuthor($author); |
||
72 | } |
||
73 | |||
74 | public function __destruct() |
||
75 | { |
||
76 | if (!empty($this->tempFiles)) { |
||
77 | foreach ($this->tempFiles as $tempFile) { |
||
78 | if (file_exists($tempFile)) { |
||
79 | unlink($tempFile); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $dir |
||
87 | */ |
||
88 | public function setTmpDir($dir) |
||
92 | |||
93 | /** |
||
94 | * Set output filename: yourFileName.xlsx |
||
95 | * |
||
96 | * @param string $fileName |
||
97 | */ |
||
98 | public function setFileName($fileName) |
||
102 | |||
103 | /** |
||
104 | * Return tmpFileName |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function tempFilename() |
||
115 | |||
116 | /** |
||
117 | * @param bool $headers |
||
118 | */ |
||
119 | public function writeToStdOut($headers = true) |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function writeToString() |
||
153 | |||
154 | /** |
||
155 | * @param string $filename |
||
156 | */ |
||
157 | public function writeToFile($filename) |
||
191 | |||
192 | /** |
||
193 | * @param string $sheetName |
||
194 | */ |
||
195 | protected function initializeSheet($sheetName) |
||
224 | |||
225 | /** |
||
226 | * @param $cellFormat |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | private function determineCellType($cellFormat) |
||
272 | |||
273 | /** |
||
274 | * backwards compatibility |
||
275 | * |
||
276 | * @param $cellFormat |
||
277 | * |
||
278 | * @return int|mixed |
||
279 | */ |
||
280 | private function addCellFormat($cellFormat) |
||
292 | |||
293 | /** |
||
294 | * @link https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.numberingformats(v=office.15).aspx |
||
295 | * |
||
296 | * @param string $cellFormat |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | private function getCellFormat($cellFormat) |
||
324 | |||
325 | /** |
||
326 | * @param string $sheetName |
||
327 | * @param array $headerTypes |
||
328 | * @param bool $suppressRow |
||
329 | */ |
||
330 | public function writeSheetHeader($sheetName, array $headerTypes, $suppressRow = false) |
||
348 | |||
349 | /** |
||
350 | * @param Sheet $sheet |
||
351 | * @param array $headerRow |
||
352 | */ |
||
353 | private function writeRowHeader(Sheet $sheet, $headerRow) |
||
364 | |||
365 | /** |
||
366 | * @param string $sheetName |
||
367 | * @param array $row |
||
368 | */ |
||
369 | public function writeSheetRow($sheetName, array $row) |
||
401 | |||
402 | /** |
||
403 | * @param string $sheetName |
||
404 | */ |
||
405 | protected function finalizeSheet($sheetName) |
||
434 | |||
435 | /** |
||
436 | * @param string $sheetName |
||
437 | * @param int $startCellRow |
||
438 | * @param int $startCellColumn |
||
439 | * @param int $endCellRow |
||
440 | * @param int $endCellColumn |
||
441 | */ |
||
442 | public function markMergedCell($sheetName, $startCellRow, $startCellColumn, $endCellRow, $endCellColumn) |
||
454 | |||
455 | /** |
||
456 | * @param array $data |
||
457 | * @param string $sheetName |
||
458 | * @param array $headerTypes |
||
459 | */ |
||
460 | public function writeSheet(array $data, $sheetName = '', array $headerTypes = []) |
||
472 | |||
473 | /** |
||
474 | * @param Writer $file |
||
475 | * @param int $rowNumber |
||
476 | * @param int $columnNumber |
||
477 | * @param mixed $value |
||
478 | * @param $cellIndex |
||
479 | */ |
||
480 | protected function writeCell(Writer $file, $rowNumber, $columnNumber, $value, $cellIndex) |
||
495 | |||
496 | /** |
||
497 | * @return string |
||
498 | */ |
||
499 | protected function writeStylesXML() |
||
509 | |||
510 | /** |
||
511 | * @param $v |
||
512 | * |
||
513 | * @return int|mixed |
||
514 | */ |
||
515 | protected function setSharedString($v) |
||
527 | |||
528 | /** |
||
529 | * @return string |
||
530 | */ |
||
531 | protected function writeSharedStringsXML() |
||
541 | |||
542 | /** |
||
543 | * @param \ZipArchive $zip |
||
544 | * @param string $filename |
||
545 | */ |
||
546 | private function checkAndUnlink(\ZipArchive $zip, $filename) |
||
557 | } |
||
558 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..