function Differ::coalesceReplacements
Same name in this branch
- 11.1.x vendor/phpstan/phpdoc-parser/src/Printer/Differ.php \PHPStan\PhpDocParser\Printer\Differ::coalesceReplacements()
Coalesce equal-length sequences of remove+add into a replace operation.
Parameters
DiffElem[] $diff:
Return value
DiffElem[]
1 call to Differ::coalesceReplacements()
- Differ::diffWithReplacements in vendor/
nikic/ php-parser/ lib/ PhpParser/ Internal/ Differ.php - Calculate diff, including "replace" operations.
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Internal/ Differ.php, line 142
Class
- Differ
- Implements the Myers diff algorithm.
Namespace
PhpParser\InternalCode
private function coalesceReplacements(array $diff) : array {
$newDiff = [];
$c = \count($diff);
for ($i = 0; $i < $c; $i++) {
$diffType = $diff[$i]->type;
if ($diffType !== DiffElem::TYPE_REMOVE) {
$newDiff[] = $diff[$i];
continue;
}
$j = $i;
while ($j < $c && $diff[$j]->type === DiffElem::TYPE_REMOVE) {
$j++;
}
$k = $j;
while ($k < $c && $diff[$k]->type === DiffElem::TYPE_ADD) {
$k++;
}
if ($j - $i === $k - $j) {
$len = $j - $i;
for ($n = 0; $n < $len; $n++) {
$newDiff[] = new DiffElem(DiffElem::TYPE_REPLACE, $diff[$i + $n]->old, $diff[$j + $n]->new);
}
}
else {
for (; $i < $k; $i++) {
$newDiff[] = $diff[$i];
}
}
$i = $k - 1;
}
return $newDiff;
}