Total Complexity | 44 |
Total Lines | 273 |
Duplicated Lines | 1.83 % |
Coverage | 98.44% |
Changes | 0 |
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.
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 |
||
13 | class StringTable extends WriterPart |
||
14 | { |
||
15 | /** |
||
16 | * Create worksheet stringtable. |
||
17 | * |
||
18 | * @param Worksheet $pSheet Worksheet |
||
19 | * @param string[] $pExistingTable Existing table to eventually merge with |
||
20 | * |
||
21 | * @throws WriterException |
||
22 | * |
||
23 | * @return string[] String table for worksheet |
||
24 | */ |
||
25 | 56 | public function createStringTable(Worksheet $pSheet, $pExistingTable = null) |
|
26 | { |
||
27 | // Create string lookup table |
||
28 | 56 | $aStringTable = []; |
|
29 | 56 | $cellCollection = null; |
|
30 | 56 | $aFlippedStringTable = null; // For faster lookup |
|
31 | |||
32 | // Is an existing table given? |
||
33 | 56 | if (($pExistingTable !== null) && is_array($pExistingTable)) { |
|
34 | 56 | $aStringTable = $pExistingTable; |
|
35 | } |
||
36 | |||
37 | // Fill index array |
||
38 | 56 | $aFlippedStringTable = $this->flipStringTable($aStringTable); |
|
39 | |||
40 | // Loop through cells |
||
41 | 56 | foreach ($pSheet->getCoordinates() as $coordinate) { |
|
42 | 56 | $cell = $pSheet->getCell($coordinate); |
|
43 | 56 | $cellValue = $cell->getValue(); |
|
44 | 56 | if (!is_object($cellValue) && |
|
45 | 56 | ($cellValue !== null) && |
|
46 | 56 | $cellValue !== '' && |
|
47 | 56 | !isset($aFlippedStringTable[$cellValue]) && |
|
48 | 56 | ($cell->getDataType() == DataType::TYPE_STRING || $cell->getDataType() == DataType::TYPE_STRING2 || $cell->getDataType() == DataType::TYPE_NULL)) { |
|
49 | 50 | $aStringTable[] = $cellValue; |
|
50 | 50 | $aFlippedStringTable[$cellValue] = true; |
|
51 | 53 | } elseif ($cellValue instanceof RichText && |
|
52 | 53 | ($cellValue !== null) && |
|
53 | 53 | !isset($aFlippedStringTable[$cellValue->getHashCode()])) { |
|
54 | 10 | $aStringTable[] = $cellValue; |
|
55 | 56 | $aFlippedStringTable[$cellValue->getHashCode()] = true; |
|
56 | } |
||
57 | } |
||
58 | |||
59 | 56 | return $aStringTable; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Write string table to XML format. |
||
64 | * |
||
65 | * @param string[] $pStringTable |
||
66 | * |
||
67 | * @throws WriterException |
||
68 | * |
||
69 | * @return string XML Output |
||
70 | */ |
||
71 | 56 | public function writeStringTable(array $pStringTable) |
|
72 | { |
||
73 | // Create XML writer |
||
74 | 56 | $objWriter = null; |
|
75 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
76 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
77 | } else { |
||
78 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
79 | } |
||
80 | |||
81 | // XML header |
||
82 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
83 | |||
84 | // String table |
||
85 | 56 | $objWriter->startElement('sst'); |
|
86 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
87 | 56 | $objWriter->writeAttribute('uniqueCount', count($pStringTable)); |
|
88 | |||
89 | // Loop through string table |
||
90 | 56 | foreach ($pStringTable as $textElement) { |
|
91 | 51 | $objWriter->startElement('si'); |
|
92 | |||
93 | 51 | if (!$textElement instanceof RichText) { |
|
94 | 50 | $textToWrite = StringHelper::controlCharacterPHP2OOXML($textElement); |
|
95 | 50 | $objWriter->startElement('t'); |
|
96 | 50 | if ($textToWrite !== trim($textToWrite)) { |
|
97 | 2 | $objWriter->writeAttribute('xml:space', 'preserve'); |
|
98 | } |
||
99 | 50 | $objWriter->writeRawData($textToWrite); |
|
100 | 50 | $objWriter->endElement(); |
|
101 | 10 | } elseif ($textElement instanceof RichText) { |
|
102 | 10 | $this->writeRichText($objWriter, $textElement); |
|
103 | } |
||
104 | |||
105 | 51 | $objWriter->endElement(); |
|
106 | } |
||
107 | |||
108 | 56 | $objWriter->endElement(); |
|
109 | |||
110 | 56 | return $objWriter->getData(); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Write Rich Text. |
||
115 | * |
||
116 | * @param XMLWriter $objWriter XML Writer |
||
117 | * @param RichText $pRichText Rich text |
||
118 | * @param string $prefix Optional Namespace prefix |
||
119 | * |
||
120 | * @throws WriterException |
||
121 | */ |
||
122 | 13 | public function writeRichText(XMLWriter $objWriter, RichText $pRichText, $prefix = null) |
|
195 | } |
||
196 | 13 | } |
|
197 | |||
198 | /** |
||
199 | * Write Rich Text. |
||
200 | * |
||
201 | * @param XMLWriter $objWriter XML Writer |
||
202 | * @param RichText|string $pRichText text string or Rich text |
||
203 | * @param string $prefix Optional Namespace prefix |
||
204 | * |
||
205 | * @throws WriterException |
||
206 | */ |
||
207 | 13 | public function writeRichTextForCharts(XMLWriter $objWriter, $pRichText = null, $prefix = null) |
|
208 | { |
||
209 | 13 | if (!$pRichText instanceof RichText) { |
|
210 | 12 | $textRun = $pRichText; |
|
211 | 12 | $pRichText = new RichText(); |
|
212 | 12 | $pRichText->createTextRun($textRun); |
|
213 | } |
||
214 | |||
215 | 13 | if ($prefix !== null) { |
|
216 | 13 | $prefix .= ':'; |
|
217 | } |
||
218 | |||
219 | // Loop through rich text elements |
||
220 | 13 | $elements = $pRichText->getRichTextElements(); |
|
221 | 13 | foreach ($elements as $element) { |
|
222 | // r |
||
223 | 13 | $objWriter->startElement($prefix . 'r'); |
|
224 | |||
225 | // rPr |
||
226 | 13 | $objWriter->startElement($prefix . 'rPr'); |
|
227 | |||
228 | // Bold |
||
229 | 13 | $objWriter->writeAttribute('b', ($element->getFont()->getBold() ? 1 : 0)); |
|
230 | // Italic |
||
231 | 13 | $objWriter->writeAttribute('i', ($element->getFont()->getItalic() ? 1 : 0)); |
|
232 | // Underline |
||
233 | 13 | $underlineType = $element->getFont()->getUnderline(); |
|
234 | switch ($underlineType) { |
||
235 | 13 | case 'single': |
|
236 | 1 | $underlineType = 'sng'; |
|
237 | |||
238 | 1 | break; |
|
239 | 13 | case 'double': |
|
240 | 1 | $underlineType = 'dbl'; |
|
241 | |||
242 | 1 | break; |
|
243 | } |
||
244 | 13 | $objWriter->writeAttribute('u', $underlineType); |
|
245 | // Strikethrough |
||
246 | 13 | $objWriter->writeAttribute('strike', ($element->getFont()->getStrikethrough() ? 'sngStrike' : 'noStrike')); |
|
247 | |||
248 | // rFont |
||
249 | 13 | $objWriter->startElement($prefix . 'latin'); |
|
250 | 13 | $objWriter->writeAttribute('typeface', $element->getFont()->getName()); |
|
251 | 13 | $objWriter->endElement(); |
|
252 | |||
253 | 13 | $objWriter->endElement(); |
|
254 | |||
255 | // t |
||
256 | 13 | $objWriter->startElement($prefix . 't'); |
|
257 | 13 | $objWriter->writeRawData(StringHelper::controlCharacterPHP2OOXML($element->getText())); |
|
258 | 13 | $objWriter->endElement(); |
|
259 | |||
260 | 13 | $objWriter->endElement(); |
|
261 | } |
||
262 | 13 | } |
|
263 | |||
264 | /** |
||
265 | * Flip string table (for index searching). |
||
266 | * |
||
267 | * @param array $stringTable Stringtable |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | 56 | public function flipStringTable(array $stringTable) |
|
286 | } |
||
287 | } |
||
288 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: