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 |
||
29 | class Spreadsheet |
||
30 | { |
||
31 | /** |
||
32 | * Unique ID. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $uniqueID; |
||
37 | |||
38 | /** |
||
39 | * Document properties. |
||
40 | * |
||
41 | * @var Document\Properties |
||
42 | */ |
||
43 | private $properties; |
||
44 | |||
45 | /** |
||
46 | * Document security. |
||
47 | * |
||
48 | * @var Document\Security |
||
49 | */ |
||
50 | private $security; |
||
51 | |||
52 | /** |
||
53 | * Collection of Worksheet objects. |
||
54 | * |
||
55 | * @var Worksheet[] |
||
56 | */ |
||
57 | private $workSheetCollection = []; |
||
58 | |||
59 | /** |
||
60 | * Calculation Engine. |
||
61 | * |
||
62 | * @var Calculation |
||
63 | */ |
||
64 | private $calculationEngine; |
||
65 | |||
66 | /** |
||
67 | * Active sheet index. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | private $activeSheetIndex = 0; |
||
72 | |||
73 | /** |
||
74 | * Named ranges. |
||
75 | * |
||
76 | * @var NamedRange[] |
||
77 | */ |
||
78 | private $namedRanges = []; |
||
79 | |||
80 | /** |
||
81 | * CellXf supervisor. |
||
82 | * |
||
83 | * @var Style |
||
84 | */ |
||
85 | private $cellXfSupervisor; |
||
86 | |||
87 | /** |
||
88 | * CellXf collection. |
||
89 | * |
||
90 | * @var Style[] |
||
91 | */ |
||
92 | private $cellXfCollection = []; |
||
93 | |||
94 | /** |
||
95 | * CellStyleXf collection. |
||
96 | * |
||
97 | * @var Style[] |
||
98 | */ |
||
99 | private $cellStyleXfCollection = []; |
||
100 | |||
101 | /** |
||
102 | * hasMacros : this workbook have macros ? |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | private $hasMacros = false; |
||
107 | |||
108 | /** |
||
109 | * macrosCode : all macros code (the vbaProject.bin file, this include form, code, etc.), null if no macro. |
||
110 | * |
||
111 | * @var binary |
||
112 | */ |
||
113 | private $macrosCode; |
||
114 | /** |
||
115 | * macrosCertificate : if macros are signed, contains vbaProjectSignature.bin file, null if not signed. |
||
116 | * |
||
117 | * @var binary |
||
118 | */ |
||
119 | private $macrosCertificate; |
||
120 | |||
121 | /** |
||
122 | * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI. |
||
123 | * |
||
124 | * @var null|string |
||
125 | */ |
||
126 | private $ribbonXMLData; |
||
127 | |||
128 | /** |
||
129 | * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements |
||
130 | * ignored if $ribbonXMLData is null. |
||
131 | * |
||
132 | * @var null|array |
||
133 | */ |
||
134 | private $ribbonBinObjects; |
||
135 | |||
136 | /** |
||
137 | * The workbook has macros ? |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | 56 | public function hasMacros() |
|
142 | { |
||
143 | 56 | return $this->hasMacros; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Define if a workbook has macros. |
||
148 | * |
||
149 | * @param bool $hasMacros true|false |
||
150 | */ |
||
151 | public function setHasMacros($hasMacros) |
||
155 | |||
156 | /** |
||
157 | * Set the macros code. |
||
158 | * |
||
159 | * @param string $macroCode string|null |
||
160 | */ |
||
161 | public function setMacrosCode($macroCode) |
||
166 | |||
167 | /** |
||
168 | * Return the macros code. |
||
169 | * |
||
170 | * @return string|null |
||
171 | */ |
||
172 | public function getMacrosCode() |
||
176 | |||
177 | /** |
||
178 | * Set the macros certificate. |
||
179 | * |
||
180 | * @param string|null $certificate |
||
181 | */ |
||
182 | public function setMacrosCertificate($certificate) |
||
186 | |||
187 | /** |
||
188 | * Is the project signed ? |
||
189 | * |
||
190 | * @return bool true|false |
||
191 | */ |
||
192 | public function hasMacrosCertificate() |
||
196 | |||
197 | /** |
||
198 | * Return the macros certificate. |
||
199 | * |
||
200 | * @return string|null |
||
201 | */ |
||
202 | public function getMacrosCertificate() |
||
206 | |||
207 | /** |
||
208 | * Remove all macros, certificate from spreadsheet. |
||
209 | */ |
||
210 | public function discardMacros() |
||
216 | |||
217 | /** |
||
218 | * set ribbon XML data. |
||
219 | * |
||
220 | * @param null|mixed $target |
||
221 | * @param null|mixed $xmlData |
||
222 | */ |
||
223 | public function setRibbonXMLData($target, $xmlData) |
||
231 | |||
232 | /** |
||
233 | * retrieve ribbon XML Data. |
||
234 | * |
||
235 | * return string|null|array |
||
236 | * |
||
237 | * @param string $what |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getRibbonXMLData($what = 'all') //we need some constants here... |
||
259 | |||
260 | /** |
||
261 | * store binaries ribbon objects (pictures). |
||
262 | * |
||
263 | * @param null|mixed $BinObjectsNames |
||
264 | * @param null|mixed $BinObjectsData |
||
265 | */ |
||
266 | public function setRibbonBinObjects($BinObjectsNames, $BinObjectsData) |
||
274 | |||
275 | /** |
||
276 | * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function). |
||
277 | * |
||
278 | * @param mixed $ThePath |
||
279 | */ |
||
280 | private function getExtensionOnly($ThePath) |
||
284 | |||
285 | /** |
||
286 | * retrieve Binaries Ribbon Objects. |
||
287 | * |
||
288 | * @param mixed $what |
||
289 | */ |
||
290 | public function getRibbonBinObjects($what = 'all') |
||
317 | |||
318 | /** |
||
319 | * This workbook have a custom UI ? |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 56 | public function hasRibbon() |
|
327 | |||
328 | /** |
||
329 | * This workbook have additionnal object for the ribbon ? |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | 56 | public function hasRibbonBinObjects() |
|
337 | |||
338 | /** |
||
339 | * Check if a sheet with a specified code name already exists. |
||
340 | * |
||
341 | * @param string $pSheetCodeName Name of the worksheet to check |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 89 | public function sheetCodeNameExists($pSheetCodeName) |
|
349 | |||
350 | /** |
||
351 | * Get sheet by code name. Warning : sheet don't have always a code name ! |
||
352 | * |
||
353 | * @param string $pName Sheet name |
||
354 | * |
||
355 | * @return Worksheet |
||
356 | */ |
||
357 | 89 | View Code Duplication | public function getSheetByCodeName($pName) |
368 | |||
369 | /** |
||
370 | * Create a new PhpSpreadsheet with one Worksheet. |
||
371 | */ |
||
372 | 89 | public function __construct() |
|
399 | |||
400 | /** |
||
401 | * Code to execute when this worksheet is unset(). |
||
402 | */ |
||
403 | 1 | public function __destruct() |
|
408 | |||
409 | /** |
||
410 | * Disconnect all worksheets from this PhpSpreadsheet workbook object, |
||
411 | * typically so that the PhpSpreadsheet object can be unset. |
||
412 | */ |
||
413 | 1 | public function disconnectWorksheets() |
|
423 | |||
424 | /** |
||
425 | * Return the calculation engine for this worksheet. |
||
426 | * |
||
427 | * @return Calculation |
||
428 | */ |
||
429 | 89 | public function getCalculationEngine() |
|
433 | |||
434 | /** |
||
435 | * Get properties. |
||
436 | * |
||
437 | * @return Document\Properties |
||
438 | */ |
||
439 | 69 | public function getProperties() |
|
443 | |||
444 | /** |
||
445 | * Set properties. |
||
446 | * |
||
447 | * @param Document\Properties $pValue |
||
448 | */ |
||
449 | public function setProperties(Document\Properties $pValue) |
||
453 | |||
454 | /** |
||
455 | * Get security. |
||
456 | * |
||
457 | * @return Document\Security |
||
458 | */ |
||
459 | 56 | public function getSecurity() |
|
463 | |||
464 | /** |
||
465 | * Set security. |
||
466 | * |
||
467 | * @param Document\Security $pValue |
||
468 | */ |
||
469 | public function setSecurity(Document\Security $pValue) |
||
473 | |||
474 | /** |
||
475 | * Get active sheet. |
||
476 | * |
||
477 | * @throws Exception |
||
478 | * |
||
479 | * @return Worksheet |
||
480 | */ |
||
481 | 77 | public function getActiveSheet() |
|
485 | |||
486 | /** |
||
487 | * Create sheet and add it to this workbook. |
||
488 | * |
||
489 | * @param int|null $sheetIndex Index where sheet should go (0,1,..., or null for last) |
||
490 | * |
||
491 | * @throws Exception |
||
492 | * |
||
493 | * @return Worksheet |
||
494 | */ |
||
495 | 35 | public function createSheet($sheetIndex = null) |
|
502 | |||
503 | /** |
||
504 | * Check if a sheet with a specified name already exists. |
||
505 | * |
||
506 | * @param string $pSheetName Name of the worksheet to check |
||
507 | * |
||
508 | * @return bool |
||
509 | */ |
||
510 | 89 | public function sheetNameExists($pSheetName) |
|
514 | |||
515 | /** |
||
516 | * Add sheet. |
||
517 | * |
||
518 | * @param Worksheet $pSheet |
||
519 | * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
520 | * |
||
521 | * @throws Exception |
||
522 | * |
||
523 | * @return Worksheet |
||
524 | */ |
||
525 | 36 | public function addSheet(Worksheet $pSheet, $iSheetIndex = null) |
|
559 | |||
560 | /** |
||
561 | * Remove sheet by index. |
||
562 | * |
||
563 | * @param int $pIndex Active sheet index |
||
564 | * |
||
565 | * @throws Exception |
||
566 | */ |
||
567 | 13 | public function removeSheetByIndex($pIndex) |
|
583 | |||
584 | /** |
||
585 | * Get sheet by index. |
||
586 | * |
||
587 | * @param int $pIndex Sheet index |
||
588 | * |
||
589 | * @throws Exception |
||
590 | * |
||
591 | * @return Worksheet |
||
592 | */ |
||
593 | 89 | public function getSheet($pIndex) |
|
604 | |||
605 | /** |
||
606 | * Get all sheets. |
||
607 | * |
||
608 | * @return Worksheet[] |
||
609 | */ |
||
610 | 39 | public function getAllSheets() |
|
614 | |||
615 | /** |
||
616 | * Get sheet by name. |
||
617 | * |
||
618 | * @param string $pName Sheet name |
||
619 | * |
||
620 | * @return Worksheet |
||
621 | */ |
||
622 | 89 | View Code Duplication | public function getSheetByName($pName) |
633 | |||
634 | /** |
||
635 | * Get index for sheet. |
||
636 | * |
||
637 | * @param Worksheet $pSheet |
||
638 | * |
||
639 | * @throws Exception |
||
640 | * |
||
641 | * @return int index |
||
642 | */ |
||
643 | 71 | public function getIndex(Worksheet $pSheet) |
|
653 | |||
654 | /** |
||
655 | * Set index for sheet by sheet name. |
||
656 | * |
||
657 | * @param string $sheetName Sheet name to modify index for |
||
658 | * @param int $newIndex New index for the sheet |
||
659 | * |
||
660 | * @throws Exception |
||
661 | * |
||
662 | * @return int New sheet index |
||
663 | */ |
||
664 | public function setIndexByName($sheetName, $newIndex) |
||
681 | |||
682 | /** |
||
683 | * Get sheet count. |
||
684 | * |
||
685 | * @return int |
||
686 | */ |
||
687 | 77 | public function getSheetCount() |
|
691 | |||
692 | /** |
||
693 | * Get active sheet index. |
||
694 | * |
||
695 | * @return int Active sheet index |
||
696 | */ |
||
697 | 58 | public function getActiveSheetIndex() |
|
701 | |||
702 | /** |
||
703 | * Set active sheet index. |
||
704 | * |
||
705 | * @param int $pIndex Active sheet index |
||
706 | * |
||
707 | * @throws Exception |
||
708 | * |
||
709 | * @return Worksheet |
||
710 | */ |
||
711 | 62 | public function setActiveSheetIndex($pIndex) |
|
724 | |||
725 | /** |
||
726 | * Set active sheet index by name. |
||
727 | * |
||
728 | * @param string $pValue Sheet title |
||
729 | * |
||
730 | * @throws Exception |
||
731 | * |
||
732 | * @return Worksheet |
||
733 | */ |
||
734 | public function setActiveSheetIndexByName($pValue) |
||
744 | |||
745 | /** |
||
746 | * Get sheet names. |
||
747 | * |
||
748 | * @return string[] |
||
749 | */ |
||
750 | 1 | public function getSheetNames() |
|
760 | |||
761 | /** |
||
762 | * Add external sheet. |
||
763 | * |
||
764 | * @param Worksheet $pSheet External sheet to add |
||
765 | * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
766 | * |
||
767 | * @throws Exception |
||
768 | * |
||
769 | * @return Worksheet |
||
770 | */ |
||
771 | 1 | public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null) |
|
796 | |||
797 | /** |
||
798 | * Get named ranges. |
||
799 | * |
||
800 | * @return NamedRange[] |
||
801 | */ |
||
802 | 62 | public function getNamedRanges() |
|
806 | |||
807 | /** |
||
808 | * Add named range. |
||
809 | * |
||
810 | * @param NamedRange $namedRange |
||
811 | * |
||
812 | * @return bool |
||
813 | */ |
||
814 | 4 | public function addNamedRange(NamedRange $namedRange) |
|
826 | |||
827 | /** |
||
828 | * Get named range. |
||
829 | * |
||
830 | * @param string $namedRange |
||
831 | * @param Worksheet|null $pSheet Scope. Use null for global scope |
||
832 | * |
||
833 | * @return NamedRange|null |
||
834 | */ |
||
835 | 4 | public function getNamedRange($namedRange, Worksheet $pSheet = null) |
|
853 | |||
854 | /** |
||
855 | * Remove named range. |
||
856 | * |
||
857 | * @param string $namedRange |
||
858 | * @param Worksheet|null $pSheet scope: use null for global scope |
||
859 | * |
||
860 | * @return Spreadsheet |
||
861 | */ |
||
862 | 1 | public function removeNamedRange($namedRange, Worksheet $pSheet = null) |
|
876 | |||
877 | /** |
||
878 | * Get worksheet iterator. |
||
879 | * |
||
880 | * @return Worksheet\Iterator |
||
881 | */ |
||
882 | 64 | public function getWorksheetIterator() |
|
886 | |||
887 | /** |
||
888 | * Copy workbook (!= clone!). |
||
889 | * |
||
890 | * @return Spreadsheet |
||
891 | */ |
||
892 | public function copy() |
||
904 | |||
905 | /** |
||
906 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
907 | */ |
||
908 | public function __clone() |
||
916 | |||
917 | /** |
||
918 | * Get the workbook collection of cellXfs. |
||
919 | * |
||
920 | * @return Style[] |
||
921 | */ |
||
922 | 64 | public function getCellXfCollection() |
|
926 | |||
927 | /** |
||
928 | * Get cellXf by index. |
||
929 | * |
||
930 | * @param int $pIndex |
||
931 | * |
||
932 | * @return Style |
||
933 | */ |
||
934 | 45 | public function getCellXfByIndex($pIndex) |
|
938 | |||
939 | /** |
||
940 | * Get cellXf by hash code. |
||
941 | * |
||
942 | * @param string $pValue |
||
943 | * |
||
944 | * @return Style|false |
||
945 | */ |
||
946 | 42 | public function getCellXfByHashCode($pValue) |
|
956 | |||
957 | /** |
||
958 | * Check if style exists in style collection. |
||
959 | * |
||
960 | * @param Style $pCellStyle |
||
961 | * |
||
962 | * @return bool |
||
963 | */ |
||
964 | public function cellXfExists($pCellStyle) |
||
968 | |||
969 | /** |
||
970 | * Get default style. |
||
971 | * |
||
972 | * @throws Exception |
||
973 | * |
||
974 | * @return Style |
||
975 | */ |
||
976 | 62 | public function getDefaultStyle() |
|
983 | |||
984 | /** |
||
985 | * Add a cellXf to the workbook. |
||
986 | * |
||
987 | * @param Style $style |
||
988 | */ |
||
989 | 89 | public function addCellXf(Style $style) |
|
994 | |||
995 | /** |
||
996 | * Remove cellXf by index. It is ensured that all cells get their xf index updated. |
||
997 | * |
||
998 | * @param int $pIndex Index to cellXf |
||
999 | * |
||
1000 | * @throws Exception |
||
1001 | */ |
||
1002 | 12 | public function removeCellXfByIndex($pIndex) |
|
1026 | |||
1027 | /** |
||
1028 | * Get the cellXf supervisor. |
||
1029 | * |
||
1030 | * @return Style |
||
1031 | */ |
||
1032 | 40 | public function getCellXfSupervisor() |
|
1036 | |||
1037 | /** |
||
1038 | * Get the workbook collection of cellStyleXfs. |
||
1039 | * |
||
1040 | * @return Style[] |
||
1041 | */ |
||
1042 | 39 | public function getCellStyleXfCollection() |
|
1046 | |||
1047 | /** |
||
1048 | * Get cellStyleXf by index. |
||
1049 | * |
||
1050 | * @param int $pIndex Index to cellXf |
||
1051 | * |
||
1052 | * @return Style |
||
1053 | */ |
||
1054 | public function getCellStyleXfByIndex($pIndex) |
||
1058 | |||
1059 | /** |
||
1060 | * Get cellStyleXf by hash code. |
||
1061 | * |
||
1062 | * @param string $pValue |
||
1063 | * |
||
1064 | * @return Style|false |
||
1065 | */ |
||
1066 | public function getCellStyleXfByHashCode($pValue) |
||
1076 | |||
1077 | /** |
||
1078 | * Add a cellStyleXf to the workbook. |
||
1079 | * |
||
1080 | * @param Style $pStyle |
||
1081 | */ |
||
1082 | 89 | public function addCellStyleXf(Style $pStyle) |
|
1087 | |||
1088 | /** |
||
1089 | * Remove cellStyleXf by index. |
||
1090 | * |
||
1091 | * @param int $pIndex Index to cellXf |
||
1092 | * |
||
1093 | * @throws Exception |
||
1094 | */ |
||
1095 | 12 | public function removeCellStyleXfByIndex($pIndex) |
|
1102 | |||
1103 | /** |
||
1104 | * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells |
||
1105 | * and columns in the workbook. |
||
1106 | */ |
||
1107 | 62 | public function garbageCollect() |
|
1182 | |||
1183 | /** |
||
1184 | * Return the unique ID value assigned to this spreadsheet workbook. |
||
1185 | * |
||
1186 | * @return string |
||
1187 | */ |
||
1188 | public function getID() |
||
1192 | } |
||
1193 |
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..