Conditions | 72 |
Paths | 4380 |
Total Lines | 272 |
Code Lines | 187 |
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 |
||
301 | protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row, &$column, &$cellContent) |
||
302 | { |
||
303 | foreach ($element->childNodes as $child) { |
||
304 | if ($child instanceof DOMText) { |
||
305 | $domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue)); |
||
306 | if (is_string($cellContent)) { |
||
307 | // simply append the text if the cell content is a plain text string |
||
308 | $cellContent .= $domText; |
||
309 | } |
||
310 | // but if we have a rich text run instead, we need to append it correctly |
||
311 | // TODO |
||
312 | } elseif ($child instanceof DOMElement) { |
||
313 | $attributeArray = []; |
||
314 | foreach ($child->attributes as $attribute) { |
||
315 | $attributeArray[$attribute->name] = $attribute->value; |
||
316 | } |
||
317 | |||
318 | switch ($child->nodeName) { |
||
319 | case 'meta': |
||
320 | foreach ($attributeArray as $attributeName => $attributeValue) { |
||
321 | // Extract character set, so we can convert to UTF-8 if required |
||
322 | if ($attributeName === 'charset') { |
||
323 | $this->setInputEncoding($attributeValue); |
||
324 | } |
||
325 | } |
||
326 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
327 | |||
328 | break; |
||
329 | case 'title': |
||
330 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
331 | $sheet->setTitle($cellContent, true, false); |
||
332 | $cellContent = ''; |
||
333 | |||
334 | break; |
||
335 | case 'span': |
||
336 | case 'div': |
||
337 | case 'font': |
||
338 | case 'i': |
||
339 | case 'em': |
||
340 | case 'strong': |
||
341 | case 'b': |
||
342 | if (isset($attributeArray['class']) && $attributeArray['class'] === 'comment') { |
||
343 | $sheet->getComment($column . $row) |
||
344 | ->getText() |
||
345 | ->createTextRun($child->textContent); |
||
346 | |||
347 | break; |
||
348 | } |
||
349 | |||
350 | if ($cellContent > '') { |
||
351 | $cellContent .= ' '; |
||
352 | } |
||
353 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
354 | if ($cellContent > '') { |
||
355 | $cellContent .= ' '; |
||
356 | } |
||
357 | |||
358 | if (isset($this->formats[$child->nodeName])) { |
||
359 | $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
||
360 | } |
||
361 | |||
362 | break; |
||
363 | case 'hr': |
||
364 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
365 | ++$row; |
||
366 | if (isset($this->formats[$child->nodeName])) { |
||
367 | $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
||
368 | } else { |
||
369 | $cellContent = '----------'; |
||
370 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
371 | } |
||
372 | ++$row; |
||
373 | // Add a break after a horizontal rule, simply by allowing the code to dropthru |
||
374 | // no break |
||
375 | case 'br': |
||
376 | if ($this->tableLevel > 0) { |
||
377 | // If we're inside a table, replace with a \n and set the cell to wrap |
||
378 | $cellContent .= "\n"; |
||
379 | $sheet->getStyle($column . $row)->getAlignment()->setWrapText(true); |
||
380 | } else { |
||
381 | // Otherwise flush our existing content and move the row cursor on |
||
382 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
383 | ++$row; |
||
384 | } |
||
385 | |||
386 | break; |
||
387 | case 'a': |
||
388 | foreach ($attributeArray as $attributeName => $attributeValue) { |
||
389 | switch ($attributeName) { |
||
390 | case 'href': |
||
391 | $sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue); |
||
392 | if (isset($this->formats[$child->nodeName])) { |
||
393 | $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
||
394 | } |
||
395 | |||
396 | break; |
||
397 | case 'class': |
||
398 | if ($attributeValue === 'comment-indicator') { |
||
399 | break; // Ignore - it's just a red square. |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | $cellContent .= ' '; |
||
404 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
405 | |||
406 | break; |
||
407 | case 'h1': |
||
408 | case 'h2': |
||
409 | case 'h3': |
||
410 | case 'h4': |
||
411 | case 'h5': |
||
412 | case 'h6': |
||
413 | case 'ol': |
||
414 | case 'ul': |
||
415 | case 'p': |
||
416 | if ($this->tableLevel > 0) { |
||
417 | // If we're inside a table, replace with a \n |
||
418 | $cellContent .= "\n"; |
||
419 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
420 | } else { |
||
421 | if ($cellContent > '') { |
||
422 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
423 | ++$row; |
||
424 | } |
||
425 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
426 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
427 | |||
428 | if (isset($this->formats[$child->nodeName])) { |
||
429 | $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
||
430 | } |
||
431 | |||
432 | ++$row; |
||
433 | $column = 'A'; |
||
434 | } |
||
435 | |||
436 | break; |
||
437 | case 'li': |
||
438 | if ($this->tableLevel > 0) { |
||
439 | // If we're inside a table, replace with a \n |
||
440 | $cellContent .= "\n"; |
||
441 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
442 | } else { |
||
443 | if ($cellContent > '') { |
||
444 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
445 | } |
||
446 | ++$row; |
||
447 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
448 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
449 | $column = 'A'; |
||
450 | } |
||
451 | |||
452 | break; |
||
453 | case 'img': |
||
454 | $this->insertImage($sheet, $column, $row, $attributeArray); |
||
455 | |||
456 | break; |
||
457 | case 'table': |
||
458 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
459 | $column = $this->setTableStartColumn($column); |
||
460 | if ($this->tableLevel > 1) { |
||
461 | --$row; |
||
462 | } |
||
463 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
464 | $column = $this->releaseTableStartColumn(); |
||
465 | if ($this->tableLevel > 1) { |
||
466 | ++$column; |
||
467 | } else { |
||
468 | ++$row; |
||
469 | } |
||
470 | |||
471 | break; |
||
472 | case 'thead': |
||
473 | case 'tbody': |
||
474 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
475 | |||
476 | break; |
||
477 | case 'tr': |
||
478 | $column = $this->getTableStartColumn(); |
||
479 | $cellContent = ''; |
||
480 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
481 | |||
482 | if (isset($attributeArray['height'])) { |
||
483 | $sheet->getRowDimension($row)->setRowHeight($attributeArray['height']); |
||
484 | } |
||
485 | |||
486 | ++$row; |
||
487 | |||
488 | break; |
||
489 | case 'th': |
||
490 | case 'td': |
||
491 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
492 | |||
493 | while (isset($this->rowspan[$column . $row])) { |
||
494 | ++$column; |
||
495 | } |
||
496 | |||
497 | // apply inline style |
||
498 | $this->applyInlineStyle($sheet, $row, $column, $attributeArray); |
||
499 | |||
500 | $this->flushCell($sheet, $column, $row, $cellContent); |
||
501 | |||
502 | if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) { |
||
503 | //create merging rowspan and colspan |
||
504 | $columnTo = $column; |
||
505 | for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
||
506 | ++$columnTo; |
||
507 | } |
||
508 | $range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1); |
||
509 | foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) { |
||
510 | $this->rowspan[$value] = true; |
||
511 | } |
||
512 | $sheet->mergeCells($range); |
||
513 | $column = $columnTo; |
||
514 | } elseif (isset($attributeArray['rowspan'])) { |
||
515 | //create merging rowspan |
||
516 | $range = $column . $row . ':' . $column . ($row + (int) $attributeArray['rowspan'] - 1); |
||
517 | foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) { |
||
518 | $this->rowspan[$value] = true; |
||
519 | } |
||
520 | $sheet->mergeCells($range); |
||
521 | } elseif (isset($attributeArray['colspan'])) { |
||
522 | //create merging colspan |
||
523 | $columnTo = $column; |
||
524 | for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
||
525 | ++$columnTo; |
||
526 | } |
||
527 | $sheet->mergeCells($column . $row . ':' . $columnTo . $row); |
||
528 | $column = $columnTo; |
||
529 | } elseif (isset($attributeArray['bgcolor'])) { |
||
530 | $sheet->getStyle($column . $row)->applyFromArray( |
||
531 | [ |
||
532 | 'fill' => [ |
||
533 | 'fillType' => Fill::FILL_SOLID, |
||
534 | 'color' => ['rgb' => $attributeArray['bgcolor']], |
||
535 | ], |
||
536 | ] |
||
537 | ); |
||
538 | } |
||
539 | |||
540 | if (isset($attributeArray['width'])) { |
||
541 | $sheet->getColumnDimension($column)->setWidth($attributeArray['width']); |
||
542 | } |
||
543 | |||
544 | if (isset($attributeArray['height'])) { |
||
545 | $sheet->getRowDimension($row)->setRowHeight($attributeArray['height']); |
||
546 | } |
||
547 | |||
548 | if (isset($attributeArray['align'])) { |
||
549 | $sheet->getStyle($column . $row)->getAlignment()->setHorizontal($attributeArray['align']); |
||
550 | } |
||
551 | |||
552 | if (isset($attributeArray['valign'])) { |
||
553 | $sheet->getStyle($column . $row)->getAlignment()->setVertical($attributeArray['valign']); |
||
554 | } |
||
555 | |||
556 | if (isset($attributeArray['data-format'])) { |
||
557 | $sheet->getStyle($column . $row)->getNumberFormat()->setFormatCode($attributeArray['data-format']); |
||
558 | } |
||
559 | |||
560 | ++$column; |
||
561 | |||
562 | break; |
||
563 | case 'body': |
||
564 | $row = 1; |
||
565 | $column = 'A'; |
||
566 | $cellContent = ''; |
||
567 | $this->tableLevel = 0; |
||
568 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
569 | |||
570 | break; |
||
571 | default: |
||
572 | $this->processDomElement($child, $sheet, $row, $column, $cellContent); |
||
573 | } |
||
970 |