Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of sebastian/diff.
*
@@ -7,181 +7,90 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Diff;
use SebastianBergmann\Diff\LCS\LongestCommonSubsequence;
use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
use SebastianBergmann\Diff\LCS\MemoryEfficientImplementation;
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 get_class;
use function gettype;
use function is_array;
use function is_object;
use function is_string;
use function key;
use function min;
use function preg_split;
use function prev;
use function reset;
use function sprintf;
use function substr;
use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
/**
* Diff implementation.
*/
class Differ
final class Differ
{
/**
* @var string
*/
private $header;
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;
/**
* @var bool
* @var DiffOutputBuilderInterface
*/
private $showNonDiffLines;
private $outputBuilder;
/**
* @param string $header
* @param bool $showNonDiffLines
* @param DiffOutputBuilderInterface $outputBuilder
*
* @throws InvalidArgumentException
*/
public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLines = true)
public function __construct($outputBuilder = null)
{
$this->header = $header;
$this->showNonDiffLines = $showNonDiffLines;
if ($outputBuilder instanceof DiffOutputBuilderInterface) {
$this->outputBuilder = $outputBuilder;
} elseif (null === $outputBuilder) {
$this->outputBuilder = new UnifiedDiffOutputBuilder;
} elseif (is_string($outputBuilder)) {
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
// @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
// @deprecated
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
} else {
throw new InvalidArgumentException(
sprintf(
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"'
)
);
}
}
/**
* Returns the diff between two arrays or strings as string.
*
* @param array|string $from
* @param array|string $to
* @param LongestCommonSubsequence $lcs
*
* @return string
* @param array|string $from
* @param array|string $to
*/
public function diff($from, $to, LongestCommonSubsequence $lcs = null)
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
{
$from = $this->validateDiffInput($from);
$to = $this->validateDiffInput($to);
$diff = $this->diffToArray($from, $to, $lcs);
$old = $this->checkIfDiffInOld($diff);
$start = isset($old[0]) ? $old[0] : 0;
$end = \count($diff);
$diff = $this->diffToArray(
$this->normalizeDiffInput($from),
$this->normalizeDiffInput($to),
$lcs
);
if ($tmp = \array_search($end, $old)) {
$end = $tmp;
}
return $this->getBuffer($diff, $old, $start, $end);
}
/**
* Casts variable to string if it is not a string or array.
*
* @param mixed $input
*
* @return string
*/
private function validateDiffInput($input)
{
if (!\is_array($input) && !\is_string($input)) {
return (string) $input;
}
return $input;
}
/**
* Takes input of the diff array and returns the old array.
* Iterates through diff line by line,
*
* @param array $diff
*
* @return array
*/
private function checkIfDiffInOld(array $diff)
{
$inOld = false;
$i = 0;
$old = array();
foreach ($diff as $line) {
if ($line[1] === 0 /* OLD */) {
if ($inOld === false) {
$inOld = $i;
}
} elseif ($inOld !== false) {
if (($i - $inOld) > 5) {
$old[$inOld] = $i - 1;
}
$inOld = false;
}
++$i;
}
return $old;
}
/**
* Generates buffer in string format, returning the patch.
*
* @param array $diff
* @param array $old
* @param int $start
* @param int $end
*
* @return string
*/
private function getBuffer(array $diff, array $old, $start, $end)
{
$buffer = $this->header;
if (!isset($old[$start])) {
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $start);
++$start;
}
for ($i = $start; $i < $end; $i++) {
if (isset($old[$i])) {
$i = $old[$i];
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $i);
} else {
$buffer = $this->getDiffBufferElement($diff, $buffer, $i);
}
}
return $buffer;
}
/**
* Gets individual buffer element.
*
* @param array $diff
* @param string $buffer
* @param int $diffIndex
*
* @return string
*/
private function getDiffBufferElement(array $diff, $buffer, $diffIndex)
{
if ($diff[$diffIndex][1] === 1 /* ADDED */) {
$buffer .= '+' . $diff[$diffIndex][0] . "\n";
} elseif ($diff[$diffIndex][1] === 2 /* REMOVED */) {
$buffer .= '-' . $diff[$diffIndex][0] . "\n";
} elseif ($this->showNonDiffLines === true) {
$buffer .= ' ' . $diff[$diffIndex][0] . "\n";
}
return $buffer;
}
/**
* Gets individual buffer element with opening.
*
* @param array $diff
* @param string $buffer
* @param int $diffIndex
*
* @return string
*/
private function getDiffBufferElementNew(array $diff, $buffer, $diffIndex)
{
if ($this->showNonDiffLines === true) {
$buffer .= "@@ @@\n";
}
return $this->getDiffBufferElement($diff, $buffer, $diffIndex);
return $this->outputBuilder->getDiff($diff);
}
/**
@@ -195,118 +104,97 @@ class Differ
* - 1: ADDED: $token was added to $from
* - 0: OLD: $token is not changed in $to
*
* @param array|string $from
* @param array|string $to
* @param LongestCommonSubsequence $lcs
*
* @return array
* @param array|string $from
* @param array|string $to
* @param LongestCommonSubsequenceCalculator $lcs
*/
public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
{
if (\is_string($from)) {
$fromMatches = $this->getNewLineMatches($from);
$from = $this->splitStringByLines($from);
} elseif (\is_array($from)) {
$fromMatches = array();
} else {
throw new \InvalidArgumentException('"from" must be an array or string.');
if (is_string($from)) {
$from = $this->splitStringByLines($from);
} elseif (!is_array($from)) {
throw new InvalidArgumentException('"from" must be an array or string.');
}
if (\is_string($to)) {
$toMatches = $this->getNewLineMatches($to);
$to = $this->splitStringByLines($to);
} elseif (\is_array($to)) {
$toMatches = array();
} else {
throw new \InvalidArgumentException('"to" must be an array or string.');
if (is_string($to)) {
$to = $this->splitStringByLines($to);
} elseif (!is_array($to)) {
throw new InvalidArgumentException('"to" must be an array or string.');
}
list($from, $to, $start, $end) = self::getArrayDiffParted($from, $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 = array();
if ($this->detectUnmatchedLineEndings($fromMatches, $toMatches)) {
$diff[] = array(
'#Warning: Strings contain different line endings!',
0
);
}
$common = $lcs->calculate(array_values($from), array_values($to));
$diff = [];
foreach ($start as $token) {
$diff[] = array($token, 0 /* OLD */);
$diff[] = [$token, self::OLD];
}
\reset($from);
\reset($to);
reset($from);
reset($to);
foreach ($common as $token) {
while (($fromToken = \reset($from)) !== $token) {
$diff[] = array(\array_shift($from), 2 /* REMOVED */);
while (($fromToken = reset($from)) !== $token) {
$diff[] = [array_shift($from), self::REMOVED];
}
while (($toToken = \reset($to)) !== $token) {
$diff[] = array(\array_shift($to), 1 /* ADDED */);
while (($toToken = reset($to)) !== $token) {
$diff[] = [array_shift($to), self::ADDED];
}
$diff[] = array($token, 0 /* OLD */);
$diff[] = [$token, self::OLD];
\array_shift($from);
\array_shift($to);
array_shift($from);
array_shift($to);
}
while (($token = \array_shift($from)) !== null) {
$diff[] = array($token, 2 /* REMOVED */);
while (($token = array_shift($from)) !== null) {
$diff[] = [$token, self::REMOVED];
}
while (($token = \array_shift($to)) !== null) {
$diff[] = array($token, 1 /* ADDED */);
while (($token = array_shift($to)) !== null) {
$diff[] = [$token, self::ADDED];
}
foreach ($end as $token) {
$diff[] = array($token, 0 /* OLD */);
$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;
}
/**
* Get new strings denoting new lines from a given string.
* Casts variable to string if it is not a string or array.
*
* @param string $string
*
* @return array
* @return array|string
*/
private function getNewLineMatches($string)
private function normalizeDiffInput($input)
{
\preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
if (!is_array($input) && !is_string($input)) {
return (string) $input;
}
return $stringMatches;
return $input;
}
/**
* Checks if input is string, if so it will split it line-by-line.
*
* @param string $input
*
* @return array
*/
private function splitStringByLines($input)
private function splitStringByLines(string $input): array
{
return \preg_split('(\r\n|\r|\n)', $input);
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}
/**
* @param array $from
* @param array $to
*
* @return LongestCommonSubsequence
*/
private function selectLcsImplementation(array $from, array $to)
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
@@ -315,57 +203,97 @@ class Differ
$memoryLimit = 100 * 1024 * 1024;
if ($this->calculateEstimatedFootprint($from, $to) > $memoryLimit) {
return new MemoryEfficientImplementation;
return new MemoryEfficientLongestCommonSubsequenceCalculator;
}
return new TimeEfficientImplementation;
return new TimeEfficientLongestCommonSubsequenceCalculator;
}
/**
* Calculates the estimated memory footprint for the DP-based method.
*
* @param array $from
* @param array $to
*
* @return int|float
* @return float|int
*/
private function calculateEstimatedFootprint(array $from, array $to)
{
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
return $itemSize * \pow(\min(\count($from), \count($to)), 2);
return $itemSize * min(count($from), count($to)) ** 2;
}
/**
* Returns true if line ends don't match on fromMatches and toMatches.
*
* @param array $fromMatches
* @param array $toMatches
*
* @return bool
* Returns true if line ends don't match in a diff.
*/
private function detectUnmatchedLineEndings(array $fromMatches, array $toMatches)
private function detectUnmatchedLineEndings(array $diff): bool
{
return isset($fromMatches[0], $toMatches[0]) &&
\count($fromMatches[0]) === \count($toMatches[0]) &&
$fromMatches[0] !== $toMatches[0];
$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;
}
/**
* @param array $from
* @param array $to
*
* @return array
*/
private static function getArrayDiffParted(array &$from, array &$to)
private function getLinebreak($line): string
{
$start = array();
$end = array();
if (!is_string($line)) {
return '';
}
\reset($to);
$lc = substr($line, -1);
if ("\r" === $lc) {
return "\r";
}
if ("\n" !== $lc) {
return '';
}
if ("\r\n" === substr($line, -2)) {
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);
$toK = key($to);
if ($toK === $k && $v === $to[$k]) {
$start[$k] = $v;
@@ -376,24 +304,24 @@ class Differ
}
}
\end($from);
\end($to);
end($from);
end($to);
do {
$fromK = \key($from);
$toK = \key($to);
$fromK = key($from);
$toK = key($to);
if (null === $fromK || null === $toK || \current($from) !== \current($to)) {
if (null === $fromK || null === $toK || current($from) !== current($to)) {
break;
}
\prev($from);
\prev($to);
prev($from);
prev($to);
$end = array($fromK => $from[$fromK]) + $end;
$end = [$fromK => $from[$fromK]] + $end;
unset($from[$fromK], $to[$toK]);
} while (true);
return array($from, $to, $start, $end);
return [$from, $to, $start, $end];
}
}