Conditions | 28 |
Paths | > 20000 |
Total Lines | 196 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
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 |
||
196 | public function save($pFilename = null) |
||
197 | { |
||
198 | if ($this->_spreadSheet !== NULL) { |
||
199 | // garbage collect |
||
200 | $this->_spreadSheet->garbageCollect(); |
||
201 | |||
202 | // If $pFilename is php://output or php://stdout, make it a temporary file... |
||
203 | $originalFilename = $pFilename; |
||
204 | if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
||
205 | $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp'); |
||
206 | if ($pFilename == '') { |
||
207 | $pFilename = $originalFilename; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog; |
||
212 | PHPExcel_Calculation::getInstance()->writeDebugLog = false; |
||
213 | $saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType(); |
||
214 | PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL); |
||
215 | |||
216 | // Create string lookup table |
||
217 | $this->_stringTable = array(); |
||
218 | for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) { |
||
219 | $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable); |
||
|
|||
220 | } |
||
221 | |||
222 | // Create styles dictionaries |
||
223 | $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) ); |
||
224 | $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) ); |
||
225 | $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) ); |
||
226 | $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) ); |
||
227 | $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) ); |
||
228 | |||
229 | // Create drawing dictionary |
||
230 | $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) ); |
||
231 | |||
232 | // Create new ZIP file and open it for writing |
||
233 | $zipClass = PHPExcel_Settings::getZipClass(); |
||
234 | $objZip = new $zipClass(); |
||
235 | |||
236 | // Retrieve OVERWRITE and CREATE constants from the instantiated zip class |
||
237 | // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP |
||
238 | $ro = new ReflectionObject($objZip); |
||
239 | $zipOverWrite = $ro->getConstant('OVERWRITE'); |
||
240 | $zipCreate = $ro->getConstant('CREATE'); |
||
241 | |||
242 | if (file_exists($pFilename)) { |
||
243 | unlink($pFilename); |
||
244 | } |
||
245 | // Try opening the ZIP file |
||
246 | if ($objZip->open($pFilename, $zipOverWrite) !== true) { |
||
247 | if ($objZip->open($pFilename, $zipCreate) !== true) { |
||
248 | throw new Exception("Could not open " . $pFilename . " for writing."); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // Add [Content_Types].xml to ZIP file |
||
253 | $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts)); |
||
254 | |||
255 | // Add relationships to ZIP file |
||
256 | $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet)); |
||
257 | $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet)); |
||
258 | |||
259 | // Add document properties to ZIP file |
||
260 | $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet)); |
||
261 | $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet)); |
||
262 | $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet); |
||
263 | if ($customPropertiesPart !== NULL) { |
||
264 | $objZip->addFromString('docProps/custom.xml', $customPropertiesPart); |
||
265 | } |
||
266 | |||
267 | // Add theme to ZIP file |
||
268 | $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet)); |
||
269 | |||
270 | // Add string table to ZIP file |
||
271 | $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable)); |
||
272 | |||
273 | // Add styles to ZIP file |
||
274 | $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet)); |
||
275 | |||
276 | // Add workbook to ZIP file |
||
277 | $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas)); |
||
278 | |||
279 | $chartCount = 0; |
||
280 | // Add worksheets |
||
281 | for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) { |
||
282 | $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts)); |
||
283 | if ($this->_includeCharts) { |
||
284 | $charts = $this->_spreadSheet->getSheet($i)->getChartCollection(); |
||
285 | if (count($charts) > 0) { |
||
286 | foreach($charts as $chart) { |
||
287 | $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart)); |
||
288 | $chartCount++; |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | $chartRef1 = $chartRef2 = 0; |
||
295 | // Add worksheet relationships (drawings, ...) |
||
296 | for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) { |
||
297 | |||
298 | // Add relationships |
||
299 | $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts)); |
||
300 | |||
301 | $drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection(); |
||
302 | $drawingCount = count($drawings); |
||
303 | if ($this->_includeCharts) { |
||
304 | $chartCount = $this->_spreadSheet->getSheet($i)->getChartCount(); |
||
305 | } |
||
306 | |||
307 | // Add drawing and image relationship parts |
||
308 | if (($drawingCount > 0) || ($chartCount > 0)) { |
||
309 | // Drawing relationships |
||
310 | $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts)); |
||
311 | |||
312 | // Drawings |
||
313 | $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts)); |
||
314 | } |
||
315 | |||
316 | // Add comment relationship parts |
||
317 | if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) { |
||
318 | // VML Comments |
||
319 | $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i))); |
||
320 | |||
321 | // Comments |
||
322 | $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i))); |
||
323 | } |
||
324 | |||
325 | // Add header/footer relationship parts |
||
326 | if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { |
||
327 | // VML Drawings |
||
328 | $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i))); |
||
329 | |||
330 | // VML Drawing relationships |
||
331 | $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i))); |
||
332 | |||
333 | // Media |
||
334 | foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { |
||
335 | $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath())); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // Add media |
||
341 | for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { |
||
342 | if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) { |
||
343 | $imageContents = null; |
||
344 | $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); |
||
345 | |||
346 | if (strpos($imagePath, 'zip://') !== false) { |
||
347 | $imagePath = substr($imagePath, 6); |
||
348 | $imagePathSplitted = explode('#', $imagePath); |
||
349 | |||
350 | $imageZip = new ZipArchive(); |
||
351 | $imageZip->open($imagePathSplitted[0]); |
||
352 | $imageContents = $imageZip->getFromName($imagePathSplitted[1]); |
||
353 | $imageZip->close(); |
||
354 | unset($imageZip); |
||
355 | } else { |
||
356 | $imageContents = file_get_contents($imagePath); |
||
357 | } |
||
358 | |||
359 | $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
||
360 | } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) { |
||
361 | ob_start(); |
||
362 | call_user_func( |
||
363 | $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), |
||
364 | $this->getDrawingHashTable()->getByIndex($i)->getImageResource() |
||
365 | ); |
||
366 | $imageContents = ob_get_contents(); |
||
367 | ob_end_clean(); |
||
368 | |||
369 | $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType); |
||
374 | PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog; |
||
375 | |||
376 | // Close file |
||
377 | if ($objZip->close() === false) { |
||
378 | throw new Exception("Could not close zip file $pFilename."); |
||
379 | } |
||
380 | |||
381 | // If a temporary file was used, copy it to the correct file stream |
||
382 | if ($originalFilename != $pFilename) { |
||
383 | if (copy($pFilename, $originalFilename) === false) { |
||
384 | throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
||
385 | } |
||
386 | @unlink($pFilename); |
||
387 | } |
||
388 | } else { |
||
389 | throw new Exception("PHPExcel object unassigned."); |
||
390 | } |
||
391 | } |
||
392 | |||
584 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: