function Differ::coalesceReplacements
Same name in this branch
- 11.1.x vendor/nikic/php-parser/lib/PhpParser/Internal/Differ.php \PhpParser\Internal\Differ::coalesceReplacements()
* Coalesce equal-length sequences of remove+add into a replace operation. * *
Parameters
DiffElem[] $diff: * @return DiffElem[]
1 call to Differ::coalesceReplacements()
- Differ::diffWithReplacements in vendor/
phpstan/ phpdoc-parser/ src/ Printer/ Differ.php - * Calculate diff, including "replace" operations. * * If a sequence of remove operations is followed by the same number of add operations, these * will be coalesced into replace operations. * *
File
-
vendor/
phpstan/ phpdoc-parser/ src/ Printer/ Differ.php, line 156
Class
Namespace
PHPStan\PhpDocParser\PrinterCode
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;
}