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 | public function hasMacros() |
||
145 | |||
146 | /** |
||
147 | * Define if a workbook has macros |
||
148 | * |
||
149 | * @param bool $hasMacros true|false |
||
150 | */ |
||
151 | public function setHasMacros($hasMacros = false) |
||
155 | |||
156 | /** |
||
157 | * Set the macros code |
||
158 | * |
||
159 | * @param string $macroCode string|null |
||
160 | */ |
||
161 | public function setMacrosCode($macroCode = null) |
||
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 = null) |
||
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 | public function setRibbonXMLData($target = null, $xmlData = null) |
||
228 | |||
229 | /** |
||
230 | * retrieve ribbon XML Data |
||
231 | * |
||
232 | * return string|null|array |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getRibbonXMLData($what = 'all') //we need some constants here... |
||
253 | |||
254 | /** |
||
255 | * store binaries ribbon objects (pictures) |
||
256 | */ |
||
257 | public function setRibbonBinObjects($BinObjectsNames = null, $BinObjectsData = null) |
||
265 | /** |
||
266 | * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function) |
||
267 | */ |
||
268 | private function getExtensionOnly($ThePath) |
||
272 | |||
273 | /** |
||
274 | * retrieve Binaries Ribbon Objects |
||
275 | */ |
||
276 | public function getRibbonBinObjects($What = 'all') |
||
303 | |||
304 | /** |
||
305 | * This workbook have a custom UI ? |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function hasRibbon() |
||
313 | |||
314 | /** |
||
315 | * This workbook have additionnal object for the ribbon ? |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | public function hasRibbonBinObjects() |
||
323 | |||
324 | /** |
||
325 | * Check if a sheet with a specified code name already exists |
||
326 | * |
||
327 | * @param string $pSheetCodeName Name of the worksheet to check |
||
328 | * @return bool |
||
329 | */ |
||
330 | 1 | public function sheetCodeNameExists($pSheetCodeName) |
|
334 | |||
335 | /** |
||
336 | * Get sheet by code name. Warning : sheet don't have always a code name ! |
||
337 | * |
||
338 | * @param string $pName Sheet name |
||
339 | * @return Worksheet |
||
340 | */ |
||
341 | 1 | View Code Duplication | public function getSheetByCodeName($pName = '') |
352 | |||
353 | /** |
||
354 | * Create a new PhpSpreadsheet with one Worksheet |
||
355 | */ |
||
356 | 1 | public function __construct() |
|
383 | |||
384 | /** |
||
385 | * Code to execute when this worksheet is unset() |
||
386 | */ |
||
387 | public function __destruct() |
||
392 | |||
393 | /** |
||
394 | * Disconnect all worksheets from this PhpSpreadsheet workbook object, |
||
395 | * typically so that the PhpSpreadsheet object can be unset |
||
396 | */ |
||
397 | public function disconnectWorksheets() |
||
407 | |||
408 | /** |
||
409 | * Return the calculation engine for this worksheet |
||
410 | * |
||
411 | * @return Calculation |
||
412 | */ |
||
413 | 1 | public function getCalculationEngine() |
|
417 | |||
418 | /** |
||
419 | * Get properties |
||
420 | * |
||
421 | * @return Document\Properties |
||
422 | */ |
||
423 | public function getProperties() |
||
427 | |||
428 | /** |
||
429 | * Set properties |
||
430 | * |
||
431 | * @param Document\Properties $pValue |
||
432 | */ |
||
433 | public function setProperties(Document\Properties $pValue) |
||
437 | |||
438 | /** |
||
439 | * Get security |
||
440 | * |
||
441 | * @return Document\Security |
||
442 | */ |
||
443 | public function getSecurity() |
||
447 | |||
448 | /** |
||
449 | * Set security |
||
450 | * |
||
451 | * @param Document\Security $pValue |
||
452 | */ |
||
453 | public function setSecurity(Document\Security $pValue) |
||
457 | |||
458 | /** |
||
459 | * Get active sheet |
||
460 | * |
||
461 | * @throws Exception |
||
462 | * @return Worksheet |
||
463 | */ |
||
464 | 1 | public function getActiveSheet() |
|
468 | |||
469 | /** |
||
470 | * Create sheet and add it to this workbook |
||
471 | * |
||
472 | * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
473 | * @throws Exception |
||
474 | * @return Worksheet |
||
475 | */ |
||
476 | public function createSheet($iSheetIndex = null) |
||
483 | |||
484 | /** |
||
485 | * Check if a sheet with a specified name already exists |
||
486 | * |
||
487 | * @param string $pSheetName Name of the worksheet to check |
||
488 | * @return bool |
||
489 | */ |
||
490 | 1 | public function sheetNameExists($pSheetName) |
|
494 | |||
495 | /** |
||
496 | * Add sheet |
||
497 | * |
||
498 | * @param Worksheet $pSheet |
||
499 | * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
500 | * @throws Exception |
||
501 | * @return Worksheet |
||
502 | */ |
||
503 | public function addSheet(Worksheet $pSheet, $iSheetIndex = null) |
||
537 | |||
538 | /** |
||
539 | * Remove sheet by index |
||
540 | * |
||
541 | * @param int $pIndex Active sheet index |
||
542 | * @throws Exception |
||
543 | */ |
||
544 | public function removeSheetByIndex($pIndex = 0) |
||
560 | |||
561 | /** |
||
562 | * Get sheet by index |
||
563 | * |
||
564 | * @param int $pIndex Sheet index |
||
565 | * @throws Exception |
||
566 | * @return Worksheet |
||
567 | */ |
||
568 | 1 | public function getSheet($pIndex = 0) |
|
579 | |||
580 | /** |
||
581 | * Get all sheets |
||
582 | * |
||
583 | * @return Worksheet[] |
||
584 | */ |
||
585 | public function getAllSheets() |
||
589 | |||
590 | /** |
||
591 | * Get sheet by name |
||
592 | * |
||
593 | * @param string $pName Sheet name |
||
594 | * @return Worksheet |
||
595 | */ |
||
596 | 1 | View Code Duplication | public function getSheetByName($pName = '') |
607 | |||
608 | /** |
||
609 | * Get index for sheet |
||
610 | * |
||
611 | * @param Worksheet $pSheet |
||
612 | * @throws Exception |
||
613 | * @return int index |
||
614 | */ |
||
615 | public function getIndex(Worksheet $pSheet) |
||
625 | |||
626 | /** |
||
627 | * Set index for sheet by sheet name. |
||
628 | * |
||
629 | * @param string $sheetName Sheet name to modify index for |
||
630 | * @param int $newIndex New index for the sheet |
||
631 | * @throws Exception |
||
632 | * @return int New sheet index |
||
633 | */ |
||
634 | public function setIndexByName($sheetName, $newIndex) |
||
651 | |||
652 | /** |
||
653 | * Get sheet count |
||
654 | * |
||
655 | * @return int |
||
656 | */ |
||
657 | public function getSheetCount() |
||
661 | |||
662 | /** |
||
663 | * Get active sheet index |
||
664 | * |
||
665 | * @return int Active sheet index |
||
666 | */ |
||
667 | public function getActiveSheetIndex() |
||
671 | |||
672 | /** |
||
673 | * Set active sheet index |
||
674 | * |
||
675 | * @param int $pIndex Active sheet index |
||
676 | * @throws Exception |
||
677 | * @return Worksheet |
||
678 | */ |
||
679 | public function setActiveSheetIndex($pIndex = 0) |
||
693 | |||
694 | /** |
||
695 | * Set active sheet index by name |
||
696 | * |
||
697 | * @param string $pValue Sheet title |
||
698 | * @throws Exception |
||
699 | * @return Worksheet |
||
700 | */ |
||
701 | public function setActiveSheetIndexByName($pValue = '') |
||
711 | |||
712 | /** |
||
713 | * Get sheet names |
||
714 | * |
||
715 | * @return string[] |
||
716 | */ |
||
717 | public function getSheetNames() |
||
727 | |||
728 | /** |
||
729 | * Add external sheet |
||
730 | * |
||
731 | * @param Worksheet $pSheet External sheet to add |
||
732 | * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) |
||
733 | * @throws Exception |
||
734 | * @return Worksheet |
||
735 | */ |
||
736 | public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null) |
||
761 | |||
762 | /** |
||
763 | * Get named ranges |
||
764 | * |
||
765 | * @return NamedRange[] |
||
766 | */ |
||
767 | public function getNamedRanges() |
||
771 | |||
772 | /** |
||
773 | * Add named range |
||
774 | * |
||
775 | * @param NamedRange $namedRange |
||
776 | * @return bool |
||
777 | */ |
||
778 | public function addNamedRange(NamedRange $namedRange) |
||
790 | |||
791 | /** |
||
792 | * Get named range |
||
793 | * |
||
794 | * @param string $namedRange |
||
795 | * @param Worksheet|null $pSheet Scope. Use null for global scope |
||
796 | * @return NamedRange|null |
||
797 | */ |
||
798 | public function getNamedRange($namedRange, Worksheet $pSheet = null) |
||
816 | |||
817 | /** |
||
818 | * Remove named range |
||
819 | * |
||
820 | * @param string $namedRange |
||
821 | * @param Worksheet|null $pSheet Scope: use null for global scope. |
||
822 | * @return Spreadsheet |
||
823 | */ |
||
824 | public function removeNamedRange($namedRange, Worksheet $pSheet = null) |
||
838 | |||
839 | /** |
||
840 | * Get worksheet iterator |
||
841 | * |
||
842 | * @return Worksheet\Iterator |
||
843 | */ |
||
844 | public function getWorksheetIterator() |
||
848 | |||
849 | /** |
||
850 | * Copy workbook (!= clone!) |
||
851 | * |
||
852 | * @return Spreadsheet |
||
853 | */ |
||
854 | public function copy() |
||
866 | |||
867 | /** |
||
868 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
||
869 | */ |
||
870 | public function __clone() |
||
878 | |||
879 | /** |
||
880 | * Get the workbook collection of cellXfs |
||
881 | * |
||
882 | * @return Style[] |
||
883 | */ |
||
884 | public function getCellXfCollection() |
||
888 | |||
889 | /** |
||
890 | * Get cellXf by index |
||
891 | * |
||
892 | * @param int $pIndex |
||
893 | * @return Style |
||
894 | */ |
||
895 | public function getCellXfByIndex($pIndex = 0) |
||
899 | |||
900 | /** |
||
901 | * Get cellXf by hash code |
||
902 | * |
||
903 | * @param string $pValue |
||
904 | * @return Style|false |
||
905 | */ |
||
906 | public function getCellXfByHashCode($pValue = '') |
||
916 | |||
917 | /** |
||
918 | * Check if style exists in style collection |
||
919 | * |
||
920 | * @param Style $pCellStyle |
||
921 | * @return bool |
||
922 | */ |
||
923 | public function cellXfExists($pCellStyle = null) |
||
927 | |||
928 | /** |
||
929 | * Get default style |
||
930 | * |
||
931 | * @throws Exception |
||
932 | * @return Style |
||
933 | */ |
||
934 | public function getDefaultStyle() |
||
941 | |||
942 | /** |
||
943 | * Add a cellXf to the workbook |
||
944 | * |
||
945 | * @param Style $style |
||
946 | */ |
||
947 | 1 | public function addCellXf(Style $style) |
|
952 | |||
953 | /** |
||
954 | * Remove cellXf by index. It is ensured that all cells get their xf index updated. |
||
955 | * |
||
956 | * @param int $pIndex Index to cellXf |
||
957 | * @throws Exception |
||
958 | */ |
||
959 | public function removeCellXfByIndex($pIndex = 0) |
||
983 | |||
984 | /** |
||
985 | * Get the cellXf supervisor |
||
986 | * |
||
987 | * @return Style |
||
988 | */ |
||
989 | public function getCellXfSupervisor() |
||
993 | |||
994 | /** |
||
995 | * Get the workbook collection of cellStyleXfs |
||
996 | * |
||
997 | * @return Style[] |
||
998 | */ |
||
999 | public function getCellStyleXfCollection() |
||
1003 | |||
1004 | /** |
||
1005 | * Get cellStyleXf by index |
||
1006 | * |
||
1007 | * @param int $pIndex Index to cellXf |
||
1008 | * @return Style |
||
1009 | */ |
||
1010 | public function getCellStyleXfByIndex($pIndex = 0) |
||
1014 | |||
1015 | /** |
||
1016 | * Get cellStyleXf by hash code |
||
1017 | * |
||
1018 | * @param string $pValue |
||
1019 | * @return Style|false |
||
1020 | */ |
||
1021 | public function getCellStyleXfByHashCode($pValue = '') |
||
1031 | |||
1032 | /** |
||
1033 | * Add a cellStyleXf to the workbook |
||
1034 | * |
||
1035 | * @param Style $pStyle |
||
1036 | */ |
||
1037 | 1 | public function addCellStyleXf(Style $pStyle) |
|
1042 | |||
1043 | /** |
||
1044 | * Remove cellStyleXf by index |
||
1045 | * |
||
1046 | * @param int $pIndex Index to cellXf |
||
1047 | * @throws Exception |
||
1048 | */ |
||
1049 | public function removeCellStyleXfByIndex($pIndex = 0) |
||
1057 | |||
1058 | /** |
||
1059 | * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells |
||
1060 | * and columns in the workbook |
||
1061 | */ |
||
1062 | public function garbageCollect() |
||
1137 | |||
1138 | /** |
||
1139 | * Return the unique ID value assigned to this spreadsheet workbook |
||
1140 | * |
||
1141 | * @return string |
||
1142 | */ |
||
1143 | public function getID() |
||
1147 | } |
||
1148 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.