Conditions | 36 |
Paths | > 20000 |
Total Lines | 232 |
Code Lines | 127 |
Lines | 0 |
Ratio | 0 % |
Tests | 112 |
CRAP Score | 37.4578 |
Changes | 3 | ||
Bugs | 0 | 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 |
||
176 | 106 | public function save($pFilename) |
|
177 | { |
||
178 | 106 | if ($this->spreadSheet !== null) { |
|
179 | // garbage collect |
||
180 | 106 | $this->spreadSheet->garbageCollect(); |
|
181 | |||
182 | 106 | $this->openFileHandle($pFilename); |
|
183 | |||
184 | 106 | $saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog(); |
|
185 | 106 | Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false); |
|
186 | 106 | $saveDateReturnType = Functions::getReturnDateType(); |
|
187 | 106 | Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); |
|
188 | |||
189 | // Create string lookup table |
||
190 | 106 | $this->stringTable = []; |
|
191 | 106 | for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
|
192 | 106 | $this->stringTable = $this->getWriterPart('StringTable')->createStringTable($this->spreadSheet->getSheet($i), $this->stringTable); |
|
|
|||
193 | } |
||
194 | |||
195 | // Create styles dictionaries |
||
196 | 106 | $this->styleHashTable->addFromSource($this->getWriterPart('Style')->allStyles($this->spreadSheet)); |
|
197 | 106 | $this->stylesConditionalHashTable->addFromSource($this->getWriterPart('Style')->allConditionalStyles($this->spreadSheet)); |
|
198 | 106 | $this->fillHashTable->addFromSource($this->getWriterPart('Style')->allFills($this->spreadSheet)); |
|
199 | 106 | $this->fontHashTable->addFromSource($this->getWriterPart('Style')->allFonts($this->spreadSheet)); |
|
200 | 106 | $this->bordersHashTable->addFromSource($this->getWriterPart('Style')->allBorders($this->spreadSheet)); |
|
201 | 106 | $this->numFmtHashTable->addFromSource($this->getWriterPart('Style')->allNumberFormats($this->spreadSheet)); |
|
202 | |||
203 | // Create drawing dictionary |
||
204 | 106 | $this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet)); |
|
205 | |||
206 | 106 | $options = new Archive(); |
|
207 | 106 | $options->setEnableZip64(false); |
|
208 | 106 | $options->setOutputStream($this->fileHandle); |
|
209 | |||
210 | 106 | $zip = new ZipStream(null, $options); |
|
211 | |||
212 | // Add [Content_Types].xml to ZIP file |
||
213 | 106 | $zip->addFile('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts)); |
|
214 | |||
215 | //if hasMacros, add the vbaProject.bin file, Certificate file(if exists) |
||
216 | 106 | if ($this->spreadSheet->hasMacros()) { |
|
217 | 1 | $macrosCode = $this->spreadSheet->getMacrosCode(); |
|
218 | 1 | if ($macrosCode !== null) { |
|
219 | // we have the code ? |
||
220 | 1 | $zip->addFile('xl/vbaProject.bin', $macrosCode); //allways in 'xl', allways named vbaProject.bin |
|
221 | 1 | if ($this->spreadSheet->hasMacrosCertificate()) { |
|
222 | //signed macros ? |
||
223 | // Yes : add the certificate file and the related rels file |
||
224 | $zip->addFile('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate()); |
||
225 | $zip->addFile('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet)); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | //a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels) |
||
230 | 106 | if ($this->spreadSheet->hasRibbon()) { |
|
231 | $tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target'); |
||
232 | $zip->addFile($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data')); |
||
233 | if ($this->spreadSheet->hasRibbonBinObjects()) { |
||
234 | $tmpRootPath = dirname($tmpRibbonTarget) . '/'; |
||
235 | $ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write |
||
236 | foreach ($ribbonBinObjects as $aPath => $aContent) { |
||
237 | $zip->addFile($tmpRootPath . $aPath, $aContent); |
||
238 | } |
||
239 | //the rels for files |
||
240 | $zip->addFile($tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet)); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | // Add relationships to ZIP file |
||
245 | 106 | $zip->addFile('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet)); |
|
246 | 106 | $zip->addFile('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet)); |
|
247 | |||
248 | // Add document properties to ZIP file |
||
249 | 106 | $zip->addFile('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet)); |
|
250 | 106 | $zip->addFile('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet)); |
|
251 | 106 | $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet); |
|
252 | 106 | if ($customPropertiesPart !== null) { |
|
253 | 2 | $zip->addFile('docProps/custom.xml', $customPropertiesPart); |
|
254 | } |
||
255 | |||
256 | // Add theme to ZIP file |
||
257 | 106 | $zip->addFile('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet)); |
|
258 | |||
259 | // Add string table to ZIP file |
||
260 | 106 | $zip->addFile('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable)); |
|
261 | |||
262 | // Add styles to ZIP file |
||
263 | 106 | $zip->addFile('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet)); |
|
264 | |||
265 | // Add workbook to ZIP file |
||
266 | 106 | $zip->addFile('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas)); |
|
267 | |||
268 | 106 | $chartCount = 0; |
|
269 | // Add worksheets |
||
270 | 106 | for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
|
271 | 106 | $zip->addFile('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts)); |
|
272 | 106 | if ($this->includeCharts) { |
|
273 | 15 | $charts = $this->spreadSheet->getSheet($i)->getChartCollection(); |
|
274 | 15 | if (count($charts) > 0) { |
|
275 | 14 | foreach ($charts as $chart) { |
|
276 | 14 | $zip->addFile('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas)); |
|
277 | 14 | ++$chartCount; |
|
278 | } |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | 106 | $chartRef1 = 0; |
|
284 | // Add worksheet relationships (drawings, ...) |
||
285 | 106 | for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { |
|
286 | // Add relationships |
||
287 | 106 | $zip->addFile('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts)); |
|
288 | |||
289 | // Add unparsedLoadedData |
||
290 | 106 | $sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName(); |
|
291 | 106 | $unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData(); |
|
292 | 106 | if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'])) { |
|
293 | 1 | foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) { |
|
294 | 1 | $zip->addFile($ctrlProp['filePath'], $ctrlProp['content']); |
|
295 | } |
||
296 | } |
||
297 | 106 | if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'])) { |
|
298 | 4 | foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) { |
|
299 | 4 | $zip->addFile($ctrlProp['filePath'], $ctrlProp['content']); |
|
300 | } |
||
301 | } |
||
302 | |||
303 | 106 | $drawings = $this->spreadSheet->getSheet($i)->getDrawingCollection(); |
|
304 | 106 | $drawingCount = count($drawings); |
|
305 | 106 | if ($this->includeCharts) { |
|
306 | 15 | $chartCount = $this->spreadSheet->getSheet($i)->getChartCount(); |
|
307 | } |
||
308 | |||
309 | // Add drawing and image relationship parts |
||
310 | 106 | if (($drawingCount > 0) || ($chartCount > 0)) { |
|
311 | // Drawing relationships |
||
312 | 25 | $zip->addFile('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts)); |
|
313 | |||
314 | // Drawings |
||
315 | 25 | $zip->addFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); |
|
316 | 82 | } elseif (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingAlternateContents'])) { |
|
317 | // Drawings |
||
318 | 1 | $zip->addFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); |
|
319 | } |
||
320 | |||
321 | // Add unparsed drawings |
||
322 | 106 | if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['Drawings'])) { |
|
323 | 2 | foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['Drawings'] as $relId => $drawingXml) { |
|
324 | 2 | $drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']); |
|
325 | 2 | if ($drawingFile !== false) { |
|
326 | 2 | $drawingFile = ltrim($drawingFile, '.'); |
|
327 | 2 | $zip->addFile('xl' . $drawingFile, $drawingXml); |
|
328 | } |
||
329 | } |
||
330 | } |
||
331 | |||
332 | // Add comment relationship parts |
||
333 | 106 | if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) { |
|
334 | // VML Comments |
||
335 | 10 | $zip->addFile('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i))); |
|
336 | |||
337 | // Comments |
||
338 | 10 | $zip->addFile('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i))); |
|
339 | } |
||
340 | |||
341 | // Add unparsed relationship parts |
||
342 | 106 | if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'])) { |
|
343 | 1 | foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'] as $vmlDrawing) { |
|
344 | 1 | $zip->addFile($vmlDrawing['filePath'], $vmlDrawing['content']); |
|
345 | } |
||
346 | } |
||
347 | |||
348 | // Add header/footer relationship parts |
||
349 | 106 | if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { |
|
350 | // VML Drawings |
||
351 | 1 | $zip->addFile('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i))); |
|
352 | |||
353 | // VML Drawing relationships |
||
354 | 1 | $zip->addFile('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i))); |
|
355 | |||
356 | // Media |
||
357 | 1 | foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { |
|
358 | 1 | $zip->addFile('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath())); |
|
359 | } |
||
360 | } |
||
361 | } |
||
362 | |||
363 | // Add media |
||
364 | 106 | for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { |
|
365 | 12 | if ($this->getDrawingHashTable()->getByIndex($i) instanceof WorksheetDrawing) { |
|
366 | 6 | $imageContents = null; |
|
367 | 6 | $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); |
|
1 ignored issue
–
show
|
|||
368 | 6 | if (strpos($imagePath, 'zip://') !== false) { |
|
369 | 2 | $imagePath = substr($imagePath, 6); |
|
370 | 2 | $imagePathSplitted = explode('#', $imagePath); |
|
371 | |||
372 | 2 | $imageZip = new ZipArchive(); |
|
373 | 2 | $imageZip->open($imagePathSplitted[0]); |
|
374 | 2 | $imageContents = $imageZip->getFromName($imagePathSplitted[1]); |
|
375 | 2 | $imageZip->close(); |
|
376 | 2 | unset($imageZip); |
|
377 | } else { |
||
378 | 5 | $imageContents = file_get_contents($imagePath); |
|
379 | } |
||
380 | |||
381 | 6 | $zip->addFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
|
382 | 6 | } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) { |
|
383 | 6 | ob_start(); |
|
384 | 6 | call_user_func( |
|
385 | 6 | $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), |
|
1 ignored issue
–
show
|
|||
386 | 6 | $this->getDrawingHashTable()->getByIndex($i)->getImageResource() |
|
1 ignored issue
–
show
|
|||
387 | ); |
||
388 | 6 | $imageContents = ob_get_contents(); |
|
389 | 6 | ob_end_clean(); |
|
390 | |||
391 | 6 | $zip->addFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
|
392 | } |
||
393 | } |
||
394 | |||
395 | 106 | Functions::setReturnDateType($saveDateReturnType); |
|
396 | 106 | Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
|
397 | |||
398 | // Close file |
||
399 | try { |
||
400 | 106 | $zip->finish(); |
|
401 | } catch (OverflowException $e) { |
||
402 | throw new WriterException('Could not close resource.'); |
||
403 | } |
||
404 | |||
405 | 106 | $this->maybeCloseFileHandle(); |
|
406 | } else { |
||
407 | throw new WriterException('PhpSpreadsheet object unassigned.'); |
||
408 | } |
||
545 |