Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TableDiff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableDiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class TableDiff extends AbstractDiff |
||
14 | { |
||
15 | /** |
||
16 | * @var null|Table |
||
17 | */ |
||
18 | protected $oldTable = null; |
||
19 | |||
20 | /** |
||
21 | * @var null|Table |
||
22 | */ |
||
23 | protected $newTable = null; |
||
24 | |||
25 | /** |
||
26 | * @var null|\DOMElement |
||
27 | */ |
||
28 | protected $diffTable = null; |
||
29 | |||
30 | /** |
||
31 | * @var null|\DOMDocument |
||
32 | */ |
||
33 | protected $diffDom = null; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $newRowOffsets = 0; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $oldRowOffsets = 0; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $cellValues = array(); |
||
49 | |||
50 | /** |
||
51 | * @var \HTMLPurifier |
||
52 | */ |
||
53 | protected $purifier; |
||
54 | |||
55 | /** |
||
56 | * TableDiff constructor. |
||
57 | * |
||
58 | * @param string $oldText |
||
59 | * @param string $newText |
||
60 | * @param string $encoding |
||
61 | * @param array|null $specialCaseTags |
||
62 | * @param bool|null $groupDiffs |
||
63 | */ |
||
64 | public function __construct($oldText, $newText, $encoding = 'UTF-8', $specialCaseTags = null, $groupDiffs = null) |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | public function build() |
||
86 | |||
87 | protected function diffTableContent() |
||
123 | |||
124 | /** |
||
125 | * @param TableRow[] $oldRows |
||
126 | * @param TableRow[] $newRows |
||
127 | * @param RowMatch[] $matches |
||
128 | */ |
||
129 | protected function diffTableRowsWithMatches($oldRows, $newRows, $matches) |
||
201 | |||
202 | /** |
||
203 | * @param Operation $operation |
||
204 | * @param array $newRows |
||
205 | * @param array $appliedRowSpans |
||
206 | * @param bool $forceExpansion |
||
207 | */ |
||
208 | View Code Duplication | protected function processInsertOperation( |
|
219 | |||
220 | /** |
||
221 | * @param Operation $operation |
||
222 | * @param array $oldRows |
||
223 | * @param array $appliedRowSpans |
||
224 | * @param bool $forceExpansion |
||
225 | */ |
||
226 | View Code Duplication | protected function processDeleteOperation( |
|
237 | |||
238 | /** |
||
239 | * @param Operation $operation |
||
240 | * @param array $oldRows |
||
241 | * @param array $newRows |
||
242 | * @param array $appliedRowSpans |
||
243 | */ |
||
244 | protected function processEqualOperation(Operation $operation, $oldRows, $newRows, &$appliedRowSpans) |
||
261 | |||
262 | /** |
||
263 | * @param Operation $operation |
||
264 | * @param array $oldRows |
||
265 | * @param array $newRows |
||
266 | * @param array $appliedRowSpans |
||
267 | */ |
||
268 | protected function processReplaceOperation(Operation $operation, $oldRows, $newRows, &$appliedRowSpans) |
||
273 | |||
274 | /** |
||
275 | * @param array $oldMatchData |
||
276 | * @param array $newMatchData |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | protected function getRowMatches($oldMatchData, $newMatchData) |
||
293 | |||
294 | /** |
||
295 | * @param array $newMatchData |
||
296 | * @param int $startInOld |
||
297 | * @param int $endInOld |
||
298 | * @param int $startInNew |
||
299 | * @param int $endInNew |
||
300 | * @param array $matches |
||
301 | */ |
||
302 | protected function findRowMatches($newMatchData, $startInOld, $endInOld, $startInNew, $endInNew, &$matches) |
||
335 | |||
336 | /** |
||
337 | * @param array $newMatchData |
||
338 | * @param int $startInOld |
||
339 | * @param int $endInOld |
||
340 | * @param int $startInNew |
||
341 | * @param int $endInNew |
||
342 | * |
||
343 | * @return RowMatch|null |
||
344 | */ |
||
345 | protected function findRowMatch($newMatchData, $startInOld, $endInOld, $startInNew, $endInNew) |
||
390 | |||
391 | /** |
||
392 | * @param TableRow|null $oldRow |
||
393 | * @param TableRow|null $newRow |
||
394 | * @param array $appliedRowSpans |
||
395 | * @param bool $forceExpansion |
||
396 | * |
||
397 | * @return array |
||
398 | */ |
||
399 | protected function diffRows($oldRow, $newRow, array &$appliedRowSpans, $forceExpansion = false) |
||
400 | { |
||
401 | // create tr dom element |
||
402 | $rowToClone = $newRow ?: $oldRow; |
||
403 | /* @var $diffRow \DOMElement */ |
||
404 | $diffRow = $this->diffDom->importNode($rowToClone->getDomNode()->cloneNode(false), false); |
||
405 | |||
406 | $oldCells = $oldRow ? $oldRow->getCells() : array(); |
||
407 | $newCells = $newRow ? $newRow->getCells() : array(); |
||
408 | |||
409 | $position = new DiffRowPosition(); |
||
410 | |||
411 | $extraRow = null; |
||
412 | |||
413 | /* @var $expandCells \DOMElement[] */ |
||
414 | $expandCells = array(); |
||
415 | /* @var $cellsWithMultipleRows \DOMElement[] */ |
||
416 | $cellsWithMultipleRows = array(); |
||
417 | |||
418 | $newCellCount = count($newCells); |
||
419 | while ($position->getIndexInNew() < $newCellCount) { |
||
420 | if (!$position->areColumnsEqual()) { |
||
421 | $type = $position->getLesserColumnType(); |
||
422 | if ($type === 'new') { |
||
423 | $row = $newRow; |
||
424 | $targetRow = $extraRow; |
||
425 | } else { |
||
426 | $row = $oldRow; |
||
427 | $targetRow = $diffRow; |
||
428 | } |
||
429 | if ($row && $targetRow && (!$type === 'old' || isset($oldCells[$position->getIndexInOld()]))) { |
||
430 | $this->syncVirtualColumns($row, $position, $cellsWithMultipleRows, $targetRow, $type, true); |
||
431 | |||
432 | continue; |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /* @var $newCell TableCell */ |
||
437 | $newCell = $newCells[$position->getIndexInNew()]; |
||
438 | /* @var $oldCell TableCell */ |
||
439 | $oldCell = isset($oldCells[$position->getIndexInOld()]) ? $oldCells[$position->getIndexInOld()] : null; |
||
440 | |||
441 | if ($oldCell && $newCell->getColspan() != $oldCell->getColspan()) { |
||
442 | if (null === $extraRow) { |
||
443 | /* @var $extraRow \DOMElement */ |
||
444 | $extraRow = $this->diffDom->importNode($rowToClone->getDomNode()->cloneNode(false), false); |
||
445 | } |
||
446 | |||
447 | if ($oldCell->getColspan() > $newCell->getColspan()) { |
||
448 | $this->diffCellsAndIncrementCounters( |
||
449 | $oldCell, |
||
450 | null, |
||
451 | $cellsWithMultipleRows, |
||
452 | $diffRow, |
||
453 | $position, |
||
454 | true |
||
455 | ); |
||
456 | $this->syncVirtualColumns($newRow, $position, $cellsWithMultipleRows, $extraRow, 'new', true); |
||
457 | } else { |
||
458 | $this->diffCellsAndIncrementCounters( |
||
459 | null, |
||
460 | $newCell, |
||
461 | $cellsWithMultipleRows, |
||
462 | $extraRow, |
||
463 | $position, |
||
464 | true |
||
465 | ); |
||
466 | $this->syncVirtualColumns($oldRow, $position, $cellsWithMultipleRows, $diffRow, 'old', true); |
||
467 | } |
||
468 | } else { |
||
469 | $diffCell = $this->diffCellsAndIncrementCounters( |
||
470 | $oldCell, |
||
471 | $newCell, |
||
472 | $cellsWithMultipleRows, |
||
473 | $diffRow, |
||
474 | $position |
||
475 | ); |
||
476 | $expandCells[] = $diffCell; |
||
477 | } |
||
478 | } |
||
479 | |||
480 | $oldCellCount = count($oldCells); |
||
481 | while ($position->getIndexInOld() < $oldCellCount) { |
||
482 | $diffCell = $this->diffCellsAndIncrementCounters( |
||
483 | $oldCells[$position->getIndexInOld()], |
||
484 | null, |
||
485 | $cellsWithMultipleRows, |
||
486 | $diffRow, |
||
487 | $position |
||
488 | ); |
||
489 | $expandCells[] = $diffCell; |
||
490 | } |
||
491 | |||
492 | if ($extraRow) { |
||
493 | foreach ($expandCells as $expandCell) { |
||
494 | $rowspan = $expandCell->getAttribute('rowspan') ?: 1; |
||
495 | $expandCell->setAttribute('rowspan', 1 + $rowspan); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | if ($extraRow || $forceExpansion) { |
||
500 | foreach ($appliedRowSpans as $rowSpanCells) { |
||
501 | /* @var $rowSpanCells \DOMElement[] */ |
||
502 | foreach ($rowSpanCells as $extendCell) { |
||
503 | $rowspan = $extendCell->getAttribute('rowspan') ?: 1; |
||
504 | $extendCell->setAttribute('rowspan', 1 + $rowspan); |
||
505 | } |
||
506 | } |
||
507 | } |
||
508 | |||
509 | if (!$forceExpansion) { |
||
510 | array_shift($appliedRowSpans); |
||
511 | $appliedRowSpans = array_values($appliedRowSpans); |
||
512 | } |
||
513 | $appliedRowSpans = array_merge($appliedRowSpans, array_values($cellsWithMultipleRows)); |
||
514 | |||
515 | return array($diffRow, $extraRow); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param TableCell|null $oldCell |
||
520 | * @param TableCell|null $newCell |
||
521 | * |
||
522 | * @return \DOMElement |
||
523 | */ |
||
524 | protected function getNewCellNode(TableCell $oldCell = null, TableCell $newCell = null) |
||
549 | |||
550 | /** |
||
551 | * @param TableCell|null $oldCell |
||
552 | * @param TableCell|null $newCell |
||
553 | * @param bool $usingExtraRow |
||
554 | * |
||
555 | * @return \DOMElement |
||
556 | */ |
||
557 | protected function diffCells($oldCell, $newCell, $usingExtraRow = false) |
||
590 | |||
591 | protected function buildTableDoms() |
||
596 | |||
597 | /** |
||
598 | * @param string $text |
||
599 | * |
||
600 | * @return \DOMDocument |
||
601 | */ |
||
602 | protected function createDocumentWithHtml($text) |
||
613 | |||
614 | /** |
||
615 | * @param string $text |
||
616 | * |
||
617 | * @return Table |
||
618 | */ |
||
619 | protected function parseTableStructure($text) |
||
631 | |||
632 | /** |
||
633 | * @param Table $table |
||
634 | * @param \DOMNode|null $node |
||
635 | */ |
||
636 | protected function parseTable(Table $table, \DOMNode $node = null) |
||
653 | |||
654 | /** |
||
655 | * @param TableRow $row |
||
656 | */ |
||
657 | protected function parseTableRow(TableRow $row) |
||
668 | |||
669 | /** |
||
670 | * @param \DOMNode $node |
||
671 | * |
||
672 | * @return string |
||
673 | */ |
||
674 | protected function getInnerHtml($node) |
||
685 | |||
686 | /** |
||
687 | * @param \DOMNode $node |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | View Code Duplication | protected function htmlFromNode($node) |
|
699 | |||
700 | /** |
||
701 | * @param \DOMNode $node |
||
702 | * @param string $html |
||
703 | */ |
||
704 | protected function setInnerHtml($node, $html) |
||
720 | |||
721 | /** |
||
722 | * @param Table $table |
||
723 | */ |
||
724 | protected function indexCellValues(Table $table) |
||
738 | |||
739 | /** |
||
740 | * @param TableRow $tableRow |
||
741 | * @param DiffRowPosition $position |
||
742 | * @param array $cellsWithMultipleRows |
||
743 | * @param \DOMNode $diffRow |
||
744 | * @param string $diffType |
||
745 | * @param bool $usingExtraRow |
||
746 | */ |
||
747 | protected function syncVirtualColumns( |
||
771 | |||
772 | /** |
||
773 | * @param null|TableCell $oldCell |
||
774 | * @param null|TableCell $newCell |
||
775 | * @param array $cellsWithMultipleRows |
||
776 | * @param \DOMElement $diffRow |
||
777 | * @param DiffRowPosition $position |
||
778 | * @param bool $usingExtraRow |
||
779 | * |
||
780 | * @return \DOMElement |
||
781 | */ |
||
782 | protected function diffCellsAndIncrementCounters( |
||
809 | |||
810 | /** |
||
811 | * @param TableRow|null $oldRow |
||
812 | * @param TableRow|null $newRow |
||
813 | * @param array $appliedRowSpans |
||
814 | * @param bool $forceExpansion |
||
815 | */ |
||
816 | protected function diffAndAppendRows($oldRow, $newRow, &$appliedRowSpans, $forceExpansion = false) |
||
831 | |||
832 | /** |
||
833 | * @param TableRow $oldRow |
||
834 | * @param TableRow $newRow |
||
835 | * @param int $oldIndex |
||
836 | * @param int $newIndex |
||
837 | * |
||
838 | * @return float|int |
||
839 | */ |
||
840 | protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow, $oldIndex, $newIndex) |
||
866 | } |
||
867 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: