Conditions | 24 |
Paths | > 20000 |
Total Lines | 143 |
Code Lines | 73 |
Lines | 5 |
Ratio | 3.5 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | $objWriter = null; |
||
|
|||
40 | View Code Duplication | if ($this->getParentWriter()->getUseDiskCaching()) { |
|
41 | $objWriter = new \PhpSpreadsheet\Shared\XMLWriter(\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
||
42 | } else { |
||
43 | $objWriter = new \PhpSpreadsheet\Shared\XMLWriter(\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY); |
||
44 | } |
||
45 | |||
46 | // XML header |
||
47 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
||
48 | |||
49 | // Types |
||
50 | $objWriter->startElement('Types'); |
||
51 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); |
||
52 | |||
53 | // Theme |
||
54 | $this->writeOverrideContentType($objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml'); |
||
55 | |||
56 | // Styles |
||
57 | $this->writeOverrideContentType($objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'); |
||
58 | |||
59 | // Rels |
||
60 | $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml'); |
||
61 | |||
62 | // XML |
||
63 | $this->writeDefaultContentType($objWriter, 'xml', 'application/xml'); |
||
64 | |||
65 | // VML |
||
66 | $this->writeDefaultContentType($objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing'); |
||
67 | |||
68 | // Workbook |
||
69 | if ($spreadsheet->hasMacros()) { //Macros in workbook ? |
||
70 | // Yes : not standard content but "macroEnabled" |
||
71 | $this->writeOverrideContentType($objWriter, '/xl/workbook.xml', 'application/vnd.ms-excel.sheet.macroEnabled.main+xml'); |
||
72 | //... and define a new type for the VBA project |
||
73 | $this->writeDefaultContentType($objWriter, 'bin', 'application/vnd.ms-office.vbaProject'); |
||
74 | if ($spreadsheet->hasMacrosCertificate()) {// signed macros ? |
||
75 | // Yes : add needed information |
||
76 | $this->writeOverrideContentType($objWriter, '/xl/vbaProjectSignature.bin', 'application/vnd.ms-office.vbaProjectSignature'); |
||
77 | } |
||
78 | } else {// no macros in workbook, so standard type |
||
79 | $this->writeOverrideContentType($objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'); |
||
80 | } |
||
81 | |||
82 | // DocProps |
||
83 | $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml'); |
||
84 | |||
85 | $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml'); |
||
86 | |||
87 | $customPropertyList = $spreadsheet->getProperties()->getCustomProperties(); |
||
88 | if (!empty($customPropertyList)) { |
||
89 | $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml'); |
||
90 | } |
||
91 | |||
92 | // Worksheets |
||
93 | $sheetCount = $spreadsheet->getSheetCount(); |
||
94 | for ($i = 0; $i < $sheetCount; ++$i) { |
||
95 | $this->writeOverrideContentType($objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'); |
||
96 | } |
||
97 | |||
98 | // Shared strings |
||
99 | $this->writeOverrideContentType($objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'); |
||
100 | |||
101 | // Add worksheet relationship content types |
||
102 | $chart = 1; |
||
103 | for ($i = 0; $i < $sheetCount; ++$i) { |
||
104 | $drawings = $spreadsheet->getSheet($i)->getDrawingCollection(); |
||
105 | $drawingCount = count($drawings); |
||
106 | $chartCount = ($includeCharts) ? $spreadsheet->getSheet($i)->getChartCount() : 0; |
||
107 | |||
108 | // We need a drawing relationship for the worksheet if we have either drawings or charts |
||
109 | if (($drawingCount > 0) || ($chartCount > 0)) { |
||
110 | $this->writeOverrideContentType($objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml'); |
||
111 | } |
||
112 | |||
113 | // If we have charts, then we need a chart relationship for every individual chart |
||
114 | if ($chartCount > 0) { |
||
115 | for ($c = 0; $c < $chartCount; ++$c) { |
||
116 | $this->writeOverrideContentType($objWriter, '/xl/charts/chart' . $chart++ . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml'); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // Comments |
||
122 | for ($i = 0; $i < $sheetCount; ++$i) { |
||
123 | if (count($spreadsheet->getSheet($i)->getComments()) > 0) { |
||
124 | $this->writeOverrideContentType($objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml'); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // Add media content-types |
||
129 | $aMediaContentTypes = array(); |
||
130 | $mediaCount = $this->getParentWriter()->getDrawingHashTable()->count(); |
||
131 | for ($i = 0; $i < $mediaCount; ++$i) { |
||
132 | $extension = ''; |
||
133 | $mimeType = ''; |
||
134 | |||
135 | if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\Drawing) { |
||
136 | $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension()); |
||
137 | $mimeType = $this->getImageMimeType($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath()); |
||
138 | } elseif ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\MemoryDrawing) { |
||
139 | $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType()); |
||
140 | $extension = explode('/', $extension); |
||
141 | $extension = $extension[1]; |
||
142 | |||
143 | $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType(); |
||
144 | } |
||
145 | |||
146 | if (!isset($aMediaContentTypes[$extension])) { |
||
147 | $aMediaContentTypes[$extension] = $mimeType; |
||
148 | |||
149 | $this->writeDefaultContentType($objWriter, $extension, $mimeType); |
||
150 | } |
||
151 | } |
||
152 | if ($spreadsheet->hasRibbonBinObjects()) { |
||
153 | // Some additional objects in the ribbon ? |
||
154 | // we need to write "Extension" but not already write for media content |
||
155 | $tabRibbonTypes=array_diff($spreadsheet->getRibbonBinObjects('types'), array_keys($aMediaContentTypes)); |
||
156 | foreach ($tabRibbonTypes as $aRibbonType) { |
||
157 | $mimeType='image/.'.$aRibbonType;//we wrote $mimeType like customUI Editor |
||
158 | $this->writeDefaultContentType($objWriter, $aRibbonType, $mimeType); |
||
159 | } |
||
160 | } |
||
161 | $sheetCount = $spreadsheet->getSheetCount(); |
||
162 | for ($i = 0; $i < $sheetCount; ++$i) { |
||
163 | if (count($spreadsheet->getSheet()->getHeaderFooter()->getImages()) > 0) { |
||
164 | foreach ($spreadsheet->getSheet()->getHeaderFooter()->getImages() as $image) { |
||
165 | if (!isset($aMediaContentTypes[strtolower($image->getExtension())])) { |
||
166 | $aMediaContentTypes[strtolower($image->getExtension())] = $this->getImageMimeType($image->getPath()); |
||
167 | |||
168 | $this->writeDefaultContentType($objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())]); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $objWriter->endElement(); |
||
175 | |||
176 | // Return |
||
177 | return $objWriter->getData(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get image mime type |
||
182 | * |
||
239 |
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.