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 StringTable 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 StringTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class StringTable extends WriterPart |
||
34 | { |
||
35 | /** |
||
36 | * Create worksheet stringtable. |
||
37 | * |
||
38 | * @param Worksheet $pSheet Worksheet |
||
39 | * @param string[] $pExistingTable Existing table to eventually merge with |
||
40 | * |
||
41 | * @throws WriterException |
||
42 | * |
||
43 | * @return string[] String table for worksheet |
||
44 | */ |
||
45 | 56 | public function createStringTable($pSheet = null, $pExistingTable = null) |
|
84 | |||
85 | /** |
||
86 | * Write string table to XML format. |
||
87 | * |
||
88 | * @param string[] $pStringTable |
||
89 | * |
||
90 | * @throws WriterException |
||
91 | * |
||
92 | * @return string XML Output |
||
93 | */ |
||
94 | 56 | public function writeStringTable(array $pStringTable) |
|
95 | { |
||
96 | // Create XML writer |
||
97 | 56 | $objWriter = null; |
|
98 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
99 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
100 | } else { |
||
101 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
102 | } |
||
103 | |||
104 | // XML header |
||
105 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
106 | |||
107 | // String table |
||
108 | 56 | $objWriter->startElement('sst'); |
|
109 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
110 | 56 | $objWriter->writeAttribute('uniqueCount', count($pStringTable)); |
|
111 | |||
112 | // Loop through string table |
||
113 | 56 | foreach ($pStringTable as $textElement) { |
|
114 | 51 | $objWriter->startElement('si'); |
|
115 | |||
116 | 51 | if (!$textElement instanceof RichText) { |
|
117 | 50 | $textToWrite = StringHelper::controlCharacterPHP2OOXML($textElement); |
|
118 | 50 | $objWriter->startElement('t'); |
|
119 | 50 | if ($textToWrite !== trim($textToWrite)) { |
|
120 | 1 | $objWriter->writeAttribute('xml:space', 'preserve'); |
|
121 | } |
||
122 | 50 | $objWriter->writeRawData($textToWrite); |
|
123 | 50 | $objWriter->endElement(); |
|
124 | 10 | } elseif ($textElement instanceof RichText) { |
|
125 | 10 | $this->writeRichText($objWriter, $textElement); |
|
126 | } |
||
127 | |||
128 | 51 | $objWriter->endElement(); |
|
129 | } |
||
130 | |||
131 | 56 | $objWriter->endElement(); |
|
132 | |||
133 | 56 | return $objWriter->getData(); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Write Rich Text. |
||
138 | * |
||
139 | * @param XMLWriter $objWriter XML Writer |
||
140 | * @param RichText $pRichText Rich text |
||
141 | * @param string $prefix Optional Namespace prefix |
||
142 | * |
||
143 | * @throws WriterException |
||
144 | */ |
||
145 | 13 | public function writeRichText(XMLWriter $objWriter, RichText $pRichText, $prefix = null) |
|
220 | |||
221 | /** |
||
222 | * Write Rich Text. |
||
223 | * |
||
224 | * @param XMLWriter $objWriter XML Writer |
||
225 | * @param string|RichText $pRichText text string or Rich text |
||
226 | * @param string $prefix Optional Namespace prefix |
||
227 | * |
||
228 | * @throws WriterException |
||
229 | */ |
||
230 | 13 | public function writeRichTextForCharts(XMLWriter $objWriter, $pRichText = null, $prefix = null) |
|
284 | |||
285 | /** |
||
286 | * Flip string table (for index searching). |
||
287 | * |
||
288 | * @param array $stringTable Stringtable |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 56 | public function flipStringTable(array $stringTable) |
|
308 | } |
||
309 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.