Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Differ.php

function Differ::coalesceReplacements

Same name in this branch
  1. 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

Differ
Inspired by https://github.com/nikic/PHP-Parser/tree/36a6dcd04e7b0285e8f0868f44bd49…

Namespace

PHPStan\PhpDocParser\Printer

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal