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 Spreadsheet 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 Spreadsheet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Spreadsheet |
||
6 | { |
||
7 | /** |
||
8 | * Unique ID. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | private $uniqueID; |
||
13 | |||
14 | /** |
||
15 | * Document properties. |
||
16 | * |
||
17 | * @var Document\Properties |
||
18 | */ |
||
19 | private $properties; |
||
20 | |||
21 | /** |
||
22 | * Document security. |
||
23 | * |
||
24 | * @var Document\Security |
||
25 | */ |
||
26 | private $security; |
||
27 | |||
28 | /** |
||
29 | * Collection of Worksheet objects. |
||
30 | * |
||
31 | * @var Worksheet[] |
||
32 | */ |
||
33 | private $workSheetCollection = []; |
||
34 | |||
35 | /** |
||
36 | * Calculation Engine. |
||
37 | * |
||
38 | * @var Calculation |
||
39 | */ |
||
40 | private $calculationEngine; |
||
41 | |||
42 | /** |
||
43 | * Active sheet index. |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | private $activeSheetIndex = 0; |
||
48 | |||
49 | /** |
||
50 | * Named ranges. |
||
51 | * |
||
52 | * @var NamedRange[] |
||
53 | */ |
||
54 | private $namedRanges = []; |
||
55 | |||
56 | /** |
||
57 | * CellXf supervisor. |
||
58 | * |
||
59 | * @var Style |
||
60 | */ |
||
61 | private $cellXfSupervisor; |
||
62 | |||
63 | /** |
||
64 | * CellXf collection. |
||
65 | * |
||
66 | * @var Style[] |
||
67 | */ |
||
68 | private $cellXfCollection = []; |
||
69 | |||
70 | /** |
||
71 | * CellStyleXf collection. |
||
72 | * |
||
73 | * @var Style[] |
||
74 | */ |
||
75 | private $cellStyleXfCollection = []; |
||
76 | |||
77 | /** |
||
78 | * hasMacros : this workbook have macros ? |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $hasMacros = false; |
||
83 | |||
84 | /** |
||
85 | * macrosCode : all macros code (the vbaProject.bin file, this include form, code, etc.), null if no macro. |
||
86 | * |
||
87 | * @var binary |
||
88 | */ |
||
89 | private $macrosCode; |
||
90 | /** |
||
91 | * macrosCertificate : if macros are signed, contains vbaProjectSignature.bin file, null if not signed. |
||
92 | * |
||
93 | * @var binary |
||
94 | */ |
||
95 | private $macrosCertificate; |
||
96 | |||
97 | /** |
||
98 | * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI. |
||
99 | * |
||
100 | * @var null|string |
||
101 | */ |
||
102 | private $ribbonXMLData; |
||
103 | |||
104 | /** |
||
105 | * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements |
||
106 | * ignored if $ribbonXMLData is null. |
||
107 | * |
||
108 | * @var null|array |
||
109 | */ |
||
110 | private $ribbonBinObjects; |
||
111 | |||
112 | /** |
||
113 | * The workbook has macros ? |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function hasMacros() |
||
118 | { |
||
119 | return $this->hasMacros; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Define if a workbook has macros. |
||
124 | * |
||
125 | * @param bool $hasMacros true|false |
||
126 | */ |
||
127 | public function setHasMacros($hasMacros) |
||
131 | |||
132 | /** |
||
133 | * Set the macros code. |
||
134 | * |
||
135 | * @param string $macroCode string|null |
||
136 | */ |
||
137 | public function setMacrosCode($macroCode) |
||
142 | |||
143 | /** |
||
144 | * Return the macros code. |
||
145 | * |
||
146 | * @return null|string |
||
147 | */ |
||
148 | public function getMacrosCode() |
||
152 | |||
153 | /** |
||
154 | * Set the macros certificate. |
||
155 | * |
||
156 | * @param null|string $certificate |
||
157 | */ |
||
158 | public function setMacrosCertificate($certificate) |
||
162 | |||
163 | /** |
||
164 | * Is the project signed ? |
||
165 | * |
||
166 | * @return bool true|false |
||
167 | */ |
||
168 | public function hasMacrosCertificate() |
||
172 | |||
173 | /** |
||
174 | * Return the macros certificate. |
||
175 | * |
||
176 | * @return null|string |
||
177 | */ |
||
178 | public function getMacrosCertificate() |
||
182 | |||
183 | /** |
||
184 | * Remove all macros, certificate from spreadsheet. |
||
185 | */ |
||
186 | public function discardMacros() |
||
192 | |||
193 | /** |
||
194 | * set ribbon XML data. |
||
195 | * |
||
196 | * @param null|mixed $target |
||
197 | * @param null|mixed $xmlData |
||
198 | */ |
||
199 | public function setRibbonXMLData($target, $xmlData) |
||
207 | |||
208 | /** |
||
209 | * retrieve ribbon XML Data. |
||
210 | * |
||
211 | * return string|null|array |
||
212 | * |
||
213 | * @param string $what |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getRibbonXMLData($what = 'all') //we need some constants here... |
||
237 | |||
238 | /** |
||
239 | * store binaries ribbon objects (pictures). |
||
240 | * |
||
241 | * @param null|mixed $BinObjectsNames |
||
242 | * @param null|mixed $BinObjectsData |
||
243 | */ |
||
244 | public function setRibbonBinObjects($BinObjectsNames, $BinObjectsData) |
||
252 | |||
253 | /** |
||
254 | * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function). |
||
255 | * |
||
256 | * @param mixed $ThePath |
||
257 | */ |
||
258 | private function getExtensionOnly($ThePath) |
||
262 | |||
263 | /** |
||
264 | * retrieve Binaries Ribbon Objects. |
||
265 | * |
||
266 | * @param mixed $what |
||
267 | */ |
||
268 | public function getRibbonBinObjects($what = 'all') |
||
297 | |||
298 | /** |
||
299 | * This workbook have a custom UI ? |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function hasRibbon() |
||
307 | |||
308 | /** |
||
309 | * This workbook have additionnal object for the ribbon ? |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | public function hasRibbonBinObjects() |
||
317 | |||
318 | /** |
||
319 | * Check if a sheet with a specified code name already exists. |
||
320 | * |
||
321 | * @param string $pSheetCodeName Name of the worksheet to check |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | public function sheetCodeNameExists($pSheetCodeName) |
||
329 | |||
330 | /** |
||
331 | * Get sheet by code name. Warning : sheet don't have always a code name ! |
||
332 | * |
||
333 | * @param string $pName Sheet name |
||
334 | * |
||
335 | * @return Worksheet |
||
336 | */ |
||
337 | View Code Duplication | public function getSheetByCodeName($pName) |
|
348 | |||
349 | /** |
||
350 | * Create a new PhpSpreadsheet with one Worksheet. |
||
351 | */ |
||
352 | 76 | public function __construct() |
|
379 | |||
380 | /** |
||
381 | * Code to execute when this worksheet is unset(). |
||
382 | */ |
||
383 | public function __destruct() |
||
388 | |||
389 | /** |
||
390 | * Disconnect all worksheets from this PhpSpreadsheet workbook object, |
||
391 | * typically so that the PhpSpreadsheet object can be unset. |
||
392 | */ |
||
393 | public function disconnectWorksheets() |
||
403 | |||
404 | /** |
||
405 | * Return the calculation engine for this worksheet. |
||
406 | * |
||
407 | * @return Calculation |
||
408 | */ |
||
409 | public function getCalculationEngine() |
||
413 | |||
414 | /** |
||
415 | * Get properties. |
||
416 | * |
||
417 | * @return Document\Properties |
||
418 | */ |
||
419 | public function getProperties() |
||
423 | |||
424 | /** |
||
425 | * Set properties. |
||
426 | * |
||
427 | * @param Document\Properties $pValue |
||
428 | */ |
||
429 | public function setProperties(Document\Properties $pValue) |
||
433 | |||
434 | /** |
||
435 | * Get security. |
||
436 | * |
||
437 | * @return Document\Security |
||
438 | */ |
||
439 | public function getSecurity() |
||
443 | |||
444 | /** |
||
445 | * Set security. |
||
446 | * |
||
447 | * @param Document\Security $pValue |
||
448 | */ |
||
449 | public function setSecurity(Document\Security $pValue) |
||
453 | |||
454 | /** |
||
455 | * Get active sheet. |
||
456 | * |
||
457 | * @throws Exception |
||
458 | * |
||
459 | * @return Worksheet |
||
460 | */ |
||
461 | public function getActiveSheet() |
||
465 | |||
466 | /** |
||
467 | * Create sheet and add it to this workbook. |
||
468 | * |
||
469 | * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last) |
||
470 | * |
||
471 | * @throws Exception |
||
472 | * |
||
473 | * @return Worksheet |
||
474 | */ |
||
475 | public function createSheet($sheetIndex = null) |
||
482 | |||
483 | /** |
||
484 | * Check if a sheet with a specified name already exists. |
||
485 | * |
||
486 | * @param string $pSheetName Name of the worksheet to check |
||
487 | * |
||
488 | * @return bool |
||
489 | */ |
||
490 | public function sheetNameExists($pSheetName) |
||
494 | |||
495 | /** |
||
496 | * Add sheet. |
||
497 | * |
||
498 | * @param Worksheet $pSheet |
||
499 | * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
500 | * |
||
501 | * @throws Exception |
||
502 | * |
||
503 | * @return Worksheet |
||
504 | */ |
||
505 | public function addSheet(Worksheet $pSheet, $iSheetIndex = null) |
||
539 | |||
540 | /** |
||
541 | * Remove sheet by index. |
||
542 | * |
||
543 | * @param int $pIndex Active sheet index |
||
544 | * |
||
545 | * @throws Exception |
||
546 | */ |
||
547 | public function removeSheetByIndex($pIndex) |
||
563 | |||
564 | /** |
||
565 | * Get sheet by index. |
||
566 | * |
||
567 | * @param int $pIndex Sheet index |
||
568 | * |
||
569 | * @throws Exception |
||
570 | * |
||
571 | * @return Worksheet |
||
572 | */ |
||
573 | public function getSheet($pIndex) |
||
585 | |||
586 | /** |
||
587 | * Get all sheets. |
||
588 | * |
||
589 | * @return Worksheet[] |
||
590 | */ |
||
591 | public function getAllSheets() |
||
595 | |||
596 | /** |
||
597 | * Get sheet by name. |
||
598 | * |
||
599 | * @param string $pName Sheet name |
||
600 | * |
||
601 | * @return Worksheet |
||
602 | */ |
||
603 | View Code Duplication | public function getSheetByName($pName) |
|
614 | |||
615 | /** |
||
616 | * Get index for sheet. |
||
617 | * |
||
618 | * @param Worksheet $pSheet |
||
619 | * |
||
620 | * @throws Exception |
||
621 | * |
||
622 | * @return int index |
||
623 | */ |
||
624 | public function getIndex(Worksheet $pSheet) |
||
634 | |||
635 | /** |
||
636 | * Set index for sheet by sheet name. |
||
637 | * |
||
638 | * @param string $sheetName Sheet name to modify index for |
||
639 | * @param int $newIndex New index for the sheet |
||
640 | * |
||
641 | * @throws Exception |
||
642 | * |
||
643 | * @return int New sheet index |
||
644 | */ |
||
645 | public function setIndexByName($sheetName, $newIndex) |
||
662 | |||
663 | /** |
||
664 | * Get sheet count. |
||
665 | * |
||
666 | * @return int |
||
667 | */ |
||
668 | public function getSheetCount() |
||
672 | |||
673 | /** |
||
674 | * Get active sheet index. |
||
675 | * |
||
676 | * @return int Active sheet index |
||
677 | */ |
||
678 | public function getActiveSheetIndex() |
||
682 | |||
683 | /** |
||
684 | * Set active sheet index. |
||
685 | * |
||
686 | * @param int $pIndex Active sheet index |
||
687 | * |
||
688 | * @throws Exception |
||
689 | * |
||
690 | * @return Worksheet |
||
691 | */ |
||
692 | public function setActiveSheetIndex($pIndex) |
||
705 | |||
706 | /** |
||
707 | * Set active sheet index by name. |
||
708 | * |
||
709 | * @param string $pValue Sheet title |
||
710 | * |
||
711 | * @throws Exception |
||
712 | * |
||
713 | * @return Worksheet |
||
714 | */ |
||
715 | public function setActiveSheetIndexByName($pValue) |
||
725 | |||
726 | /** |
||
727 | * Get sheet names. |
||
728 | * |
||
729 | * @return string[] |
||
730 | */ |
||
731 | public function getSheetNames() |
||
741 | |||
742 | /** |
||
743 | * Add external sheet. |
||
744 | * |
||
745 | * @param Worksheet $pSheet External sheet to add |
||
746 | * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
747 | * |
||
748 | * @throws Exception |
||
749 | * |
||
750 | * @return Worksheet |
||
751 | */ |
||
752 | public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null) |
||
777 | |||
778 | /** |
||
779 | * Get named ranges. |
||
780 | * |
||
781 | * @return NamedRange[] |
||
782 | */ |
||
783 | public function getNamedRanges() |
||
787 | |||
788 | /** |
||
789 | * Add named range. |
||
790 | * |
||
791 | * @param NamedRange $namedRange |
||
792 | * |
||
793 | * @return bool |
||
794 | */ |
||
795 | public function addNamedRange(NamedRange $namedRange) |
||
807 | |||
808 | /** |
||
809 | * Get named range. |
||
810 | * |
||
811 | * @param string $namedRange |
||
812 | * @param null|Worksheet $pSheet Scope. Use null for global scope |
||
813 | * |
||
814 | * @return null|NamedRange |
||
815 | */ |
||
816 | public function getNamedRange($namedRange, Worksheet $pSheet = null) |
||
834 | |||
835 | /** |
||
836 | * Remove named range. |
||
837 | * |
||
838 | * @param string $namedRange |
||
839 | * @param null|Worksheet $pSheet scope: use null for global scope |
||
840 | * |
||
841 | * @return Spreadsheet |
||
842 | */ |
||
843 | public function removeNamedRange($namedRange, Worksheet $pSheet = null) |
||
857 | |||
858 | /** |
||
859 | * Get worksheet iterator. |
||
860 | * |
||
861 | * @return Worksheet\Iterator |
||
862 | */ |
||
863 | public function getWorksheetIterator() |
||
867 | |||
868 | /** |
||
869 | * Copy workbook (!= clone!). |
||
870 | * |
||
871 | * @return Spreadsheet |
||
872 | */ |
||
873 | public function copy() |
||
885 | |||
886 | /** |
||
887 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
888 | */ |
||
889 | public function __clone() |
||
897 | |||
898 | /** |
||
899 | * Get the workbook collection of cellXfs. |
||
900 | * |
||
901 | * @return Style[] |
||
902 | */ |
||
903 | public function getCellXfCollection() |
||
907 | |||
908 | /** |
||
909 | * Get cellXf by index. |
||
910 | * |
||
911 | * @param int $pIndex |
||
912 | * |
||
913 | * @return Style |
||
914 | */ |
||
915 | public function getCellXfByIndex($pIndex) |
||
919 | |||
920 | /** |
||
921 | * Get cellXf by hash code. |
||
922 | * |
||
923 | * @param string $pValue |
||
924 | * |
||
925 | * @return false|Style |
||
926 | */ |
||
927 | public function getCellXfByHashCode($pValue) |
||
937 | |||
938 | /** |
||
939 | * Check if style exists in style collection. |
||
940 | * |
||
941 | * @param Style $pCellStyle |
||
942 | * |
||
943 | * @return bool |
||
944 | */ |
||
945 | public function cellXfExists($pCellStyle) |
||
949 | |||
950 | /** |
||
951 | * Get default style. |
||
952 | * |
||
953 | * @throws Exception |
||
954 | * |
||
955 | * @return Style |
||
956 | */ |
||
957 | public function getDefaultStyle() |
||
965 | |||
966 | /** |
||
967 | * Add a cellXf to the workbook. |
||
968 | * |
||
969 | * @param Style $style |
||
970 | */ |
||
971 | public function addCellXf(Style $style) |
||
976 | |||
977 | /** |
||
978 | * Remove cellXf by index. It is ensured that all cells get their xf index updated. |
||
979 | * |
||
980 | * @param int $pIndex Index to cellXf |
||
981 | * |
||
982 | * @throws Exception |
||
983 | */ |
||
984 | public function removeCellXfByIndex($pIndex) |
||
1008 | |||
1009 | /** |
||
1010 | * Get the cellXf supervisor. |
||
1011 | * |
||
1012 | * @return Style |
||
1013 | */ |
||
1014 | public function getCellXfSupervisor() |
||
1018 | |||
1019 | /** |
||
1020 | * Get the workbook collection of cellStyleXfs. |
||
1021 | * |
||
1022 | * @return Style[] |
||
1023 | */ |
||
1024 | public function getCellStyleXfCollection() |
||
1028 | |||
1029 | /** |
||
1030 | * Get cellStyleXf by index. |
||
1031 | * |
||
1032 | * @param int $pIndex Index to cellXf |
||
1033 | * |
||
1034 | * @return Style |
||
1035 | */ |
||
1036 | public function getCellStyleXfByIndex($pIndex) |
||
1040 | |||
1041 | /** |
||
1042 | * Get cellStyleXf by hash code. |
||
1043 | * |
||
1044 | * @param string $pValue |
||
1045 | * |
||
1046 | * @return false|Style |
||
1047 | */ |
||
1048 | public function getCellStyleXfByHashCode($pValue) |
||
1058 | |||
1059 | /** |
||
1060 | * Add a cellStyleXf to the workbook. |
||
1061 | * |
||
1062 | * @param Style $pStyle |
||
1063 | */ |
||
1064 | public function addCellStyleXf(Style $pStyle) |
||
1069 | |||
1070 | /** |
||
1071 | * Remove cellStyleXf by index. |
||
1072 | * |
||
1073 | * @param int $pIndex Index to cellXf |
||
1074 | * |
||
1075 | * @throws Exception |
||
1076 | */ |
||
1077 | public function removeCellStyleXfByIndex($pIndex) |
||
1084 | |||
1085 | /** |
||
1086 | * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells |
||
1087 | * and columns in the workbook. |
||
1088 | */ |
||
1089 | public function garbageCollect() |
||
1164 | |||
1165 | /** |
||
1166 | * Return the unique ID value assigned to this spreadsheet workbook. |
||
1167 | * |
||
1168 | * @return string |
||
1169 | */ |
||
1170 | public function getID() |
||
1174 | } |
||
1175 |
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..