src/Psalm/Internal/Diff/FileDiffer.php
private static function coalesceReplacements(array $diff)
    {
        $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];
                }
            }

            /** @psalm-suppress LoopInvalidation */
            $i = $k - 1;
        }
        return $newDiff;
    }
private static function coalesceReplacements(array $diff)
    {
        $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];
                }
            }

            /** @psalm-suppress LoopInvalidation */
            $i = $k - 1;
        }
        return $newDiff;
    }
private static function coalesceReplacements ( array $ diff ) { $ 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 ] ; } } /** @psalm-suppress LoopInvalidation */ $ i = $ k - 1 ; } return $ newDiff ; }
Coalesce equal-length sequences of remove+add into a replace operation.

@param DiffElem[] $diff
@return DiffElem[]
Coalesce equal - length sequences of remove + add into a replace operation .