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:
1 | <?php |
||
32 | class Rels extends WriterPart |
||
33 | { |
||
34 | /** |
||
35 | * Write relationships to XML format. |
||
36 | * |
||
37 | * @param Spreadsheet $spreadsheet |
||
38 | * |
||
39 | * @throws WriterException |
||
40 | * |
||
41 | * @return string XML Output |
||
42 | */ |
||
43 | 56 | public function writeRelationships(Spreadsheet $spreadsheet) |
|
44 | { |
||
45 | // Create XML writer |
||
46 | 56 | $objWriter = null; |
|
|
|||
47 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
48 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
49 | } else { |
||
50 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
51 | } |
||
52 | |||
53 | // XML header |
||
54 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
55 | |||
56 | // Relationships |
||
57 | 56 | $objWriter->startElement('Relationships'); |
|
58 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
59 | |||
60 | 56 | $customPropertyList = $spreadsheet->getProperties()->getCustomProperties(); |
|
61 | 56 | if (!empty($customPropertyList)) { |
|
62 | // Relationship docProps/app.xml |
||
63 | 2 | $this->writeRelationship( |
|
64 | 2 | $objWriter, |
|
65 | 2 | 4, |
|
66 | 2 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties', |
|
67 | 2 | 'docProps/custom.xml' |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | // Relationship docProps/app.xml |
||
72 | 56 | $this->writeRelationship( |
|
73 | 56 | $objWriter, |
|
74 | 56 | 3, |
|
75 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', |
|
76 | 56 | 'docProps/app.xml' |
|
77 | ); |
||
78 | |||
79 | // Relationship docProps/core.xml |
||
80 | 56 | $this->writeRelationship( |
|
81 | 56 | $objWriter, |
|
82 | 56 | 2, |
|
83 | 56 | 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', |
|
84 | 56 | 'docProps/core.xml' |
|
85 | ); |
||
86 | |||
87 | // Relationship xl/workbook.xml |
||
88 | 56 | $this->writeRelationship( |
|
89 | 56 | $objWriter, |
|
90 | 56 | 1, |
|
91 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', |
|
92 | 56 | 'xl/workbook.xml' |
|
93 | ); |
||
94 | // a custom UI in workbook ? |
||
95 | 56 | if ($spreadsheet->hasRibbon()) { |
|
96 | $this->writeRelationShip( |
||
97 | $objWriter, |
||
98 | 5, |
||
99 | 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility', |
||
100 | $spreadsheet->getRibbonXMLData('target') |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | 56 | $objWriter->endElement(); |
|
105 | |||
106 | 56 | return $objWriter->getData(); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Write workbook relationships to XML format. |
||
111 | * |
||
112 | * @param Spreadsheet $spreadsheet |
||
113 | * |
||
114 | * @throws WriterException |
||
115 | * |
||
116 | * @return string XML Output |
||
117 | */ |
||
118 | 56 | public function writeWorkbookRelationships(Spreadsheet $spreadsheet) |
|
119 | { |
||
120 | // Create XML writer |
||
121 | 56 | $objWriter = null; |
|
122 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
123 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
124 | } else { |
||
125 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
126 | } |
||
127 | |||
128 | // XML header |
||
129 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
130 | |||
131 | // Relationships |
||
132 | 56 | $objWriter->startElement('Relationships'); |
|
133 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
134 | |||
135 | // Relationship styles.xml |
||
136 | 56 | $this->writeRelationship( |
|
137 | 56 | $objWriter, |
|
138 | 56 | 1, |
|
139 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', |
|
140 | 56 | 'styles.xml' |
|
141 | ); |
||
142 | |||
143 | // Relationship theme/theme1.xml |
||
144 | 56 | $this->writeRelationship( |
|
145 | 56 | $objWriter, |
|
146 | 56 | 2, |
|
147 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', |
|
148 | 56 | 'theme/theme1.xml' |
|
149 | ); |
||
150 | |||
151 | // Relationship sharedStrings.xml |
||
152 | 56 | $this->writeRelationship( |
|
153 | 56 | $objWriter, |
|
154 | 56 | 3, |
|
155 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings', |
|
156 | 56 | 'sharedStrings.xml' |
|
157 | ); |
||
158 | |||
159 | // Relationships with sheets |
||
160 | 56 | $sheetCount = $spreadsheet->getSheetCount(); |
|
161 | 56 | for ($i = 0; $i < $sheetCount; ++$i) { |
|
162 | 56 | $this->writeRelationship( |
|
163 | 56 | $objWriter, |
|
164 | 56 | ($i + 1 + 3), |
|
165 | 56 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet', |
|
166 | 56 | 'worksheets/sheet' . ($i + 1) . '.xml' |
|
167 | ); |
||
168 | } |
||
169 | // Relationships for vbaProject if needed |
||
170 | // id : just after the last sheet |
||
171 | 56 | if ($spreadsheet->hasMacros()) { |
|
172 | $this->writeRelationShip( |
||
173 | $objWriter, |
||
174 | ($i + 1 + 3), |
||
175 | 'http://schemas.microsoft.com/office/2006/relationships/vbaProject', |
||
176 | 'vbaProject.bin' |
||
177 | ); |
||
178 | ++$i; //increment i if needed for an another relation |
||
179 | } |
||
180 | |||
181 | 56 | $objWriter->endElement(); |
|
182 | |||
183 | 56 | return $objWriter->getData(); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Write worksheet relationships to XML format. |
||
188 | * |
||
189 | * Numbering is as follows: |
||
190 | * rId1 - Drawings |
||
191 | * rId_hyperlink_x - Hyperlinks |
||
192 | * |
||
193 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet |
||
194 | * @param int $pWorksheetId |
||
195 | * @param bool $includeCharts Flag indicating if we should write charts |
||
196 | * |
||
197 | * @throws WriterException |
||
198 | * |
||
199 | * @return string XML Output |
||
200 | */ |
||
201 | 56 | public function writeWorksheetRelationships(\PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet, $pWorksheetId = 1, $includeCharts = false) |
|
202 | { |
||
203 | // Create XML writer |
||
204 | 56 | $objWriter = null; |
|
205 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
206 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
207 | } else { |
||
208 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
209 | } |
||
210 | |||
211 | // XML header |
||
212 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
213 | |||
214 | // Relationships |
||
215 | 56 | $objWriter->startElement('Relationships'); |
|
216 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
217 | |||
218 | // Write drawing relationships? |
||
219 | 56 | $d = 0; |
|
220 | 56 | if ($includeCharts) { |
|
221 | 14 | $charts = $pWorksheet->getChartCollection(); |
|
222 | } else { |
||
223 | 43 | $charts = []; |
|
224 | } |
||
225 | 56 | if (($pWorksheet->getDrawingCollection()->count() > 0) || |
|
226 | 56 | (count($charts) > 0)) { |
|
227 | 22 | $this->writeRelationship( |
|
228 | 22 | $objWriter, |
|
229 | 22 | ++$d, |
|
230 | 22 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing', |
|
231 | 22 | '../drawings/drawing' . $pWorksheetId . '.xml' |
|
232 | ); |
||
233 | } |
||
234 | |||
235 | // Write hyperlink relationships? |
||
236 | 56 | $i = 1; |
|
237 | 56 | foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) { |
|
238 | 9 | if (!$hyperlink->isInternal()) { |
|
239 | 9 | $this->writeRelationship( |
|
240 | 9 | $objWriter, |
|
241 | 9 | '_hyperlink_' . $i, |
|
242 | 9 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink', |
|
243 | 9 | $hyperlink->getUrl(), |
|
244 | 9 | 'External' |
|
245 | ); |
||
246 | |||
247 | 9 | ++$i; |
|
248 | } |
||
249 | } |
||
250 | |||
251 | // Write comments relationship? |
||
252 | 56 | $i = 1; |
|
253 | 56 | if (count($pWorksheet->getComments()) > 0) { |
|
254 | 9 | $this->writeRelationship( |
|
255 | 9 | $objWriter, |
|
256 | 9 | '_comments_vml' . $i, |
|
257 | 9 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing', |
|
258 | 9 | '../drawings/vmlDrawing' . $pWorksheetId . '.vml' |
|
259 | ); |
||
260 | |||
261 | 9 | $this->writeRelationship( |
|
262 | 9 | $objWriter, |
|
263 | 9 | '_comments' . $i, |
|
264 | 9 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments', |
|
265 | 9 | '../comments' . $pWorksheetId . '.xml' |
|
266 | ); |
||
267 | } |
||
268 | |||
269 | // Write header/footer relationship? |
||
270 | 56 | $i = 1; |
|
271 | 56 | if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) { |
|
272 | 1 | $this->writeRelationship( |
|
273 | 1 | $objWriter, |
|
274 | 1 | '_headerfooter_vml' . $i, |
|
275 | 1 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing', |
|
276 | 1 | '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml' |
|
277 | ); |
||
278 | } |
||
279 | |||
280 | 56 | $objWriter->endElement(); |
|
281 | |||
282 | 56 | return $objWriter->getData(); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * Write drawing relationships to XML format. |
||
287 | * |
||
288 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet |
||
289 | * @param int &$chartRef Chart ID |
||
290 | * @param bool $includeCharts Flag indicating if we should write charts |
||
291 | * |
||
292 | * @throws WriterException |
||
293 | * |
||
294 | * @return string XML Output |
||
295 | */ |
||
296 | 22 | public function writeDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet, &$chartRef, $includeCharts = false) |
|
297 | { |
||
298 | // Create XML writer |
||
299 | 22 | $objWriter = null; |
|
300 | 22 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
301 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
302 | } else { |
||
303 | 22 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
304 | } |
||
305 | |||
306 | // XML header |
||
307 | 22 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
308 | |||
309 | // Relationships |
||
310 | 22 | $objWriter->startElement('Relationships'); |
|
311 | 22 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
312 | |||
313 | // Loop through images and write relationships |
||
314 | 22 | $i = 1; |
|
315 | 22 | $iterator = $pWorksheet->getDrawingCollection()->getIterator(); |
|
316 | 22 | while ($iterator->valid()) { |
|
317 | 10 | if ($iterator->current() instanceof \PhpOffice\PhpSpreadsheet\Worksheet\Drawing |
|
318 | 10 | || $iterator->current() instanceof MemoryDrawing) { |
|
319 | // Write relationship for image drawing |
||
320 | 10 | $this->writeRelationship( |
|
321 | 10 | $objWriter, |
|
322 | 10 | $i, |
|
323 | 10 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', |
|
324 | 10 | '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename()) |
|
325 | ); |
||
326 | } |
||
327 | |||
328 | 10 | $iterator->next(); |
|
329 | 10 | ++$i; |
|
330 | } |
||
331 | |||
332 | 22 | if ($includeCharts) { |
|
333 | // Loop through charts and write relationships |
||
334 | 13 | $chartCount = $pWorksheet->getChartCount(); |
|
335 | 13 | if ($chartCount > 0) { |
|
336 | 13 | for ($c = 0; $c < $chartCount; ++$c) { |
|
337 | 13 | $this->writeRelationship( |
|
338 | 13 | $objWriter, |
|
339 | 13 | $i++, |
|
340 | 13 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart', |
|
341 | 13 | '../charts/chart' . ++$chartRef . '.xml' |
|
342 | ); |
||
343 | } |
||
344 | } |
||
345 | } |
||
346 | |||
347 | 22 | $objWriter->endElement(); |
|
348 | |||
349 | 22 | return $objWriter->getData(); |
|
350 | } |
||
351 | |||
352 | /** |
||
353 | * Write header/footer drawing relationships to XML format. |
||
354 | * |
||
355 | * @param \PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet |
||
356 | * |
||
357 | * @throws WriterException |
||
358 | * |
||
359 | * @return string XML Output |
||
360 | */ |
||
361 | 1 | public function writeHeaderFooterDrawingRelationships(\PhpOffice\PhpSpreadsheet\Worksheet $pWorksheet) |
|
362 | { |
||
363 | // Create XML writer |
||
364 | 1 | $objWriter = null; |
|
365 | 1 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
366 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
367 | } else { |
||
368 | 1 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
369 | } |
||
370 | |||
371 | // XML header |
||
372 | 1 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
373 | |||
374 | // Relationships |
||
375 | 1 | $objWriter->startElement('Relationships'); |
|
376 | 1 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
377 | |||
378 | // Loop through images and write relationships |
||
379 | 1 | foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) { |
|
380 | // Write relationship for image drawing |
||
381 | 1 | $this->writeRelationship( |
|
382 | 1 | $objWriter, |
|
383 | 1 | $key, |
|
384 | 1 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image', |
|
385 | 1 | '../media/' . $value->getIndexedFilename() |
|
386 | ); |
||
387 | } |
||
388 | |||
389 | 1 | $objWriter->endElement(); |
|
390 | |||
391 | 1 | return $objWriter->getData(); |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * Write Override content type. |
||
396 | * |
||
397 | * @param XMLWriter $objWriter XML Writer |
||
398 | * @param int $pId Relationship ID. rId will be prepended! |
||
399 | * @param string $pType Relationship type |
||
400 | * @param string $pTarget Relationship target |
||
401 | * @param string $pTargetMode Relationship target mode |
||
402 | * |
||
403 | * @throws WriterException |
||
404 | */ |
||
405 | 56 | View Code Duplication | private function writeRelationship(XMLWriter $objWriter, $pId, $pType, $pTarget, $pTargetMode = '') |
423 | } |
||
424 |
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.