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