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 $tmpFileName = 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 |
||
95 | * exp: yourFileName.xlsx |
||
96 | * |
||
97 | * @param string $tmpFileName |
||
98 | */ |
||
99 | public function setTmpFileName($tmpFileName) |
||
103 | |||
104 | /** |
||
105 | * Return tmpFileName |
||
106 | * @return string |
||
107 | */ |
||
108 | protected function tempFilename() |
||
116 | |||
117 | /** |
||
118 | * @param bool $headers |
||
119 | */ |
||
120 | public function writeToStdOut($headers = true) |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function writeToString() |
||
154 | |||
155 | /** |
||
156 | * @param string $filename |
||
157 | */ |
||
158 | public function writeToFile($filename) |
||
192 | |||
193 | /** |
||
194 | * @param string $sheetName |
||
195 | */ |
||
196 | protected function initializeSheet($sheetName) |
||
225 | |||
226 | /** |
||
227 | * @param $cellFormat |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | private function determineCellType($cellFormat) |
||
270 | |||
271 | /** |
||
272 | * backwards compatibility |
||
273 | * |
||
274 | * @param $cellFormat |
||
275 | * |
||
276 | * @return int|mixed |
||
277 | */ |
||
278 | private function addCellFormat($cellFormat) |
||
290 | |||
291 | /** |
||
292 | * @link https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.numberingformats(v=office.15).aspx |
||
293 | * |
||
294 | * @param string $cellFormat |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | private function getCellFormat($cellFormat) |
||
321 | |||
322 | /** |
||
323 | * @param string $sheetName |
||
324 | * @param array $headerTypes |
||
325 | * @param bool $suppressRow |
||
326 | */ |
||
327 | public function writeSheetHeader($sheetName, array $headerTypes, $suppressRow = false) |
||
345 | |||
346 | /** |
||
347 | * @param Sheet $sheet |
||
348 | * @param array $headerRow |
||
349 | */ |
||
350 | private function writeRowHeader(Sheet $sheet, $headerRow) |
||
361 | |||
362 | /** |
||
363 | * @param string $sheetName |
||
364 | * @param array $row |
||
365 | */ |
||
366 | public function writeSheetRow($sheetName, array $row) |
||
398 | |||
399 | /** |
||
400 | * @param string $sheetName |
||
401 | */ |
||
402 | protected function finalizeSheet($sheetName) |
||
431 | |||
432 | /** |
||
433 | * @param string $sheetName |
||
434 | * @param int $startCellRow |
||
435 | * @param int $startCellColumn |
||
436 | * @param int $endCellRow |
||
437 | * @param int $endCellColumn |
||
438 | */ |
||
439 | public function markMergedCell($sheetName, $startCellRow, $startCellColumn, $endCellRow, $endCellColumn) |
||
451 | |||
452 | /** |
||
453 | * @param array $data |
||
454 | * @param string $sheetName |
||
455 | * @param array $headerTypes |
||
456 | */ |
||
457 | public function writeSheet(array $data, $sheetName = '', array $headerTypes = []) |
||
469 | |||
470 | /** |
||
471 | * @param Writer $file |
||
472 | * @param int $rowNumber |
||
473 | * @param int $columnNumber |
||
474 | * @param mixed $value |
||
475 | * @param $cellIndex |
||
476 | */ |
||
477 | protected function writeCell(Writer $file, $rowNumber, $columnNumber, $value, $cellIndex) |
||
492 | |||
493 | /** |
||
494 | * @return string |
||
495 | */ |
||
496 | protected function writeStylesXML() |
||
506 | |||
507 | /** |
||
508 | * @param $v |
||
509 | * |
||
510 | * @return int|mixed |
||
511 | */ |
||
512 | protected function setSharedString($v) |
||
524 | |||
525 | /** |
||
526 | * @return string |
||
527 | */ |
||
528 | protected function writeSharedStringsXML() |
||
538 | |||
539 | /** |
||
540 | * @param \ZipArchive $zip |
||
541 | * @param string $filename |
||
542 | */ |
||
543 | private function checkAndUnlink(\ZipArchive $zip, $filename) |
||
554 | } |
||
555 |
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..