Total Complexity | 97 |
Total Lines | 697 |
Duplicated Lines | 11.19 % |
Coverage | 92.41% |
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 Style 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 Style, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Style extends WriterPart |
||
16 | { |
||
17 | /** |
||
18 | * Write styles to XML format. |
||
19 | * |
||
20 | * @param Spreadsheet $spreadsheet |
||
21 | * |
||
22 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
23 | * |
||
24 | * @return string XML Output |
||
25 | */ |
||
26 | 56 | public function writeStyles(Spreadsheet $spreadsheet) |
|
27 | { |
||
28 | // Create XML writer |
||
29 | 56 | $objWriter = null; |
|
30 | 56 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
|
|||
31 | $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
32 | } else { |
||
33 | 56 | $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
|
34 | } |
||
35 | |||
36 | // XML header |
||
37 | 56 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
|
38 | |||
39 | // styleSheet |
||
40 | 56 | $objWriter->startElement('styleSheet'); |
|
41 | 56 | $objWriter->writeAttribute('xml:space', 'preserve'); |
|
42 | 56 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
43 | |||
44 | // numFmts |
||
45 | 56 | $objWriter->startElement('numFmts'); |
|
46 | 56 | $objWriter->writeAttribute('count', $this->getParentWriter()->getNumFmtHashTable()->count()); |
|
47 | |||
48 | // numFmt |
||
49 | 56 | for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) { |
|
50 | 15 | $this->writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i); |
|
51 | } |
||
52 | |||
53 | 56 | $objWriter->endElement(); |
|
54 | |||
55 | // fonts |
||
56 | 56 | $objWriter->startElement('fonts'); |
|
57 | 56 | $objWriter->writeAttribute('count', $this->getParentWriter()->getFontHashTable()->count()); |
|
58 | |||
59 | // font |
||
60 | 56 | for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) { |
|
61 | 56 | $this->writeFont($objWriter, $this->getParentWriter()->getFontHashTable()->getByIndex($i)); |
|
62 | } |
||
63 | |||
64 | 56 | $objWriter->endElement(); |
|
65 | |||
66 | // fills |
||
67 | 56 | $objWriter->startElement('fills'); |
|
68 | 56 | $objWriter->writeAttribute('count', $this->getParentWriter()->getFillHashTable()->count()); |
|
69 | |||
70 | // fill |
||
71 | 56 | for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) { |
|
72 | 56 | $this->writeFill($objWriter, $this->getParentWriter()->getFillHashTable()->getByIndex($i)); |
|
73 | } |
||
74 | |||
75 | 56 | $objWriter->endElement(); |
|
76 | |||
77 | // borders |
||
78 | 56 | $objWriter->startElement('borders'); |
|
79 | 56 | $objWriter->writeAttribute('count', $this->getParentWriter()->getBordersHashTable()->count()); |
|
80 | |||
81 | // border |
||
82 | 56 | for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) { |
|
83 | 56 | $this->writeBorder($objWriter, $this->getParentWriter()->getBordersHashTable()->getByIndex($i)); |
|
84 | } |
||
85 | |||
86 | 56 | $objWriter->endElement(); |
|
87 | |||
88 | // cellStyleXfs |
||
89 | 56 | $objWriter->startElement('cellStyleXfs'); |
|
90 | 56 | $objWriter->writeAttribute('count', 1); |
|
91 | |||
92 | // xf |
||
93 | 56 | $objWriter->startElement('xf'); |
|
94 | 56 | $objWriter->writeAttribute('numFmtId', 0); |
|
95 | 56 | $objWriter->writeAttribute('fontId', 0); |
|
96 | 56 | $objWriter->writeAttribute('fillId', 0); |
|
97 | 56 | $objWriter->writeAttribute('borderId', 0); |
|
98 | 56 | $objWriter->endElement(); |
|
99 | |||
100 | 56 | $objWriter->endElement(); |
|
101 | |||
102 | // cellXfs |
||
103 | 56 | $objWriter->startElement('cellXfs'); |
|
104 | 56 | $objWriter->writeAttribute('count', count($spreadsheet->getCellXfCollection())); |
|
105 | |||
106 | // xf |
||
107 | 56 | foreach ($spreadsheet->getCellXfCollection() as $cellXf) { |
|
108 | 56 | $this->writeCellStyleXf($objWriter, $cellXf, $spreadsheet); |
|
109 | } |
||
110 | |||
111 | 56 | $objWriter->endElement(); |
|
112 | |||
113 | // cellStyles |
||
114 | 56 | $objWriter->startElement('cellStyles'); |
|
115 | 56 | $objWriter->writeAttribute('count', 1); |
|
116 | |||
117 | // cellStyle |
||
118 | 56 | $objWriter->startElement('cellStyle'); |
|
119 | 56 | $objWriter->writeAttribute('name', 'Normal'); |
|
120 | 56 | $objWriter->writeAttribute('xfId', 0); |
|
121 | 56 | $objWriter->writeAttribute('builtinId', 0); |
|
122 | 56 | $objWriter->endElement(); |
|
123 | |||
124 | 56 | $objWriter->endElement(); |
|
125 | |||
126 | // dxfs |
||
127 | 56 | $objWriter->startElement('dxfs'); |
|
128 | 56 | $objWriter->writeAttribute('count', $this->getParentWriter()->getStylesConditionalHashTable()->count()); |
|
129 | |||
130 | // dxf |
||
131 | 56 | for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) { |
|
132 | 2 | $this->writeCellStyleDxf($objWriter, $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i)->getStyle()); |
|
133 | } |
||
134 | |||
135 | 56 | $objWriter->endElement(); |
|
136 | |||
137 | // tableStyles |
||
138 | 56 | $objWriter->startElement('tableStyles'); |
|
139 | 56 | $objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9'); |
|
140 | 56 | $objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1'); |
|
141 | 56 | $objWriter->endElement(); |
|
142 | |||
143 | 56 | $objWriter->endElement(); |
|
144 | |||
145 | // Return |
||
146 | 56 | return $objWriter->getData(); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Write Fill. |
||
151 | * |
||
152 | * @param XMLWriter $objWriter XML Writer |
||
153 | * @param Fill $pFill Fill style |
||
154 | * |
||
155 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
156 | */ |
||
157 | 56 | private function writeFill(XMLWriter $objWriter, Fill $pFill) |
|
158 | { |
||
159 | // Check if this is a pattern type or gradient type |
||
160 | 56 | if ($pFill->getFillType() === Fill::FILL_GRADIENT_LINEAR || |
|
161 | 56 | $pFill->getFillType() === Fill::FILL_GRADIENT_PATH) { |
|
162 | // Gradient fill |
||
163 | 5 | $this->writeGradientFill($objWriter, $pFill); |
|
164 | 56 | } elseif ($pFill->getFillType() !== null) { |
|
165 | // Pattern fill |
||
166 | 56 | $this->writePatternFill($objWriter, $pFill); |
|
167 | } |
||
168 | 56 | } |
|
169 | |||
170 | /** |
||
171 | * Write Gradient Fill. |
||
172 | * |
||
173 | * @param XMLWriter $objWriter XML Writer |
||
174 | * @param Fill $pFill Fill style |
||
175 | * |
||
176 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
177 | */ |
||
178 | 5 | private function writeGradientFill(XMLWriter $objWriter, Fill $pFill) |
|
213 | 5 | } |
|
214 | |||
215 | /** |
||
216 | * Write Pattern Fill. |
||
217 | * |
||
218 | * @param XMLWriter $objWriter XML Writer |
||
219 | * @param Fill $pFill Fill style |
||
220 | * |
||
221 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
222 | */ |
||
223 | 56 | private function writePatternFill(XMLWriter $objWriter, Fill $pFill) |
|
224 | { |
||
225 | // fill |
||
226 | 56 | $objWriter->startElement('fill'); |
|
227 | |||
228 | // patternFill |
||
229 | 56 | $objWriter->startElement('patternFill'); |
|
230 | 56 | $objWriter->writeAttribute('patternType', $pFill->getFillType()); |
|
231 | |||
232 | 56 | View Code Duplication | if ($pFill->getFillType() !== Fill::FILL_NONE) { |
233 | // fgColor |
||
234 | 56 | if ($pFill->getStartColor()->getARGB()) { |
|
235 | 56 | $objWriter->startElement('fgColor'); |
|
236 | 56 | $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB()); |
|
237 | 56 | $objWriter->endElement(); |
|
238 | } |
||
239 | } |
||
240 | 56 | View Code Duplication | if ($pFill->getFillType() !== Fill::FILL_NONE) { |
241 | // bgColor |
||
242 | 56 | if ($pFill->getEndColor()->getARGB()) { |
|
243 | 56 | $objWriter->startElement('bgColor'); |
|
244 | 56 | $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB()); |
|
245 | 56 | $objWriter->endElement(); |
|
246 | } |
||
247 | } |
||
248 | |||
249 | 56 | $objWriter->endElement(); |
|
250 | |||
251 | 56 | $objWriter->endElement(); |
|
252 | 56 | } |
|
253 | |||
254 | /** |
||
255 | * Write Font. |
||
256 | * |
||
257 | * @param XMLWriter $objWriter XML Writer |
||
258 | * @param Font $pFont Font style |
||
259 | * |
||
260 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
261 | */ |
||
262 | 56 | private function writeFont(XMLWriter $objWriter, Font $pFont) |
|
333 | 56 | } |
|
334 | |||
335 | /** |
||
336 | * Write Border. |
||
337 | * |
||
338 | * @param XMLWriter $objWriter XML Writer |
||
339 | * @param Borders $pBorders Borders style |
||
340 | * |
||
341 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
342 | */ |
||
343 | 56 | private function writeBorder(XMLWriter $objWriter, Borders $pBorders) |
|
344 | { |
||
345 | // Write border |
||
346 | 56 | $objWriter->startElement('border'); |
|
347 | // Diagonal? |
||
348 | 56 | switch ($pBorders->getDiagonalDirection()) { |
|
349 | 56 | case Borders::DIAGONAL_UP: |
|
350 | 1 | $objWriter->writeAttribute('diagonalUp', 'true'); |
|
351 | 1 | $objWriter->writeAttribute('diagonalDown', 'false'); |
|
352 | |||
353 | 1 | break; |
|
354 | 56 | case Borders::DIAGONAL_DOWN: |
|
355 | 1 | $objWriter->writeAttribute('diagonalUp', 'false'); |
|
356 | 1 | $objWriter->writeAttribute('diagonalDown', 'true'); |
|
357 | |||
358 | 1 | break; |
|
359 | 56 | case Borders::DIAGONAL_BOTH: |
|
360 | 1 | $objWriter->writeAttribute('diagonalUp', 'true'); |
|
361 | 1 | $objWriter->writeAttribute('diagonalDown', 'true'); |
|
362 | |||
363 | 1 | break; |
|
364 | } |
||
365 | |||
366 | // BorderPr |
||
367 | 56 | $this->writeBorderPr($objWriter, 'left', $pBorders->getLeft()); |
|
368 | 56 | $this->writeBorderPr($objWriter, 'right', $pBorders->getRight()); |
|
369 | 56 | $this->writeBorderPr($objWriter, 'top', $pBorders->getTop()); |
|
370 | 56 | $this->writeBorderPr($objWriter, 'bottom', $pBorders->getBottom()); |
|
371 | 56 | $this->writeBorderPr($objWriter, 'diagonal', $pBorders->getDiagonal()); |
|
372 | 56 | $objWriter->endElement(); |
|
373 | 56 | } |
|
374 | |||
375 | /** |
||
376 | * Write Cell Style Xf. |
||
377 | * |
||
378 | * @param XMLWriter $objWriter XML Writer |
||
379 | * @param \PhpOffice\PhpSpreadsheet\Style\Style $pStyle Style |
||
380 | * @param Spreadsheet $spreadsheet Workbook |
||
381 | * |
||
382 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
383 | */ |
||
384 | 56 | private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $pStyle, Spreadsheet $spreadsheet) |
|
385 | { |
||
386 | // xf |
||
387 | 56 | $objWriter->startElement('xf'); |
|
388 | 56 | $objWriter->writeAttribute('xfId', 0); |
|
389 | 56 | $objWriter->writeAttribute('fontId', (int) $this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode())); |
|
390 | 56 | if ($pStyle->getQuotePrefix()) { |
|
391 | 1 | $objWriter->writeAttribute('quotePrefix', 1); |
|
392 | } |
||
393 | |||
394 | 56 | if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) { |
|
395 | 15 | $objWriter->writeAttribute('numFmtId', (int) ($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164)); |
|
396 | } else { |
||
397 | 56 | $objWriter->writeAttribute('numFmtId', (int) $pStyle->getNumberFormat()->getBuiltInFormatCode()); |
|
398 | } |
||
399 | |||
400 | 56 | $objWriter->writeAttribute('fillId', (int) $this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode())); |
|
401 | 56 | $objWriter->writeAttribute('borderId', (int) $this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode())); |
|
402 | |||
403 | // Apply styles? |
||
404 | 56 | $objWriter->writeAttribute('applyFont', ($spreadsheet->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0'); |
|
405 | 56 | $objWriter->writeAttribute('applyNumberFormat', ($spreadsheet->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0'); |
|
406 | 56 | $objWriter->writeAttribute('applyFill', ($spreadsheet->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0'); |
|
407 | 56 | $objWriter->writeAttribute('applyBorder', ($spreadsheet->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0'); |
|
408 | 56 | $objWriter->writeAttribute('applyAlignment', ($spreadsheet->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0'); |
|
409 | 56 | View Code Duplication | if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
410 | 9 | $objWriter->writeAttribute('applyProtection', 'true'); |
|
411 | } |
||
412 | |||
413 | // alignment |
||
414 | 56 | $objWriter->startElement('alignment'); |
|
415 | 56 | $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal()); |
|
416 | 56 | $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical()); |
|
417 | |||
418 | 56 | $textRotation = 0; |
|
419 | 56 | View Code Duplication | if ($pStyle->getAlignment()->getTextRotation() >= 0) { |
420 | 56 | $textRotation = $pStyle->getAlignment()->getTextRotation(); |
|
421 | } elseif ($pStyle->getAlignment()->getTextRotation() < 0) { |
||
422 | $textRotation = 90 - $pStyle->getAlignment()->getTextRotation(); |
||
423 | } |
||
424 | 56 | $objWriter->writeAttribute('textRotation', $textRotation); |
|
425 | |||
426 | 56 | $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false')); |
|
427 | 56 | $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false')); |
|
428 | |||
429 | 56 | if ($pStyle->getAlignment()->getIndent() > 0) { |
|
430 | $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent()); |
||
431 | } |
||
432 | 56 | if ($pStyle->getAlignment()->getReadOrder() > 0) { |
|
433 | $objWriter->writeAttribute('readingOrder', $pStyle->getAlignment()->getReadOrder()); |
||
434 | } |
||
435 | 56 | $objWriter->endElement(); |
|
436 | |||
437 | // protection |
||
438 | 56 | if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
|
439 | 9 | $objWriter->startElement('protection'); |
|
440 | 9 | View Code Duplication | if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT) { |
441 | 8 | $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
|
442 | } |
||
443 | 9 | View Code Duplication | if ($pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
444 | 3 | $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
|
445 | } |
||
446 | 9 | $objWriter->endElement(); |
|
447 | } |
||
448 | |||
449 | 56 | $objWriter->endElement(); |
|
450 | 56 | } |
|
451 | |||
452 | /** |
||
453 | * Write Cell Style Dxf. |
||
454 | * |
||
455 | * @param XMLWriter $objWriter XML Writer |
||
456 | * @param \PhpOffice\PhpSpreadsheet\Style\Style $pStyle Style |
||
457 | * |
||
458 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
459 | */ |
||
460 | 2 | private function writeCellStyleDxf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $pStyle) |
|
461 | { |
||
462 | // dxf |
||
463 | 2 | $objWriter->startElement('dxf'); |
|
464 | |||
465 | // font |
||
466 | 2 | $this->writeFont($objWriter, $pStyle->getFont()); |
|
467 | |||
468 | // numFmt |
||
469 | 2 | $this->writeNumFmt($objWriter, $pStyle->getNumberFormat()); |
|
470 | |||
471 | // fill |
||
472 | 2 | $this->writeFill($objWriter, $pStyle->getFill()); |
|
473 | |||
474 | // alignment |
||
475 | 2 | $objWriter->startElement('alignment'); |
|
476 | 2 | if ($pStyle->getAlignment()->getHorizontal() !== null) { |
|
477 | $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal()); |
||
478 | } |
||
479 | 2 | if ($pStyle->getAlignment()->getVertical() !== null) { |
|
480 | $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical()); |
||
481 | } |
||
482 | |||
483 | 2 | if ($pStyle->getAlignment()->getTextRotation() !== null) { |
|
484 | $textRotation = 0; |
||
485 | View Code Duplication | if ($pStyle->getAlignment()->getTextRotation() >= 0) { |
|
486 | $textRotation = $pStyle->getAlignment()->getTextRotation(); |
||
487 | } elseif ($pStyle->getAlignment()->getTextRotation() < 0) { |
||
488 | $textRotation = 90 - $pStyle->getAlignment()->getTextRotation(); |
||
489 | } |
||
490 | $objWriter->writeAttribute('textRotation', $textRotation); |
||
491 | } |
||
492 | 2 | $objWriter->endElement(); |
|
493 | |||
494 | // border |
||
495 | 2 | $this->writeBorder($objWriter, $pStyle->getBorders()); |
|
496 | |||
497 | // protection |
||
498 | 2 | if (($pStyle->getProtection()->getLocked() !== null) || ($pStyle->getProtection()->getHidden() !== null)) { |
|
499 | if ($pStyle->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT || |
||
500 | $pStyle->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT) { |
||
501 | $objWriter->startElement('protection'); |
||
502 | View Code Duplication | if (($pStyle->getProtection()->getLocked() !== null) && |
|
503 | ($pStyle->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT)) { |
||
504 | $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
||
505 | } |
||
506 | View Code Duplication | if (($pStyle->getProtection()->getHidden() !== null) && |
|
507 | ($pStyle->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT)) { |
||
508 | $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
||
509 | } |
||
510 | $objWriter->endElement(); |
||
511 | } |
||
512 | } |
||
513 | |||
514 | 2 | $objWriter->endElement(); |
|
515 | 2 | } |
|
516 | |||
517 | /** |
||
518 | * Write BorderPr. |
||
519 | * |
||
520 | * @param XMLWriter $objWriter XML Writer |
||
521 | * @param string $pName Element name |
||
522 | * @param Border $pBorder Border style |
||
523 | * |
||
524 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
525 | */ |
||
526 | 56 | private function writeBorderPr(XMLWriter $objWriter, $pName, Border $pBorder) |
|
527 | { |
||
528 | // Write BorderPr |
||
529 | 56 | if ($pBorder->getBorderStyle() != Border::BORDER_NONE) { |
|
530 | 12 | $objWriter->startElement($pName); |
|
531 | 12 | $objWriter->writeAttribute('style', $pBorder->getBorderStyle()); |
|
532 | |||
533 | // color |
||
534 | 12 | $objWriter->startElement('color'); |
|
535 | 12 | $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB()); |
|
536 | 12 | $objWriter->endElement(); |
|
537 | |||
538 | 12 | $objWriter->endElement(); |
|
539 | } |
||
540 | 56 | } |
|
541 | |||
542 | /** |
||
543 | * Write NumberFormat. |
||
544 | * |
||
545 | * @param XMLWriter $objWriter XML Writer |
||
546 | * @param NumberFormat $pNumberFormat Number Format |
||
547 | * @param int $pId Number Format identifier |
||
548 | * |
||
549 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
550 | */ |
||
551 | 17 | private function writeNumFmt(XMLWriter $objWriter, NumberFormat $pNumberFormat, $pId = 0) |
|
552 | { |
||
553 | // Translate formatcode |
||
554 | 17 | $formatCode = $pNumberFormat->getFormatCode(); |
|
555 | |||
556 | // numFmt |
||
557 | 17 | if ($formatCode !== null) { |
|
558 | 16 | $objWriter->startElement('numFmt'); |
|
559 | 16 | $objWriter->writeAttribute('numFmtId', ($pId + 164)); |
|
560 | 16 | $objWriter->writeAttribute('formatCode', $formatCode); |
|
561 | 16 | $objWriter->endElement(); |
|
562 | } |
||
563 | 17 | } |
|
564 | |||
565 | /** |
||
566 | * Get an array of all styles. |
||
567 | * |
||
568 | * @param Spreadsheet $spreadsheet |
||
569 | * |
||
570 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
571 | * |
||
572 | * @return \PhpOffice\PhpSpreadsheet\Style\Style[] All styles in PhpSpreadsheet |
||
573 | */ |
||
574 | 56 | public function allStyles(Spreadsheet $spreadsheet) |
|
575 | { |
||
576 | 56 | return $spreadsheet->getCellXfCollection(); |
|
577 | } |
||
578 | |||
579 | /** |
||
580 | * Get an array of all conditional styles. |
||
581 | * |
||
582 | * @param Spreadsheet $spreadsheet |
||
583 | * |
||
584 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
585 | * |
||
586 | * @return Conditional[] All conditional styles in PhpSpreadsheet |
||
587 | */ |
||
588 | 56 | public function allConditionalStyles(Spreadsheet $spreadsheet) |
|
589 | { |
||
590 | // Get an array of all styles |
||
591 | 56 | $aStyles = []; |
|
592 | |||
593 | 56 | $sheetCount = $spreadsheet->getSheetCount(); |
|
594 | 56 | for ($i = 0; $i < $sheetCount; ++$i) { |
|
595 | 56 | foreach ($spreadsheet->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) { |
|
596 | 2 | foreach ($conditionalStyles as $conditionalStyle) { |
|
597 | 2 | $aStyles[] = $conditionalStyle; |
|
598 | } |
||
599 | } |
||
600 | } |
||
601 | |||
602 | 56 | return $aStyles; |
|
603 | } |
||
604 | |||
605 | /** |
||
606 | * Get an array of all fills. |
||
607 | * |
||
608 | * @param Spreadsheet $spreadsheet |
||
609 | * |
||
610 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
611 | * |
||
612 | * @return Fill[] All fills in PhpSpreadsheet |
||
613 | */ |
||
614 | 56 | public function allFills(Spreadsheet $spreadsheet) |
|
615 | { |
||
616 | // Get an array of unique fills |
||
617 | 56 | $aFills = []; |
|
618 | |||
619 | // Two first fills are predefined |
||
620 | 56 | $fill0 = new Fill(); |
|
621 | 56 | $fill0->setFillType(Fill::FILL_NONE); |
|
622 | 56 | $aFills[] = $fill0; |
|
623 | |||
624 | 56 | $fill1 = new Fill(); |
|
625 | 56 | $fill1->setFillType(Fill::FILL_PATTERN_GRAY125); |
|
626 | 56 | $aFills[] = $fill1; |
|
627 | // The remaining fills |
||
628 | 56 | $aStyles = $this->allStyles($spreadsheet); |
|
629 | /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */ |
||
630 | 56 | foreach ($aStyles as $style) { |
|
631 | 56 | if (!isset($aFills[$style->getFill()->getHashCode()])) { |
|
632 | 56 | $aFills[$style->getFill()->getHashCode()] = $style->getFill(); |
|
633 | } |
||
634 | } |
||
635 | |||
636 | 56 | return $aFills; |
|
637 | } |
||
638 | |||
639 | /** |
||
640 | * Get an array of all fonts. |
||
641 | * |
||
642 | * @param Spreadsheet $spreadsheet |
||
643 | * |
||
644 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
645 | * |
||
646 | * @return Font[] All fonts in PhpSpreadsheet |
||
647 | */ |
||
648 | 56 | View Code Duplication | public function allFonts(Spreadsheet $spreadsheet) |
649 | { |
||
650 | // Get an array of unique fonts |
||
651 | 56 | $aFonts = []; |
|
652 | 56 | $aStyles = $this->allStyles($spreadsheet); |
|
653 | |||
654 | /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */ |
||
655 | 56 | foreach ($aStyles as $style) { |
|
656 | 56 | if (!isset($aFonts[$style->getFont()->getHashCode()])) { |
|
657 | 56 | $aFonts[$style->getFont()->getHashCode()] = $style->getFont(); |
|
658 | } |
||
659 | } |
||
660 | |||
661 | 56 | return $aFonts; |
|
662 | } |
||
663 | |||
664 | /** |
||
665 | * Get an array of all borders. |
||
666 | * |
||
667 | * @param Spreadsheet $spreadsheet |
||
668 | * |
||
669 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
670 | * |
||
671 | * @return Borders[] All borders in PhpSpreadsheet |
||
672 | */ |
||
673 | 56 | View Code Duplication | public function allBorders(Spreadsheet $spreadsheet) |
687 | } |
||
688 | |||
689 | /** |
||
690 | * Get an array of all number formats. |
||
691 | * |
||
692 | * @param Spreadsheet $spreadsheet |
||
693 | * |
||
694 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
695 | * |
||
696 | * @return NumberFormat[] All number formats in PhpSpreadsheet |
||
697 | */ |
||
698 | 56 | public function allNumberFormats(Spreadsheet $spreadsheet) |
|
712 | } |
||
713 | } |
||
714 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.