first commit
This commit is contained in:
124
vendor/sebastian/diff/src/Chunk.php
vendored
Normal file
124
vendor/sebastian/diff/src/Chunk.php
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use ArrayIterator;
|
||||
use IteratorAggregate;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* @template-implements IteratorAggregate<int, Line>
|
||||
*/
|
||||
final class Chunk implements IteratorAggregate
|
||||
{
|
||||
private int $start;
|
||||
private int $startRange;
|
||||
private int $end;
|
||||
private int $endRange;
|
||||
private array $lines;
|
||||
|
||||
public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = [])
|
||||
{
|
||||
$this->start = $start;
|
||||
$this->startRange = $startRange;
|
||||
$this->end = $end;
|
||||
$this->endRange = $endRange;
|
||||
$this->lines = $lines;
|
||||
}
|
||||
|
||||
public function start(): int
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
public function startRange(): int
|
||||
{
|
||||
return $this->startRange;
|
||||
}
|
||||
|
||||
public function end(): int
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
public function endRange(): int
|
||||
{
|
||||
return $this->endRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return list<Line>
|
||||
*/
|
||||
public function lines(): array
|
||||
{
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-param list<Line> $lines
|
||||
*/
|
||||
public function setLines(array $lines): void
|
||||
{
|
||||
foreach ($lines as $line) {
|
||||
if (!$line instanceof Line) {
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
|
||||
$this->lines = $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use start() instead
|
||||
*/
|
||||
public function getStart(): int
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use startRange() instead
|
||||
*/
|
||||
public function getStartRange(): int
|
||||
{
|
||||
return $this->startRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use end() instead
|
||||
*/
|
||||
public function getEnd(): int
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use endRange() instead
|
||||
*/
|
||||
public function getEndRange(): int
|
||||
{
|
||||
return $this->endRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return list<Line>
|
||||
*
|
||||
* @deprecated Use lines() instead
|
||||
*/
|
||||
public function getLines(): array
|
||||
{
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
return new ArrayIterator($this->lines);
|
||||
}
|
||||
}
|
114
vendor/sebastian/diff/src/Diff.php
vendored
Normal file
114
vendor/sebastian/diff/src/Diff.php
vendored
Normal file
@ -0,0 +1,114 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use ArrayIterator;
|
||||
use IteratorAggregate;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* @template-implements IteratorAggregate<int, Chunk>
|
||||
*/
|
||||
final class Diff implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @psalm-var non-empty-string
|
||||
*/
|
||||
private string $from;
|
||||
|
||||
/**
|
||||
* @psalm-var non-empty-string
|
||||
*/
|
||||
private string $to;
|
||||
|
||||
/**
|
||||
* @psalm-var list<Chunk>
|
||||
*/
|
||||
private array $chunks;
|
||||
|
||||
/**
|
||||
* @psalm-param non-empty-string $from
|
||||
* @psalm-param non-empty-string $to
|
||||
* @psalm-param list<Chunk> $chunks
|
||||
*/
|
||||
public function __construct(string $from, string $to, array $chunks = [])
|
||||
{
|
||||
$this->from = $from;
|
||||
$this->to = $to;
|
||||
$this->chunks = $chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return non-empty-string
|
||||
*/
|
||||
public function from(): string
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return non-empty-string
|
||||
*/
|
||||
public function to(): string
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return list<Chunk>
|
||||
*/
|
||||
public function chunks(): array
|
||||
{
|
||||
return $this->chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-param list<Chunk> $chunks
|
||||
*/
|
||||
public function setChunks(array $chunks): void
|
||||
{
|
||||
$this->chunks = $chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return non-empty-string
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function getFrom(): string
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return non-empty-string
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function getTo(): string
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return list<Chunk>
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function getChunks(): array
|
||||
{
|
||||
return $this->chunks;
|
||||
}
|
||||
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
return new ArrayIterator($this->chunks);
|
||||
}
|
||||
}
|
239
vendor/sebastian/diff/src/Differ.php
vendored
Normal file
239
vendor/sebastian/diff/src/Differ.php
vendored
Normal file
@ -0,0 +1,239 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use const PHP_INT_SIZE;
|
||||
use const PREG_SPLIT_DELIM_CAPTURE;
|
||||
use const PREG_SPLIT_NO_EMPTY;
|
||||
use function array_shift;
|
||||
use function array_unshift;
|
||||
use function array_values;
|
||||
use function count;
|
||||
use function current;
|
||||
use function end;
|
||||
use function is_string;
|
||||
use function key;
|
||||
use function min;
|
||||
use function preg_split;
|
||||
use function prev;
|
||||
use function reset;
|
||||
use function str_ends_with;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
|
||||
|
||||
final class Differ
|
||||
{
|
||||
public const OLD = 0;
|
||||
public const ADDED = 1;
|
||||
public const REMOVED = 2;
|
||||
public const DIFF_LINE_END_WARNING = 3;
|
||||
public const NO_LINE_END_EOF_WARNING = 4;
|
||||
private DiffOutputBuilderInterface $outputBuilder;
|
||||
|
||||
public function __construct(DiffOutputBuilderInterface $outputBuilder)
|
||||
{
|
||||
$this->outputBuilder = $outputBuilder;
|
||||
}
|
||||
|
||||
public function diff(array|string $from, array|string $to, ?LongestCommonSubsequenceCalculator $lcs = null): string
|
||||
{
|
||||
$diff = $this->diffToArray($from, $to, $lcs);
|
||||
|
||||
return $this->outputBuilder->getDiff($diff);
|
||||
}
|
||||
|
||||
public function diffToArray(array|string $from, array|string $to, ?LongestCommonSubsequenceCalculator $lcs = null): array
|
||||
{
|
||||
if (is_string($from)) {
|
||||
$from = $this->splitStringByLines($from);
|
||||
}
|
||||
|
||||
if (is_string($to)) {
|
||||
$to = $this->splitStringByLines($to);
|
||||
}
|
||||
|
||||
[$from, $to, $start, $end] = self::getArrayDiffParted($from, $to);
|
||||
|
||||
if ($lcs === null) {
|
||||
$lcs = $this->selectLcsImplementation($from, $to);
|
||||
}
|
||||
|
||||
$common = $lcs->calculate(array_values($from), array_values($to));
|
||||
$diff = [];
|
||||
|
||||
foreach ($start as $token) {
|
||||
$diff[] = [$token, self::OLD];
|
||||
}
|
||||
|
||||
reset($from);
|
||||
reset($to);
|
||||
|
||||
foreach ($common as $token) {
|
||||
while (($fromToken = reset($from)) !== $token) {
|
||||
$diff[] = [array_shift($from), self::REMOVED];
|
||||
}
|
||||
|
||||
while (($toToken = reset($to)) !== $token) {
|
||||
$diff[] = [array_shift($to), self::ADDED];
|
||||
}
|
||||
|
||||
$diff[] = [$token, self::OLD];
|
||||
|
||||
array_shift($from);
|
||||
array_shift($to);
|
||||
}
|
||||
|
||||
while (($token = array_shift($from)) !== null) {
|
||||
$diff[] = [$token, self::REMOVED];
|
||||
}
|
||||
|
||||
while (($token = array_shift($to)) !== null) {
|
||||
$diff[] = [$token, self::ADDED];
|
||||
}
|
||||
|
||||
foreach ($end as $token) {
|
||||
$diff[] = [$token, self::OLD];
|
||||
}
|
||||
|
||||
if ($this->detectUnmatchedLineEndings($diff)) {
|
||||
array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
|
||||
}
|
||||
|
||||
return $diff;
|
||||
}
|
||||
|
||||
private function splitStringByLines(string $input): array
|
||||
{
|
||||
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
private function selectLcsImplementation(array $from, array $to): LongestCommonSubsequenceCalculator
|
||||
{
|
||||
// We do not want to use the time-efficient implementation if its memory
|
||||
// footprint will probably exceed this value. Note that the footprint
|
||||
// calculation is only an estimation for the matrix and the LCS method
|
||||
// will typically allocate a bit more memory than this.
|
||||
$memoryLimit = 100 * 1024 * 1024;
|
||||
|
||||
if ($this->calculateEstimatedFootprint($from, $to) > $memoryLimit) {
|
||||
return new MemoryEfficientLongestCommonSubsequenceCalculator;
|
||||
}
|
||||
|
||||
return new TimeEfficientLongestCommonSubsequenceCalculator;
|
||||
}
|
||||
|
||||
private function calculateEstimatedFootprint(array $from, array $to): float|int
|
||||
{
|
||||
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
|
||||
|
||||
return $itemSize * min(count($from), count($to)) ** 2;
|
||||
}
|
||||
|
||||
private function detectUnmatchedLineEndings(array $diff): bool
|
||||
{
|
||||
$newLineBreaks = ['' => true];
|
||||
$oldLineBreaks = ['' => true];
|
||||
|
||||
foreach ($diff as $entry) {
|
||||
if (self::OLD === $entry[1]) {
|
||||
$ln = $this->getLinebreak($entry[0]);
|
||||
$oldLineBreaks[$ln] = true;
|
||||
$newLineBreaks[$ln] = true;
|
||||
} elseif (self::ADDED === $entry[1]) {
|
||||
$newLineBreaks[$this->getLinebreak($entry[0])] = true;
|
||||
} elseif (self::REMOVED === $entry[1]) {
|
||||
$oldLineBreaks[$this->getLinebreak($entry[0])] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// if either input or output is a single line without breaks than no warning should be raised
|
||||
if (['' => true] === $newLineBreaks || ['' => true] === $oldLineBreaks) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// two-way compare
|
||||
foreach ($newLineBreaks as $break => $set) {
|
||||
if (!isset($oldLineBreaks[$break])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($oldLineBreaks as $break => $set) {
|
||||
if (!isset($newLineBreaks[$break])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function getLinebreak($line): string
|
||||
{
|
||||
if (!is_string($line)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$lc = substr($line, -1);
|
||||
|
||||
if ("\r" === $lc) {
|
||||
return "\r";
|
||||
}
|
||||
|
||||
if ("\n" !== $lc) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (str_ends_with($line, "\r\n")) {
|
||||
return "\r\n";
|
||||
}
|
||||
|
||||
return "\n";
|
||||
}
|
||||
|
||||
private static function getArrayDiffParted(array &$from, array &$to): array
|
||||
{
|
||||
$start = [];
|
||||
$end = [];
|
||||
|
||||
reset($to);
|
||||
|
||||
foreach ($from as $k => $v) {
|
||||
$toK = key($to);
|
||||
|
||||
if ($toK === $k && $v === $to[$k]) {
|
||||
$start[$k] = $v;
|
||||
|
||||
unset($from[$k], $to[$k]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
end($from);
|
||||
end($to);
|
||||
|
||||
do {
|
||||
$fromK = key($from);
|
||||
$toK = key($to);
|
||||
|
||||
if (null === $fromK || null === $toK || current($from) !== current($to)) {
|
||||
break;
|
||||
}
|
||||
|
||||
prev($from);
|
||||
prev($to);
|
||||
|
||||
$end = [$fromK => $from[$fromK]] + $end;
|
||||
unset($from[$fromK], $to[$toK]);
|
||||
} while (true);
|
||||
|
||||
return [$from, $to, $start, $end];
|
||||
}
|
||||
}
|
37
vendor/sebastian/diff/src/Exception/ConfigurationException.php
vendored
Normal file
37
vendor/sebastian/diff/src/Exception/ConfigurationException.php
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use function gettype;
|
||||
use function is_object;
|
||||
use function sprintf;
|
||||
use Exception;
|
||||
|
||||
final class ConfigurationException extends InvalidArgumentException
|
||||
{
|
||||
public function __construct(
|
||||
string $option,
|
||||
string $expected,
|
||||
$value,
|
||||
int $code = 0,
|
||||
?Exception $previous = null
|
||||
) {
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
'Option "%s" must be %s, got "%s".',
|
||||
$option,
|
||||
$expected,
|
||||
is_object($value) ? $value::class : (null === $value ? '<null>' : gettype($value) . '#' . $value),
|
||||
),
|
||||
$code,
|
||||
$previous,
|
||||
);
|
||||
}
|
||||
}
|
16
vendor/sebastian/diff/src/Exception/Exception.php
vendored
Normal file
16
vendor/sebastian/diff/src/Exception/Exception.php
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface Exception extends Throwable
|
||||
{
|
||||
}
|
14
vendor/sebastian/diff/src/Exception/InvalidArgumentException.php
vendored
Normal file
14
vendor/sebastian/diff/src/Exception/InvalidArgumentException.php
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
class InvalidArgumentException extends \InvalidArgumentException implements Exception
|
||||
{
|
||||
}
|
66
vendor/sebastian/diff/src/Line.php
vendored
Normal file
66
vendor/sebastian/diff/src/Line.php
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
final class Line
|
||||
{
|
||||
public const ADDED = 1;
|
||||
public const REMOVED = 2;
|
||||
public const UNCHANGED = 3;
|
||||
private int $type;
|
||||
private string $content;
|
||||
|
||||
public function __construct(int $type = self::UNCHANGED, string $content = '')
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function content(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function type(): int
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function isAdded(): bool
|
||||
{
|
||||
return $this->type === self::ADDED;
|
||||
}
|
||||
|
||||
public function isRemoved(): bool
|
||||
{
|
||||
return $this->type === self::REMOVED;
|
||||
}
|
||||
|
||||
public function isUnchanged(): bool
|
||||
{
|
||||
return $this->type === self::UNCHANGED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function getType(): int
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
18
vendor/sebastian/diff/src/LongestCommonSubsequenceCalculator.php
vendored
Normal file
18
vendor/sebastian/diff/src/LongestCommonSubsequenceCalculator.php
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
interface LongestCommonSubsequenceCalculator
|
||||
{
|
||||
/**
|
||||
* Calculates the longest common subsequence of two arrays.
|
||||
*/
|
||||
public function calculate(array $from, array $to): array;
|
||||
}
|
97
vendor/sebastian/diff/src/MemoryEfficientLongestCommonSubsequenceCalculator.php
vendored
Normal file
97
vendor/sebastian/diff/src/MemoryEfficientLongestCommonSubsequenceCalculator.php
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use function array_fill;
|
||||
use function array_merge;
|
||||
use function array_reverse;
|
||||
use function array_slice;
|
||||
use function count;
|
||||
use function in_array;
|
||||
use function max;
|
||||
|
||||
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculate(array $from, array $to): array
|
||||
{
|
||||
$cFrom = count($from);
|
||||
$cTo = count($to);
|
||||
|
||||
if ($cFrom === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($cFrom === 1) {
|
||||
if (in_array($from[0], $to, true)) {
|
||||
return [$from[0]];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
$i = (int) ($cFrom / 2);
|
||||
$fromStart = array_slice($from, 0, $i);
|
||||
$fromEnd = array_slice($from, $i);
|
||||
$llB = $this->length($fromStart, $to);
|
||||
$llE = $this->length(array_reverse($fromEnd), array_reverse($to));
|
||||
$jMax = 0;
|
||||
$max = 0;
|
||||
|
||||
for ($j = 0; $j <= $cTo; $j++) {
|
||||
$m = $llB[$j] + $llE[$cTo - $j];
|
||||
|
||||
if ($m >= $max) {
|
||||
$max = $m;
|
||||
$jMax = $j;
|
||||
}
|
||||
}
|
||||
|
||||
$toStart = array_slice($to, 0, $jMax);
|
||||
$toEnd = array_slice($to, $jMax);
|
||||
|
||||
return array_merge(
|
||||
$this->calculate($fromStart, $toStart),
|
||||
$this->calculate($fromEnd, $toEnd),
|
||||
);
|
||||
}
|
||||
|
||||
private function length(array $from, array $to): array
|
||||
{
|
||||
$current = array_fill(0, count($to) + 1, 0);
|
||||
$cFrom = count($from);
|
||||
$cTo = count($to);
|
||||
|
||||
for ($i = 0; $i < $cFrom; $i++) {
|
||||
$prev = $current;
|
||||
|
||||
for ($j = 0; $j < $cTo; $j++) {
|
||||
if ($from[$i] === $to[$j]) {
|
||||
$current[$j + 1] = $prev[$j] + 1;
|
||||
} else {
|
||||
/**
|
||||
* @noinspection PhpConditionCanBeReplacedWithMinMaxCallInspection
|
||||
*
|
||||
* We do not use max() here to avoid the function call overhead
|
||||
*/
|
||||
if ($current[$j] > $prev[$j + 1]) {
|
||||
$current[$j + 1] = $current[$j];
|
||||
} else {
|
||||
$current[$j + 1] = $prev[$j + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
}
|
52
vendor/sebastian/diff/src/Output/AbstractChunkOutputBuilder.php
vendored
Normal file
52
vendor/sebastian/diff/src/Output/AbstractChunkOutputBuilder.php
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
use function count;
|
||||
|
||||
abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
|
||||
{
|
||||
/**
|
||||
* Takes input of the diff array and returns the common parts.
|
||||
* Iterates through diff line by line.
|
||||
*/
|
||||
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
|
||||
{
|
||||
$diffSize = count($diff);
|
||||
$capturing = false;
|
||||
$chunkStart = 0;
|
||||
$chunkSize = 0;
|
||||
$commonChunks = [];
|
||||
|
||||
for ($i = 0; $i < $diffSize; $i++) {
|
||||
if ($diff[$i][1] === 0 /* OLD */) {
|
||||
if ($capturing === false) {
|
||||
$capturing = true;
|
||||
$chunkStart = $i;
|
||||
$chunkSize = 0;
|
||||
} else {
|
||||
$chunkSize++;
|
||||
}
|
||||
} elseif ($capturing !== false) {
|
||||
if ($chunkSize >= $lineThreshold) {
|
||||
$commonChunks[$chunkStart] = $chunkStart + $chunkSize;
|
||||
}
|
||||
|
||||
$capturing = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($capturing !== false && $chunkSize >= $lineThreshold) {
|
||||
$commonChunks[$chunkStart] = $chunkStart + $chunkSize;
|
||||
}
|
||||
|
||||
return $commonChunks;
|
||||
}
|
||||
}
|
70
vendor/sebastian/diff/src/Output/DiffOnlyOutputBuilder.php
vendored
Normal file
70
vendor/sebastian/diff/src/Output/DiffOnlyOutputBuilder.php
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
use function fclose;
|
||||
use function fopen;
|
||||
use function fwrite;
|
||||
use function str_ends_with;
|
||||
use function stream_get_contents;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
* Builds a diff string representation in a loose unified diff format
|
||||
* listing only changes lines. Does not include line numbers.
|
||||
*/
|
||||
final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
|
||||
{
|
||||
private string $header;
|
||||
|
||||
public function __construct(string $header = "--- Original\n+++ New\n")
|
||||
{
|
||||
$this->header = $header;
|
||||
}
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
|
||||
if ('' !== $this->header) {
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
if (!str_ends_with($this->header, "\n")) {
|
||||
fwrite($buffer, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($diff as $diffEntry) {
|
||||
if ($diffEntry[1] === Differ::ADDED) {
|
||||
fwrite($buffer, '+' . $diffEntry[0]);
|
||||
} elseif ($diffEntry[1] === Differ::REMOVED) {
|
||||
fwrite($buffer, '-' . $diffEntry[0]);
|
||||
} elseif ($diffEntry[1] === Differ::DIFF_LINE_END_WARNING) {
|
||||
fwrite($buffer, ' ' . $diffEntry[0]);
|
||||
|
||||
continue; // Warnings should not be tested for line break, it will always be there
|
||||
} else { /* Not changed (old) 0 */
|
||||
continue; // we didn't write the not-changed line, so do not add a line break either
|
||||
}
|
||||
|
||||
$lc = substr($diffEntry[0], -1);
|
||||
|
||||
if ($lc !== "\n" && $lc !== "\r") {
|
||||
fwrite($buffer, "\n"); // \No newline at end of file
|
||||
}
|
||||
}
|
||||
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
fclose($buffer);
|
||||
|
||||
return $diff;
|
||||
}
|
||||
}
|
19
vendor/sebastian/diff/src/Output/DiffOutputBuilderInterface.php
vendored
Normal file
19
vendor/sebastian/diff/src/Output/DiffOutputBuilderInterface.php
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
/**
|
||||
* Defines how an output builder should take a generated
|
||||
* diff array and return a string representation of that diff.
|
||||
*/
|
||||
interface DiffOutputBuilderInterface
|
||||
{
|
||||
public function getDiff(array $diff): string;
|
||||
}
|
326
vendor/sebastian/diff/src/Output/StrictUnifiedDiffOutputBuilder.php
vendored
Normal file
326
vendor/sebastian/diff/src/Output/StrictUnifiedDiffOutputBuilder.php
vendored
Normal file
@ -0,0 +1,326 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
use function array_merge;
|
||||
use function array_splice;
|
||||
use function count;
|
||||
use function fclose;
|
||||
use function fopen;
|
||||
use function fwrite;
|
||||
use function is_bool;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
use function max;
|
||||
use function min;
|
||||
use function sprintf;
|
||||
use function stream_get_contents;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\ConfigurationException;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
* Strict Unified diff output builder.
|
||||
*
|
||||
* Generates (strict) Unified diff's (unidiffs) with hunks.
|
||||
*/
|
||||
final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
{
|
||||
private static array $default = [
|
||||
'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
|
||||
'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
|
||||
'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
|
||||
'fromFile' => null,
|
||||
'fromFileDate' => null,
|
||||
'toFile' => null,
|
||||
'toFileDate' => null,
|
||||
];
|
||||
private bool $changed;
|
||||
private bool $collapseRanges;
|
||||
|
||||
/**
|
||||
* @psalm-var positive-int
|
||||
*/
|
||||
private int $commonLineThreshold;
|
||||
private string $header;
|
||||
|
||||
/**
|
||||
* @psalm-var positive-int
|
||||
*/
|
||||
private int $contextLines;
|
||||
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$options = array_merge(self::$default, $options);
|
||||
|
||||
if (!is_bool($options['collapseRanges'])) {
|
||||
throw new ConfigurationException('collapseRanges', 'a bool', $options['collapseRanges']);
|
||||
}
|
||||
|
||||
if (!is_int($options['contextLines']) || $options['contextLines'] < 0) {
|
||||
throw new ConfigurationException('contextLines', 'an int >= 0', $options['contextLines']);
|
||||
}
|
||||
|
||||
if (!is_int($options['commonLineThreshold']) || $options['commonLineThreshold'] <= 0) {
|
||||
throw new ConfigurationException('commonLineThreshold', 'an int > 0', $options['commonLineThreshold']);
|
||||
}
|
||||
|
||||
$this->assertString($options, 'fromFile');
|
||||
$this->assertString($options, 'toFile');
|
||||
$this->assertStringOrNull($options, 'fromFileDate');
|
||||
$this->assertStringOrNull($options, 'toFileDate');
|
||||
|
||||
$this->header = sprintf(
|
||||
"--- %s%s\n+++ %s%s\n",
|
||||
$options['fromFile'],
|
||||
null === $options['fromFileDate'] ? '' : "\t" . $options['fromFileDate'],
|
||||
$options['toFile'],
|
||||
null === $options['toFileDate'] ? '' : "\t" . $options['toFileDate'],
|
||||
);
|
||||
|
||||
$this->collapseRanges = $options['collapseRanges'];
|
||||
$this->commonLineThreshold = $options['commonLineThreshold'];
|
||||
$this->contextLines = $options['contextLines'];
|
||||
}
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
if (0 === count($diff)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->changed = false;
|
||||
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
$this->writeDiffHunks($buffer, $diff);
|
||||
|
||||
if (!$this->changed) {
|
||||
fclose($buffer);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
|
||||
fclose($buffer);
|
||||
|
||||
// If the last char is not a linebreak: add it.
|
||||
// This might happen when both the `from` and `to` do not have a trailing linebreak
|
||||
$last = substr($diff, -1);
|
||||
|
||||
return "\n" !== $last && "\r" !== $last
|
||||
? $diff . "\n"
|
||||
: $diff;
|
||||
}
|
||||
|
||||
private function writeDiffHunks($output, array $diff): void
|
||||
{
|
||||
// detect "No newline at end of file" and insert into `$diff` if needed
|
||||
|
||||
$upperLimit = count($diff);
|
||||
|
||||
if (0 === $diff[$upperLimit - 1][1]) {
|
||||
$lc = substr($diff[$upperLimit - 1][0], -1);
|
||||
|
||||
if ("\n" !== $lc) {
|
||||
array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
} else {
|
||||
// search back for the last `+` and `-` line,
|
||||
// check if it has a trailing linebreak, else add a warning under it
|
||||
$toFind = [1 => true, 2 => true];
|
||||
|
||||
for ($i = $upperLimit - 1; $i >= 0; $i--) {
|
||||
if (isset($toFind[$diff[$i][1]])) {
|
||||
unset($toFind[$diff[$i][1]]);
|
||||
$lc = substr($diff[$i][0], -1);
|
||||
|
||||
if ("\n" !== $lc) {
|
||||
array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
|
||||
if (!count($toFind)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write hunks to output buffer
|
||||
|
||||
$cutOff = max($this->commonLineThreshold, $this->contextLines);
|
||||
$hunkCapture = false;
|
||||
$sameCount = $toRange = $fromRange = 0;
|
||||
$toStart = $fromStart = 1;
|
||||
$i = 0;
|
||||
|
||||
/** @var int $i */
|
||||
foreach ($diff as $i => $entry) {
|
||||
if (0 === $entry[1]) { // same
|
||||
if (false === $hunkCapture) {
|
||||
$fromStart++;
|
||||
$toStart++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sameCount++;
|
||||
$toRange++;
|
||||
$fromRange++;
|
||||
|
||||
if ($sameCount === $cutOff) {
|
||||
$contextStartOffset = ($hunkCapture - $this->contextLines) < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines;
|
||||
|
||||
// note: $contextEndOffset = $this->contextLines;
|
||||
//
|
||||
// because we never go beyond the end of the diff.
|
||||
// with the cutoff/contextlines here the follow is never true;
|
||||
//
|
||||
// if ($i - $cutOff + $this->contextLines + 1 > \count($diff)) {
|
||||
// $contextEndOffset = count($diff) - 1;
|
||||
// }
|
||||
//
|
||||
// ; that would be true for a trailing incomplete hunk case which is dealt with after this loop
|
||||
|
||||
$this->writeHunk(
|
||||
$diff,
|
||||
$hunkCapture - $contextStartOffset,
|
||||
$i - $cutOff + $this->contextLines + 1,
|
||||
$fromStart - $contextStartOffset,
|
||||
$fromRange - $cutOff + $contextStartOffset + $this->contextLines,
|
||||
$toStart - $contextStartOffset,
|
||||
$toRange - $cutOff + $contextStartOffset + $this->contextLines,
|
||||
$output,
|
||||
);
|
||||
|
||||
$fromStart += $fromRange;
|
||||
$toStart += $toRange;
|
||||
|
||||
$hunkCapture = false;
|
||||
$sameCount = $toRange = $fromRange = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sameCount = 0;
|
||||
|
||||
if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->changed = true;
|
||||
|
||||
if (false === $hunkCapture) {
|
||||
$hunkCapture = $i;
|
||||
}
|
||||
|
||||
if (Differ::ADDED === $entry[1]) { // added
|
||||
$toRange++;
|
||||
}
|
||||
|
||||
if (Differ::REMOVED === $entry[1]) { // removed
|
||||
$fromRange++;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $hunkCapture) {
|
||||
return;
|
||||
}
|
||||
|
||||
// we end here when cutoff (commonLineThreshold) was not reached, but we were capturing a hunk,
|
||||
// do not render hunk till end automatically because the number of context lines might be less than the commonLineThreshold
|
||||
|
||||
$contextStartOffset = $hunkCapture - $this->contextLines < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines;
|
||||
|
||||
// prevent trying to write out more common lines than there are in the diff _and_
|
||||
// do not write more than configured through the context lines
|
||||
$contextEndOffset = min($sameCount, $this->contextLines);
|
||||
|
||||
$fromRange -= $sameCount;
|
||||
$toRange -= $sameCount;
|
||||
|
||||
$this->writeHunk(
|
||||
$diff,
|
||||
$hunkCapture - $contextStartOffset,
|
||||
$i - $sameCount + $contextEndOffset + 1,
|
||||
$fromStart - $contextStartOffset,
|
||||
$fromRange + $contextStartOffset + $contextEndOffset,
|
||||
$toStart - $contextStartOffset,
|
||||
$toRange + $contextStartOffset + $contextEndOffset,
|
||||
$output,
|
||||
);
|
||||
}
|
||||
|
||||
private function writeHunk(
|
||||
array $diff,
|
||||
int $diffStartIndex,
|
||||
int $diffEndIndex,
|
||||
int $fromStart,
|
||||
int $fromRange,
|
||||
int $toStart,
|
||||
int $toRange,
|
||||
$output
|
||||
): void {
|
||||
fwrite($output, '@@ -' . $fromStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $fromRange) {
|
||||
fwrite($output, ',' . $fromRange);
|
||||
}
|
||||
|
||||
fwrite($output, ' +' . $toStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $toRange) {
|
||||
fwrite($output, ',' . $toRange);
|
||||
}
|
||||
|
||||
fwrite($output, " @@\n");
|
||||
|
||||
for ($i = $diffStartIndex; $i < $diffEndIndex; $i++) {
|
||||
if ($diff[$i][1] === Differ::ADDED) {
|
||||
$this->changed = true;
|
||||
fwrite($output, '+' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::REMOVED) {
|
||||
$this->changed = true;
|
||||
fwrite($output, '-' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::OLD) {
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
$this->changed = true;
|
||||
fwrite($output, $diff[$i][0]);
|
||||
}
|
||||
// } elseif ($diff[$i][1] === Differ::DIFF_LINE_END_WARNING) { // custom comment inserted by PHPUnit/diff package
|
||||
// skip
|
||||
// } else {
|
||||
// unknown/invalid
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
private function assertString(array $options, string $option): void
|
||||
{
|
||||
if (!is_string($options[$option])) {
|
||||
throw new ConfigurationException($option, 'a string', $options[$option]);
|
||||
}
|
||||
}
|
||||
|
||||
private function assertStringOrNull(array $options, string $option): void
|
||||
{
|
||||
if (null !== $options[$option] && !is_string($options[$option])) {
|
||||
throw new ConfigurationException($option, 'a string or <null>', $options[$option]);
|
||||
}
|
||||
}
|
||||
}
|
257
vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php
vendored
Normal file
257
vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php
vendored
Normal file
@ -0,0 +1,257 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
use function array_splice;
|
||||
use function count;
|
||||
use function fclose;
|
||||
use function fopen;
|
||||
use function fwrite;
|
||||
use function max;
|
||||
use function min;
|
||||
use function str_ends_with;
|
||||
use function stream_get_contents;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
* Builds a diff string representation in unified diff format in chunks.
|
||||
*/
|
||||
final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
{
|
||||
private bool $collapseRanges = true;
|
||||
private int $commonLineThreshold = 6;
|
||||
|
||||
/**
|
||||
* @psalm-var positive-int
|
||||
*/
|
||||
private int $contextLines = 3;
|
||||
private string $header;
|
||||
private bool $addLineNumbers;
|
||||
|
||||
public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false)
|
||||
{
|
||||
$this->header = $header;
|
||||
$this->addLineNumbers = $addLineNumbers;
|
||||
}
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
|
||||
if ('' !== $this->header) {
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
if (!str_ends_with($this->header, "\n")) {
|
||||
fwrite($buffer, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (0 !== count($diff)) {
|
||||
$this->writeDiffHunks($buffer, $diff);
|
||||
}
|
||||
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
|
||||
fclose($buffer);
|
||||
|
||||
// If the diff is non-empty and last char is not a linebreak: add it.
|
||||
// This might happen when both the `from` and `to` do not have a trailing linebreak
|
||||
$last = substr($diff, -1);
|
||||
|
||||
return '' !== $diff && "\n" !== $last && "\r" !== $last
|
||||
? $diff . "\n"
|
||||
: $diff;
|
||||
}
|
||||
|
||||
private function writeDiffHunks($output, array $diff): void
|
||||
{
|
||||
// detect "No newline at end of file" and insert into `$diff` if needed
|
||||
|
||||
$upperLimit = count($diff);
|
||||
|
||||
if (0 === $diff[$upperLimit - 1][1]) {
|
||||
$lc = substr($diff[$upperLimit - 1][0], -1);
|
||||
|
||||
if ("\n" !== $lc) {
|
||||
array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
} else {
|
||||
// search back for the last `+` and `-` line,
|
||||
// check if it has trailing linebreak, else add a warning under it
|
||||
$toFind = [1 => true, 2 => true];
|
||||
|
||||
for ($i = $upperLimit - 1; $i >= 0; $i--) {
|
||||
if (isset($toFind[$diff[$i][1]])) {
|
||||
unset($toFind[$diff[$i][1]]);
|
||||
$lc = substr($diff[$i][0], -1);
|
||||
|
||||
if ("\n" !== $lc) {
|
||||
array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
|
||||
if (!count($toFind)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write hunks to output buffer
|
||||
|
||||
$cutOff = max($this->commonLineThreshold, $this->contextLines);
|
||||
$hunkCapture = false;
|
||||
$sameCount = $toRange = $fromRange = 0;
|
||||
$toStart = $fromStart = 1;
|
||||
$i = 0;
|
||||
|
||||
/** @var int $i */
|
||||
foreach ($diff as $i => $entry) {
|
||||
if (0 === $entry[1]) { // same
|
||||
if (false === $hunkCapture) {
|
||||
$fromStart++;
|
||||
$toStart++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sameCount++;
|
||||
$toRange++;
|
||||
$fromRange++;
|
||||
|
||||
if ($sameCount === $cutOff) {
|
||||
$contextStartOffset = ($hunkCapture - $this->contextLines) < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines;
|
||||
|
||||
// note: $contextEndOffset = $this->contextLines;
|
||||
//
|
||||
// because we never go beyond the end of the diff.
|
||||
// with the cutoff/contextlines here the follow is never true;
|
||||
//
|
||||
// if ($i - $cutOff + $this->contextLines + 1 > \count($diff)) {
|
||||
// $contextEndOffset = count($diff) - 1;
|
||||
// }
|
||||
//
|
||||
// ; that would be true for a trailing incomplete hunk case which is dealt with after this loop
|
||||
|
||||
$this->writeHunk(
|
||||
$diff,
|
||||
$hunkCapture - $contextStartOffset,
|
||||
$i - $cutOff + $this->contextLines + 1,
|
||||
$fromStart - $contextStartOffset,
|
||||
$fromRange - $cutOff + $contextStartOffset + $this->contextLines,
|
||||
$toStart - $contextStartOffset,
|
||||
$toRange - $cutOff + $contextStartOffset + $this->contextLines,
|
||||
$output,
|
||||
);
|
||||
|
||||
$fromStart += $fromRange;
|
||||
$toStart += $toRange;
|
||||
|
||||
$hunkCapture = false;
|
||||
$sameCount = $toRange = $fromRange = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$sameCount = 0;
|
||||
|
||||
if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false === $hunkCapture) {
|
||||
$hunkCapture = $i;
|
||||
}
|
||||
|
||||
if (Differ::ADDED === $entry[1]) {
|
||||
$toRange++;
|
||||
}
|
||||
|
||||
if (Differ::REMOVED === $entry[1]) {
|
||||
$fromRange++;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $hunkCapture) {
|
||||
return;
|
||||
}
|
||||
|
||||
// we end here when cutoff (commonLineThreshold) was not reached, but we were capturing a hunk,
|
||||
// do not render hunk till end automatically because the number of context lines might be less than the commonLineThreshold
|
||||
|
||||
$contextStartOffset = $hunkCapture - $this->contextLines < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines;
|
||||
|
||||
// prevent trying to write out more common lines than there are in the diff _and_
|
||||
// do not write more than configured through the context lines
|
||||
$contextEndOffset = min($sameCount, $this->contextLines);
|
||||
|
||||
$fromRange -= $sameCount;
|
||||
$toRange -= $sameCount;
|
||||
|
||||
$this->writeHunk(
|
||||
$diff,
|
||||
$hunkCapture - $contextStartOffset,
|
||||
$i - $sameCount + $contextEndOffset + 1,
|
||||
$fromStart - $contextStartOffset,
|
||||
$fromRange + $contextStartOffset + $contextEndOffset,
|
||||
$toStart - $contextStartOffset,
|
||||
$toRange + $contextStartOffset + $contextEndOffset,
|
||||
$output,
|
||||
);
|
||||
}
|
||||
|
||||
private function writeHunk(
|
||||
array $diff,
|
||||
int $diffStartIndex,
|
||||
int $diffEndIndex,
|
||||
int $fromStart,
|
||||
int $fromRange,
|
||||
int $toStart,
|
||||
int $toRange,
|
||||
$output
|
||||
): void {
|
||||
if ($this->addLineNumbers) {
|
||||
fwrite($output, '@@ -' . $fromStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $fromRange) {
|
||||
fwrite($output, ',' . $fromRange);
|
||||
}
|
||||
|
||||
fwrite($output, ' +' . $toStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $toRange) {
|
||||
fwrite($output, ',' . $toRange);
|
||||
}
|
||||
|
||||
fwrite($output, " @@\n");
|
||||
} else {
|
||||
fwrite($output, "@@ @@\n");
|
||||
}
|
||||
|
||||
for ($i = $diffStartIndex; $i < $diffEndIndex; $i++) {
|
||||
if ($diff[$i][1] === Differ::ADDED) {
|
||||
fwrite($output, '+' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::REMOVED) {
|
||||
fwrite($output, '-' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::OLD) {
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
fwrite($output, "\n"); // $diff[$i][0]
|
||||
} else { /* Not changed (old) Differ::OLD or Warning Differ::DIFF_LINE_END_WARNING */
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
112
vendor/sebastian/diff/src/Parser.php
vendored
Normal file
112
vendor/sebastian/diff/src/Parser.php
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use function array_pop;
|
||||
use function assert;
|
||||
use function count;
|
||||
use function max;
|
||||
use function preg_match;
|
||||
use function preg_split;
|
||||
|
||||
/**
|
||||
* Unified diff parser.
|
||||
*/
|
||||
final class Parser
|
||||
{
|
||||
/**
|
||||
* @return Diff[]
|
||||
*/
|
||||
public function parse(string $string): array
|
||||
{
|
||||
$lines = preg_split('(\r\n|\r|\n)', $string);
|
||||
|
||||
if (!empty($lines) && $lines[count($lines) - 1] === '') {
|
||||
array_pop($lines);
|
||||
}
|
||||
|
||||
$lineCount = count($lines);
|
||||
$diffs = [];
|
||||
$diff = null;
|
||||
$collected = [];
|
||||
|
||||
for ($i = 0; $i < $lineCount; $i++) {
|
||||
if (preg_match('#^---\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i], $fromMatch) &&
|
||||
preg_match('#^\\+\\+\\+\\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i + 1], $toMatch)) {
|
||||
if ($diff !== null) {
|
||||
$this->parseFileDiff($diff, $collected);
|
||||
|
||||
$diffs[] = $diff;
|
||||
$collected = [];
|
||||
}
|
||||
|
||||
assert(!empty($fromMatch['file']));
|
||||
assert(!empty($toMatch['file']));
|
||||
|
||||
$diff = new Diff($fromMatch['file'], $toMatch['file']);
|
||||
|
||||
$i++;
|
||||
} else {
|
||||
if (preg_match('/^(?:diff --git |index [\da-f.]+|[+-]{3} [ab])/', $lines[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$collected[] = $lines[$i];
|
||||
}
|
||||
}
|
||||
|
||||
if ($diff !== null && count($collected)) {
|
||||
$this->parseFileDiff($diff, $collected);
|
||||
|
||||
$diffs[] = $diff;
|
||||
}
|
||||
|
||||
return $diffs;
|
||||
}
|
||||
|
||||
private function parseFileDiff(Diff $diff, array $lines): void
|
||||
{
|
||||
$chunks = [];
|
||||
$chunk = null;
|
||||
$diffLines = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match, PREG_UNMATCHED_AS_NULL)) {
|
||||
$chunk = new Chunk(
|
||||
(int) $match['start'],
|
||||
isset($match['startrange']) ? max(0, (int) $match['startrange']) : 1,
|
||||
(int) $match['end'],
|
||||
isset($match['endrange']) ? max(0, (int) $match['endrange']) : 1,
|
||||
);
|
||||
|
||||
$chunks[] = $chunk;
|
||||
$diffLines = [];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
|
||||
$type = Line::UNCHANGED;
|
||||
|
||||
if ($match['type'] === '+') {
|
||||
$type = Line::ADDED;
|
||||
} elseif ($match['type'] === '-') {
|
||||
$type = Line::REMOVED;
|
||||
}
|
||||
|
||||
$diffLines[] = new Line($type, $match['line']);
|
||||
|
||||
$chunk?->setLines($diffLines);
|
||||
}
|
||||
}
|
||||
|
||||
$diff->setChunks($chunks);
|
||||
}
|
||||
}
|
82
vendor/sebastian/diff/src/TimeEfficientLongestCommonSubsequenceCalculator.php
vendored
Normal file
82
vendor/sebastian/diff/src/TimeEfficientLongestCommonSubsequenceCalculator.php
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use function array_reverse;
|
||||
use function count;
|
||||
use function max;
|
||||
use SplFixedArray;
|
||||
|
||||
final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function calculate(array $from, array $to): array
|
||||
{
|
||||
$common = [];
|
||||
$fromLength = count($from);
|
||||
$toLength = count($to);
|
||||
$width = $fromLength + 1;
|
||||
$matrix = new SplFixedArray($width * ($toLength + 1));
|
||||
|
||||
for ($i = 0; $i <= $fromLength; $i++) {
|
||||
$matrix[$i] = 0;
|
||||
}
|
||||
|
||||
for ($j = 0; $j <= $toLength; $j++) {
|
||||
$matrix[$j * $width] = 0;
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $fromLength; $i++) {
|
||||
for ($j = 1; $j <= $toLength; $j++) {
|
||||
$o = ($j * $width) + $i;
|
||||
|
||||
// don't use max() to avoid function call overhead
|
||||
$firstOrLast = $from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0;
|
||||
|
||||
if ($matrix[$o - 1] > $matrix[$o - $width]) {
|
||||
if ($firstOrLast > $matrix[$o - 1]) {
|
||||
$matrix[$o] = $firstOrLast;
|
||||
} else {
|
||||
$matrix[$o] = $matrix[$o - 1];
|
||||
}
|
||||
} else {
|
||||
if ($firstOrLast > $matrix[$o - $width]) {
|
||||
$matrix[$o] = $firstOrLast;
|
||||
} else {
|
||||
$matrix[$o] = $matrix[$o - $width];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$i = $fromLength;
|
||||
$j = $toLength;
|
||||
|
||||
while ($i > 0 && $j > 0) {
|
||||
if ($from[$i - 1] === $to[$j - 1]) {
|
||||
$common[] = $from[$i - 1];
|
||||
$i--;
|
||||
$j--;
|
||||
} else {
|
||||
$o = ($j * $width) + $i;
|
||||
|
||||
if ($matrix[$o - $width] > $matrix[$o - 1]) {
|
||||
$j--;
|
||||
} else {
|
||||
$i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_reverse($common);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user