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 |
||
27 | class StringTable extends WriterPart |
||
28 | { |
||
29 | /** |
||
30 | * Create worksheet stringtable. |
||
31 | * |
||
32 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $pSheet Worksheet |
||
33 | * @param string[] $pExistingTable Existing table to eventually merge with |
||
34 | * |
||
35 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
36 | * |
||
37 | * @return string[] String table for worksheet |
||
38 | */ |
||
39 | 55 | public function createStringTable($pSheet = null, $pExistingTable = null) |
|
40 | { |
||
41 | 55 | if ($pSheet !== null) { |
|
42 | // Create string lookup table |
||
43 | 55 | $aStringTable = []; |
|
44 | 55 | $cellCollection = null; |
|
|
|||
45 | 55 | $aFlippedStringTable = null; // For faster lookup |
|
46 | |||
47 | // Is an existing table given? |
||
48 | 55 | if (($pExistingTable !== null) && is_array($pExistingTable)) { |
|
49 | 55 | $aStringTable = $pExistingTable; |
|
50 | } |
||
51 | |||
52 | // Fill index array |
||
53 | 55 | $aFlippedStringTable = $this->flipStringTable($aStringTable); |
|
54 | |||
55 | // Loop through cells |
||
56 | 55 | foreach ($pSheet->getCoordinates() as $coordinate) { |
|
57 | 55 | $cell = $pSheet->getCell($coordinate); |
|
58 | 55 | $cellValue = $cell->getValue(); |
|
59 | 55 | if (!is_object($cellValue) && |
|
60 | 54 | ($cellValue !== null) && |
|
61 | 51 | $cellValue !== '' && |
|
62 | 51 | !isset($aFlippedStringTable[$cellValue]) && |
|
63 | 51 | ($cell->getDataType() == \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING || $cell->getDataType() == \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING2 || $cell->getDataType() == \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NULL)) { |
|
64 | 49 | $aStringTable[] = $cellValue; |
|
65 | 49 | $aFlippedStringTable[$cellValue] = true; |
|
66 | } elseif ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText && |
||
67 | 10 | ($cellValue !== null) && |
|
68 | 10 | !isset($aFlippedStringTable[$cellValue->getHashCode()])) { |
|
69 | 10 | $aStringTable[] = $cellValue; |
|
70 | 10 | $aFlippedStringTable[$cellValue->getHashCode()] = true; |
|
71 | } |
||
72 | } |
||
73 | |||
74 | 55 | return $aStringTable; |
|
75 | } |
||
76 | throw new \PhpOffice\PhpSpreadsheet\Writer\Exception("Invalid \PhpOffice\PhpSpreadsheet\Worksheet object passed."); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Write string table to XML format. |
||
81 | * |
||
82 | * @param string[] $pStringTable |
||
83 | * |
||
84 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
85 | * |
||
86 | * @return string XML Output |
||
87 | */ |
||
88 | 55 | public function writeStringTable(array $pStringTable) |
|
89 | { |
||
90 | // Create XML writer |
||
91 | 55 | $objWriter = null; |
|
92 | 55 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
93 | $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
94 | } else { |
||
95 | 55 | $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY); |
|
96 | } |
||
97 | |||
98 | // XML header |
||
99 | 55 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
100 | |||
101 | // String table |
||
102 | 55 | $objWriter->startElement('sst'); |
|
103 | 55 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
104 | 55 | $objWriter->writeAttribute('uniqueCount', count($pStringTable)); |
|
105 | |||
106 | // Loop through string table |
||
107 | 55 | foreach ($pStringTable as $textElement) { |
|
108 | 50 | $objWriter->startElement('si'); |
|
109 | |||
110 | 50 | if (!$textElement instanceof \PhpOffice\PhpSpreadsheet\RichText) { |
|
111 | 49 | $textToWrite = \PhpOffice\PhpSpreadsheet\Shared\StringHelper::controlCharacterPHP2OOXML($textElement); |
|
112 | 49 | $objWriter->startElement('t'); |
|
113 | 49 | if ($textToWrite !== trim($textToWrite)) { |
|
114 | 1 | $objWriter->writeAttribute('xml:space', 'preserve'); |
|
115 | } |
||
116 | 49 | $objWriter->writeRawData($textToWrite); |
|
117 | 49 | $objWriter->endElement(); |
|
118 | } elseif ($textElement instanceof \PhpOffice\PhpSpreadsheet\RichText) { |
||
119 | 10 | $this->writeRichText($objWriter, $textElement); |
|
120 | } |
||
121 | |||
122 | 50 | $objWriter->endElement(); |
|
123 | } |
||
124 | |||
125 | 55 | $objWriter->endElement(); |
|
126 | |||
127 | 55 | return $objWriter->getData(); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Write Rich Text. |
||
132 | * |
||
133 | * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer |
||
134 | * @param \PhpOffice\PhpSpreadsheet\RichText $pRichText Rich text |
||
135 | * @param string $prefix Optional Namespace prefix |
||
136 | * |
||
137 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
138 | */ |
||
139 | 13 | public function writeRichText(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\RichText $pRichText, $prefix = null) |
|
214 | |||
215 | /** |
||
216 | * Write Rich Text. |
||
217 | * |
||
218 | * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter XML Writer |
||
219 | * @param string|\PhpOffice\PhpSpreadsheet\RichText $pRichText text string or Rich text |
||
220 | * @param string $prefix Optional Namespace prefix |
||
221 | * |
||
222 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
223 | */ |
||
224 | 13 | public function writeRichTextForCharts(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $pRichText = null, $prefix = null) |
|
225 | { |
||
226 | 13 | if (!$pRichText instanceof \PhpOffice\PhpSpreadsheet\RichText) { |
|
227 | 12 | $textRun = $pRichText; |
|
228 | 12 | $pRichText = new \PhpOffice\PhpSpreadsheet\RichText(); |
|
229 | 12 | $pRichText->createTextRun($textRun); |
|
230 | } |
||
231 | |||
232 | 13 | if ($prefix !== null) { |
|
233 | 13 | $prefix .= ':'; |
|
234 | } |
||
235 | |||
236 | // Loop through rich text elements |
||
237 | 13 | $elements = $pRichText->getRichTextElements(); |
|
238 | 13 | foreach ($elements as $element) { |
|
239 | // r |
||
240 | 13 | $objWriter->startElement($prefix . 'r'); |
|
241 | |||
242 | // rPr |
||
243 | 13 | $objWriter->startElement($prefix . 'rPr'); |
|
244 | |||
245 | // Bold |
||
246 | 13 | $objWriter->writeAttribute('b', ($element->getFont()->getBold() ? 1 : 0)); |
|
247 | // Italic |
||
248 | 13 | $objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? 1 : 0)); |
|
249 | // Underline |
||
250 | 13 | $underlineType = $element->getFont()->getUnderline(); |
|
251 | switch ($underlineType) { |
||
252 | 13 | case 'single': |
|
253 | 1 | $underlineType = 'sng'; |
|
254 | 1 | break; |
|
255 | 13 | case 'double': |
|
256 | 1 | $underlineType = 'dbl'; |
|
257 | 1 | break; |
|
258 | } |
||
259 | 13 | $objWriter->writeAttribute('u', $underlineType); |
|
260 | // Strikethrough |
||
261 | 13 | $objWriter->writeAttribute('strike', ($element->getFont()->getStrikethrough() ? 'sngStrike' : 'noStrike')); |
|
262 | |||
263 | // rFont |
||
264 | 13 | $objWriter->startElement($prefix . 'latin'); |
|
265 | 13 | $objWriter->writeAttribute('typeface', $element->getFont()->getName()); |
|
266 | 13 | $objWriter->endElement(); |
|
267 | |||
268 | 13 | $objWriter->endElement(); |
|
269 | |||
270 | // t |
||
271 | 13 | $objWriter->startElement($prefix . 't'); |
|
272 | 13 | $objWriter->writeRawData(\PhpOffice\PhpSpreadsheet\Shared\StringHelper::controlCharacterPHP2OOXML($element->getText())); |
|
273 | 13 | $objWriter->endElement(); |
|
274 | |||
275 | 13 | $objWriter->endElement(); |
|
276 | } |
||
277 | 13 | } |
|
278 | |||
279 | /** |
||
280 | * Flip string table (for index searching). |
||
281 | * |
||
282 | * @param array $stringTable Stringtable |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | 55 | public function flipStringTable(array $stringTable) |
|
302 | } |
||
303 |
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.