Skip to content

Commit 7a3a367

Browse files
committed
Code style cleanup in TableDiff
1 parent 673cf9c commit 7a3a367

File tree

1 file changed

+92
-52
lines changed

1 file changed

+92
-52
lines changed

lib/Caxy/HtmlDiff/Table/TableDiff.php

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TableDiff extends AbstractDiff
5454
* @var \HTMLPurifier
5555
*/
5656
protected $purifier;
57-
57+
5858
public function __construct($oldText, $newText, $encoding, $specialCaseTags, $groupDiffs)
5959
{
6060
parent::__construct($oldText, $newText, $encoding, $specialCaseTags, $groupDiffs);
@@ -155,7 +155,6 @@ protected function diffTableContent()
155155

156156
$appliedRowSpans = array();
157157

158-
// @todo: Do row matching
159158
$oldMatchData = array();
160159
$newMatchData = array();
161160

@@ -302,21 +301,47 @@ protected function diffRows($oldRow, $newRow, array &$appliedRowSpans, $forceExp
302301
// @todo: How do we handle cells that have both rowspan and colspan?
303302
304303
if ($oldCell->getColspan() > $newCell->getColspan()) {
305-
$this->diffCellsAndIncrementCounters($oldCell, null, $cellsWithMultipleRows, $diffRow, $position, true);
304+
$this->diffCellsAndIncrementCounters(
305+
$oldCell,
306+
null,
307+
$cellsWithMultipleRows,
308+
$diffRow,
309+
$position,
310+
true
311+
);
306312
$this->syncVirtualColumns($newRow, $position, $cellsWithMultipleRows, $extraRow, 'new', true);
307313
} else {
308-
$this->diffCellsAndIncrementCounters(null, $newCell, $cellsWithMultipleRows, $extraRow, $position, true);
314+
$this->diffCellsAndIncrementCounters(
315+
null,
316+
$newCell,
317+
$cellsWithMultipleRows,
318+
$extraRow,
319+
$position,
320+
true
321+
);
309322
$this->syncVirtualColumns($oldRow, $position, $cellsWithMultipleRows, $diffRow, 'old', true);
310323
}
311324
} else {
312-
$diffCell = $this->diffCellsAndIncrementCounters($oldCell, $newCell, $cellsWithMultipleRows, $diffRow, $position);
325+
$diffCell = $this->diffCellsAndIncrementCounters(
326+
$oldCell,
327+
$newCell,
328+
$cellsWithMultipleRows,
329+
$diffRow,
330+
$position
331+
);
313332
$expandCells[] = $diffCell;
314333
}
315334
}
316335

317336
$oldCellCount = count($oldCells);
318337
while ($position->getIndexInOld() < $oldCellCount) {
319-
$diffCell = $this->diffCellsAndIncrementCounters($oldCells[$position->getIndexInOld()], null, $cellsWithMultipleRows, $diffRow, $position);
338+
$diffCell = $this->diffCellsAndIncrementCounters(
339+
$oldCells[$position->getIndexInOld()],
340+
null,
341+
$cellsWithMultipleRows,
342+
$diffRow,
343+
$position
344+
);
320345
$expandCells[] = $diffCell;
321346
}
322347

@@ -377,7 +402,7 @@ protected function getNewCellNode(TableCell $oldCell = null, TableCell $newCell
377402
protected function diffCells($oldCell, $newCell, $usingExtraRow = false)
378403
{
379404
$diffCell = $this->getNewCellNode($oldCell, $newCell);
380-
405+
381406
$oldContent = $oldCell ? $this->getInnerHtml($oldCell->getDomNode()) : '';
382407
$newContent = $newCell ? $this->getInnerHtml($newCell->getDomNode()) : '';
383408

@@ -394,15 +419,15 @@ protected function diffCells($oldCell, $newCell, $usingExtraRow = false)
394419
$this->setInnerHtml($diffCell, $diff);
395420

396421
if (null === $newCell) {
397-
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class') . ' del'));
422+
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class').' del'));
398423
}
399424

400425
if (null === $oldCell) {
401-
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class') . ' ins'));
426+
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class').' ins'));
402427
}
403428

404429
if ($usingExtraRow) {
405-
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class') . ' extra-row'));
430+
$diffCell->setAttribute('class', trim($diffCell->getAttribute('class').' extra-row'));
406431
}
407432

408433
return $diffCell;
@@ -475,6 +500,7 @@ protected function htmlFromNode($node)
475500
$domDocument = new \DOMDocument();
476501
$newNode = $domDocument->importNode($node, true);
477502
$domDocument->appendChild($newNode);
503+
478504
return trim($domDocument->saveHTML());
479505
}
480506

@@ -492,7 +518,7 @@ protected function setInnerHtml($node, $html)
492518
foreach ($root->childNodes as $child) {
493519
$fragment->appendChild($node->ownerDocument->importNode($child, true));
494520
}
495-
521+
496522
$node->appendChild($fragment);
497523
}
498524

@@ -520,8 +546,12 @@ protected function getMatches()
520546

521547
$startInOld = new TablePosition(0, 0);
522548
$startInNew = new TablePosition(0, 0);
523-
$endInOld = new TablePosition($oldRowCount - 1, count($this->oldTable->getRow($oldRowCount - 1)->getCells()) - 1);
524-
$endInNew = new TablePosition($newRowCount - 1, count($this->newTable->getRow($newRowCount - 1)->getCells()) - 1);
549+
$endInOld = new TablePosition(
550+
$oldRowCount - 1, count($this->oldTable->getRow($oldRowCount - 1)->getCells()) - 1
551+
);
552+
$endInNew = new TablePosition(
553+
$newRowCount - 1, count($this->newTable->getRow($newRowCount - 1)->getCells()) - 1
554+
);
525555

526556
$this->findMatches($startInOld, $endInOld, $startInNew, $endInNew, $matches);
527557

@@ -535,7 +565,13 @@ protected function findMatches($startInOld, $endInOld, $startInNew, $endInNew, &
535565
if (TablePosition::compare($startInOld, $match->getStartInOld()) < 0 &&
536566
TablePosition::compare($startInNew, $match->getStartInNew()) < 0
537567
) {
538-
$this->findMatches($startInOld, $match->getStartInOld(), $startInNew, $match->getStartInNew(), $matches);
568+
$this->findMatches(
569+
$startInOld,
570+
$match->getStartInOld(),
571+
$startInNew,
572+
$match->getStartInNew(),
573+
$matches
574+
);
539575
}
540576

541577
$matches[] = $match;
@@ -568,7 +604,7 @@ protected function findMatch($startInOld, $endInOld, $startInNew, $endInNew)
568604
$currentPos = $this->oldTable->getPositionAfter($currentPos);
569605
continue;
570606
}
571-
607+
572608
foreach ($this->cellValues[$value] as $posInNew) {
573609
if (TablePosition::compare($posInNew, $startInNew) < 0) {
574610
continue;
@@ -596,27 +632,38 @@ protected function findMatch($startInOld, $endInOld, $startInNew, $endInNew)
596632
if ($bestMatchSize != 0) {
597633
$bestEndInOld = $this->oldTable->getPositionAfter($bestMatchInOld, $bestMatchSize);
598634
$bestEndInNew = $this->newTable->getPositionAfter($bestMatchInNew, $bestMatchSize);
635+
599636
return new TableMatch($bestMatchInOld, $bestMatchInNew, $bestEndInOld, $bestEndInNew);
600637
}
601638

602639
return null;
603640
}
604641

605642
/**
606-
* @param $tableRow
607-
* @param $currentColumn
608-
* @param $targetColumn
609-
* @param $currentCell
610-
* @param $cellsWithMultipleRows
611-
* @param $diffRow
612-
* @param $currentIndex
643+
* @param $tableRow
644+
* @param $currentColumn
645+
* @param $targetColumn
646+
* @param $currentCell
647+
* @param $cellsWithMultipleRows
648+
* @param $diffRow
649+
* @param $currentIndex
613650
* @param string $diffType
614651
*/
615-
protected function syncVirtualColumns($tableRow, DiffRowPosition $position, &$cellsWithMultipleRows, $diffRow, $diffType, $usingExtraRow = false)
616-
{
652+
protected function syncVirtualColumns(
653+
$tableRow,
654+
DiffRowPosition $position,
655+
&$cellsWithMultipleRows,
656+
$diffRow,
657+
$diffType,
658+
$usingExtraRow = false
659+
) {
617660
$currentCell = $tableRow->getCell($position->getIndex($diffType));
618661
while ($position->isColumnLessThanOther($diffType) && $currentCell) {
619-
$diffCell = $diffType === 'new' ? $this->diffCells(null, $currentCell, $usingExtraRow) : $this->diffCells($currentCell, null, $usingExtraRow);
662+
$diffCell = $diffType === 'new' ? $this->diffCells(null, $currentCell, $usingExtraRow) : $this->diffCells(
663+
$currentCell,
664+
null,
665+
$usingExtraRow
666+
);
620667
// Store cell in appliedRowSpans if spans multiple rows
621668
if ($diffCell->getAttribute('rowspan') > 1) {
622669
$cellsWithMultipleRows[$diffCell->getAttribute('rowspan')][] = $diffCell;
@@ -628,21 +675,23 @@ protected function syncVirtualColumns($tableRow, DiffRowPosition $position, &$ce
628675
}
629676

630677
/**
631-
* @param $oldCell
632-
* @param $newCell
633-
* @param $cellsWithMultipleRows
634-
* @param $diffRow
635-
* @param $expandCells
636-
* @param $indexInNew
637-
* @param $indexInOld
638-
* @param $newColspan
639-
* @param $virtualColInNew
640-
* @param $oldColspan
641-
* @param $virtualColInOld
642-
* @param $diffCell
678+
* @param null|TableCell $oldCell
679+
* @param null|TableCell $newCell
680+
* @param array $cellsWithMultipleRows
681+
* @param \DOMElement $diffRow
682+
* @param DiffRowPosition $position
683+
* @param bool $usingExtraRow
684+
*
685+
* @return \DOMElement
643686
*/
644-
protected function diffCellsAndIncrementCounters($oldCell, $newCell, &$cellsWithMultipleRows, $diffRow, DiffRowPosition $position, $usingExtraRow = false)
645-
{
687+
protected function diffCellsAndIncrementCounters(
688+
$oldCell,
689+
$newCell,
690+
&$cellsWithMultipleRows,
691+
$diffRow,
692+
DiffRowPosition $position,
693+
$usingExtraRow = false
694+
) {
646695
$diffCell = $this->diffCells($oldCell, $newCell, $usingExtraRow);
647696
// Store cell in appliedRowSpans if spans multiple rows
648697
if ($diffCell->getAttribute('rowspan') > 1) {
@@ -664,9 +713,9 @@ protected function diffCellsAndIncrementCounters($oldCell, $newCell, &$cellsWith
664713
}
665714

666715
/**
667-
* @param $oldRow
668-
* @param $newRow
669-
* @param $appliedRowSpans
716+
* @param $oldRow
717+
* @param $newRow
718+
* @param $appliedRowSpans
670719
* @param bool $forceExpansion
671720
*/
672721
protected function diffAndAppendRows($oldRow, $newRow, &$appliedRowSpans, $forceExpansion = false)
@@ -704,15 +753,6 @@ protected function getMatchPercentage(TableRow $oldRow, TableRow $newRow)
704753
}
705754
}
706755

707-
$matchPercentage = count($matches) > 0 ? $thresholdCount / count($matches) : 0;
708-
709-
$fullPercentage = null;
710-
similar_text($oldRow->getInnerHtml(), $newRow->getInnerHtml(), $fullPercentage);
711-
712-
if ($matchPercentage > $fullPercentage) {
713-
addDebugOutput(sprintf('Using match percentage! %s vs %s', $matchPercentage, $fullPercentage), __METHOD__);
714-
}
715-
716-
return $matchPercentage;
756+
return (count($matches) > 0) ? $thresholdCount / count($matches) : 0;
717757
}
718758
}

0 commit comments

Comments
 (0)