upgrade to laravel 8.x
This commit is contained in:
Vendored
-1
@@ -7,7 +7,6 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
final class Chunk
|
||||
|
||||
Vendored
-3
@@ -7,7 +7,6 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
final class Diff
|
||||
@@ -28,8 +27,6 @@ final class Diff
|
||||
private $chunks;
|
||||
|
||||
/**
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
* @param Chunk[] $chunks
|
||||
*/
|
||||
public function __construct(string $from, string $to, array $chunks = [])
|
||||
|
||||
Vendored
+61
-64
@@ -7,21 +7,42 @@
|
||||
* 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 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.
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -40,16 +61,16 @@ final class Differ
|
||||
$this->outputBuilder = $outputBuilder;
|
||||
} elseif (null === $outputBuilder) {
|
||||
$this->outputBuilder = new UnifiedDiffOutputBuilder;
|
||||
} elseif (\is_string($outputBuilder)) {
|
||||
} 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(
|
||||
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 . '"'
|
||||
is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"'
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -58,11 +79,8 @@ final class Differ
|
||||
/**
|
||||
* Returns the diff between two arrays or strings as string.
|
||||
*
|
||||
* @param array|string $from
|
||||
* @param array|string $to
|
||||
* @param null|LongestCommonSubsequenceCalculator $lcs
|
||||
*
|
||||
* @return string
|
||||
* @param array|string $from
|
||||
* @param array|string $to
|
||||
*/
|
||||
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
|
||||
{
|
||||
@@ -89,20 +107,18 @@ final class Differ
|
||||
* @param array|string $from
|
||||
* @param array|string $to
|
||||
* @param LongestCommonSubsequenceCalculator $lcs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
|
||||
{
|
||||
if (\is_string($from)) {
|
||||
if (is_string($from)) {
|
||||
$from = $this->splitStringByLines($from);
|
||||
} elseif (!\is_array($from)) {
|
||||
} elseif (!is_array($from)) {
|
||||
throw new InvalidArgumentException('"from" must be an array or string.');
|
||||
}
|
||||
|
||||
if (\is_string($to)) {
|
||||
if (is_string($to)) {
|
||||
$to = $this->splitStringByLines($to);
|
||||
} elseif (!\is_array($to)) {
|
||||
} elseif (!is_array($to)) {
|
||||
throw new InvalidArgumentException('"to" must be an array or string.');
|
||||
}
|
||||
|
||||
@@ -112,36 +128,36 @@ final class Differ
|
||||
$lcs = $this->selectLcsImplementation($from, $to);
|
||||
}
|
||||
|
||||
$common = $lcs->calculate(\array_values($from), \array_values($to));
|
||||
$common = $lcs->calculate(array_values($from), array_values($to));
|
||||
$diff = [];
|
||||
|
||||
foreach ($start as $token) {
|
||||
$diff[] = [$token, self::OLD];
|
||||
}
|
||||
|
||||
\reset($from);
|
||||
\reset($to);
|
||||
reset($from);
|
||||
reset($to);
|
||||
|
||||
foreach ($common as $token) {
|
||||
while (($fromToken = \reset($from)) !== $token) {
|
||||
$diff[] = [\array_shift($from), self::REMOVED];
|
||||
while (($fromToken = reset($from)) !== $token) {
|
||||
$diff[] = [array_shift($from), self::REMOVED];
|
||||
}
|
||||
|
||||
while (($toToken = \reset($to)) !== $token) {
|
||||
$diff[] = [\array_shift($to), self::ADDED];
|
||||
while (($toToken = reset($to)) !== $token) {
|
||||
$diff[] = [array_shift($to), self::ADDED];
|
||||
}
|
||||
|
||||
$diff[] = [$token, self::OLD];
|
||||
|
||||
\array_shift($from);
|
||||
\array_shift($to);
|
||||
array_shift($from);
|
||||
array_shift($to);
|
||||
}
|
||||
|
||||
while (($token = \array_shift($from)) !== null) {
|
||||
while (($token = array_shift($from)) !== null) {
|
||||
$diff[] = [$token, self::REMOVED];
|
||||
}
|
||||
|
||||
while (($token = \array_shift($to)) !== null) {
|
||||
while (($token = array_shift($to)) !== null) {
|
||||
$diff[] = [$token, self::ADDED];
|
||||
}
|
||||
|
||||
@@ -150,7 +166,7 @@ final class Differ
|
||||
}
|
||||
|
||||
if ($this->detectUnmatchedLineEndings($diff)) {
|
||||
\array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
|
||||
array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
|
||||
}
|
||||
|
||||
return $diff;
|
||||
@@ -159,13 +175,11 @@ final class Differ
|
||||
/**
|
||||
* Casts variable to string if it is not a string or array.
|
||||
*
|
||||
* @param mixed $input
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
private function normalizeDiffInput($input)
|
||||
{
|
||||
if (!\is_array($input) && !\is_string($input)) {
|
||||
if (!is_array($input) && !is_string($input)) {
|
||||
return (string) $input;
|
||||
}
|
||||
|
||||
@@ -174,22 +188,12 @@ final class Differ
|
||||
|
||||
/**
|
||||
* Checks if input is string, if so it will split it line-by-line.
|
||||
*
|
||||
* @param string $input
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function splitStringByLines(string $input): array
|
||||
{
|
||||
return \preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return LongestCommonSubsequenceCalculator
|
||||
*/
|
||||
private function selectLcsImplementation(array $from, array $to): LongestCommonSubsequenceCalculator
|
||||
{
|
||||
// We do not want to use the time-efficient implementation if its memory
|
||||
@@ -208,24 +212,17 @@ final class Differ
|
||||
/**
|
||||
* Calculates the estimated memory footprint for the DP-based method.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return float|int
|
||||
*/
|
||||
private function calculateEstimatedFootprint(array $from, array $to)
|
||||
{
|
||||
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
|
||||
|
||||
return $itemSize * \min(\count($from), \count($to)) ** 2;
|
||||
return $itemSize * min(count($from), count($to)) ** 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if line ends don't match in a diff.
|
||||
*
|
||||
* @param array $diff
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function detectUnmatchedLineEndings(array $diff): bool
|
||||
{
|
||||
@@ -267,11 +264,11 @@ final class Differ
|
||||
|
||||
private function getLinebreak($line): string
|
||||
{
|
||||
if (!\is_string($line)) {
|
||||
if (!is_string($line)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$lc = \substr($line, -1);
|
||||
$lc = substr($line, -1);
|
||||
|
||||
if ("\r" === $lc) {
|
||||
return "\r";
|
||||
@@ -281,7 +278,7 @@ final class Differ
|
||||
return '';
|
||||
}
|
||||
|
||||
if ("\r\n" === \substr($line, -2)) {
|
||||
if ("\r\n" === substr($line, -2)) {
|
||||
return "\r\n";
|
||||
}
|
||||
|
||||
@@ -293,10 +290,10 @@ final class Differ
|
||||
$start = [];
|
||||
$end = [];
|
||||
|
||||
\reset($to);
|
||||
reset($to);
|
||||
|
||||
foreach ($from as $k => $v) {
|
||||
$toK = \key($to);
|
||||
$toK = key($to);
|
||||
|
||||
if ($toK === $k && $v === $to[$k]) {
|
||||
$start[$k] = $v;
|
||||
@@ -307,19 +304,19 @@ final 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 = [$fromK => $from[$fromK]] + $end;
|
||||
unset($from[$fromK], $to[$toK]);
|
||||
|
||||
@@ -7,31 +7,29 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use function get_class;
|
||||
use function gettype;
|
||||
use function is_object;
|
||||
use function sprintf;
|
||||
use Exception;
|
||||
|
||||
final class ConfigurationException extends InvalidArgumentException
|
||||
{
|
||||
/**
|
||||
* @param string $option
|
||||
* @param string $expected
|
||||
* @param mixed $value
|
||||
* @param int $code
|
||||
* @param null|\Exception $previous
|
||||
*/
|
||||
public function __construct(
|
||||
string $option,
|
||||
string $expected,
|
||||
$value,
|
||||
int $code = 0,
|
||||
\Exception $previous = null
|
||||
Exception $previous = null
|
||||
) {
|
||||
parent::__construct(
|
||||
\sprintf(
|
||||
sprintf(
|
||||
'Option "%s" must be %s, got "%s".',
|
||||
$option,
|
||||
$expected,
|
||||
\is_object($value) ? \get_class($value) : (null === $value ? '<null>' : \gettype($value) . '#' . $value)
|
||||
is_object($value) ? get_class($value) : (null === $value ? '<null>' : gettype($value) . '#' . $value)
|
||||
),
|
||||
$code,
|
||||
$previous
|
||||
|
||||
+3
-2
@@ -7,9 +7,10 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
interface Exception
|
||||
use Throwable;
|
||||
|
||||
interface Exception extends Throwable
|
||||
{
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
* 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
|
||||
|
||||
Vendored
+2
-1
@@ -7,13 +7,14 @@
|
||||
* 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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,18 +7,12 @@
|
||||
* 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.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function calculate(array $from, array $to): array;
|
||||
}
|
||||
|
||||
+21
-14
@@ -7,9 +7,16 @@
|
||||
* 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
|
||||
{
|
||||
/**
|
||||
@@ -17,15 +24,15 @@ final class MemoryEfficientLongestCommonSubsequenceCalculator implements Longest
|
||||
*/
|
||||
public function calculate(array $from, array $to): array
|
||||
{
|
||||
$cFrom = \count($from);
|
||||
$cTo = \count($to);
|
||||
$cFrom = count($from);
|
||||
$cTo = count($to);
|
||||
|
||||
if ($cFrom === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($cFrom === 1) {
|
||||
if (\in_array($from[0], $to, true)) {
|
||||
if (in_array($from[0], $to, true)) {
|
||||
return [$from[0]];
|
||||
}
|
||||
|
||||
@@ -33,10 +40,10 @@ final class MemoryEfficientLongestCommonSubsequenceCalculator implements Longest
|
||||
}
|
||||
|
||||
$i = (int) ($cFrom / 2);
|
||||
$fromStart = \array_slice($from, 0, $i);
|
||||
$fromEnd = \array_slice($from, $i);
|
||||
$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));
|
||||
$llE = $this->length(array_reverse($fromEnd), array_reverse($to));
|
||||
$jMax = 0;
|
||||
$max = 0;
|
||||
|
||||
@@ -49,10 +56,10 @@ final class MemoryEfficientLongestCommonSubsequenceCalculator implements Longest
|
||||
}
|
||||
}
|
||||
|
||||
$toStart = \array_slice($to, 0, $jMax);
|
||||
$toEnd = \array_slice($to, $jMax);
|
||||
$toStart = array_slice($to, 0, $jMax);
|
||||
$toEnd = array_slice($to, $jMax);
|
||||
|
||||
return \array_merge(
|
||||
return array_merge(
|
||||
$this->calculate($fromStart, $toStart),
|
||||
$this->calculate($fromEnd, $toEnd)
|
||||
);
|
||||
@@ -60,9 +67,9 @@ final class MemoryEfficientLongestCommonSubsequenceCalculator implements Longest
|
||||
|
||||
private function length(array $from, array $to): array
|
||||
{
|
||||
$current = \array_fill(0, \count($to) + 1, 0);
|
||||
$cFrom = \count($from);
|
||||
$cTo = \count($to);
|
||||
$current = array_fill(0, count($to) + 1, 0);
|
||||
$cFrom = count($from);
|
||||
$cTo = count($to);
|
||||
|
||||
for ($i = 0; $i < $cFrom; $i++) {
|
||||
$prev = $current;
|
||||
@@ -71,7 +78,7 @@ final class MemoryEfficientLongestCommonSubsequenceCalculator implements Longest
|
||||
if ($from[$i] === $to[$j]) {
|
||||
$current[$j + 1] = $prev[$j] + 1;
|
||||
} else {
|
||||
$current[$j + 1] = \max($current[$j], $prev[$j + 1]);
|
||||
$current[$j + 1] = max($current[$j], $prev[$j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,23 +7,19 @@
|
||||
* 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.
|
||||
*
|
||||
* @param array $diff
|
||||
* @param int $lineThreshold
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
|
||||
{
|
||||
$diffSize = \count($diff);
|
||||
$diffSize = count($diff);
|
||||
$capturing = false;
|
||||
$chunkStart = 0;
|
||||
$chunkSize = 0;
|
||||
|
||||
+16
-12
@@ -7,9 +7,13 @@
|
||||
* 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 stream_get_contents;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
@@ -30,38 +34,38 @@ final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
$buffer = \fopen('php://memory', 'r+b');
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
|
||||
if ('' !== $this->header) {
|
||||
\fwrite($buffer, $this->header);
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
if ("\n" !== \substr($this->header, -1, 1)) {
|
||||
\fwrite($buffer, "\n");
|
||||
if ("\n" !== substr($this->header, -1, 1)) {
|
||||
fwrite($buffer, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($diff as $diffEntry) {
|
||||
if ($diffEntry[1] === Differ::ADDED) {
|
||||
\fwrite($buffer, '+' . $diffEntry[0]);
|
||||
fwrite($buffer, '+' . $diffEntry[0]);
|
||||
} elseif ($diffEntry[1] === Differ::REMOVED) {
|
||||
\fwrite($buffer, '-' . $diffEntry[0]);
|
||||
fwrite($buffer, '-' . $diffEntry[0]);
|
||||
} elseif ($diffEntry[1] === Differ::DIFF_LINE_END_WARNING) {
|
||||
\fwrite($buffer, ' ' . $diffEntry[0]);
|
||||
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 non changs line, so do not add a line break either
|
||||
}
|
||||
|
||||
$lc = \substr($diffEntry[0], -1);
|
||||
$lc = substr($diffEntry[0], -1);
|
||||
|
||||
if ($lc !== "\n" && $lc !== "\r") {
|
||||
\fwrite($buffer, "\n"); // \No newline at end of file
|
||||
fwrite($buffer, "\n"); // \No newline at end of file
|
||||
}
|
||||
}
|
||||
|
||||
$diff = \stream_get_contents($buffer, -1, 0);
|
||||
\fclose($buffer);
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
fclose($buffer);
|
||||
|
||||
return $diff;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff\Output;
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,9 +7,22 @@
|
||||
* 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;
|
||||
|
||||
@@ -29,6 +42,7 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
'toFile' => null,
|
||||
'toFileDate' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
@@ -56,33 +70,26 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$options = \array_merge(self::$default, $options);
|
||||
$options = array_merge(self::$default, $options);
|
||||
|
||||
if (!\is_bool($options['collapseRanges'])) {
|
||||
if (!is_bool($options['collapseRanges'])) {
|
||||
throw new ConfigurationException('collapseRanges', 'a bool', $options['collapseRanges']);
|
||||
}
|
||||
|
||||
if (!\is_int($options['contextLines']) || $options['contextLines'] < 0) {
|
||||
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) {
|
||||
if (!is_int($options['commonLineThreshold']) || $options['commonLineThreshold'] <= 0) {
|
||||
throw new ConfigurationException('commonLineThreshold', 'an int > 0', $options['commonLineThreshold']);
|
||||
}
|
||||
|
||||
foreach (['fromFile', 'toFile'] as $option) {
|
||||
if (!\is_string($options[$option])) {
|
||||
throw new ConfigurationException($option, 'a string', $options[$option]);
|
||||
}
|
||||
}
|
||||
$this->assertString($options, 'fromFile');
|
||||
$this->assertString($options, 'toFile');
|
||||
$this->assertStringOrNull($options, 'fromFileDate');
|
||||
$this->assertStringOrNull($options, 'toFileDate');
|
||||
|
||||
foreach (['fromFileDate', 'toFileDate'] as $option) {
|
||||
if (null !== $options[$option] && !\is_string($options[$option])) {
|
||||
throw new ConfigurationException($option, 'a string or <null>', $options[$option]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->header = \sprintf(
|
||||
$this->header = sprintf(
|
||||
"--- %s%s\n+++ %s%s\n",
|
||||
$options['fromFile'],
|
||||
null === $options['fromFileDate'] ? '' : "\t" . $options['fromFileDate'],
|
||||
@@ -97,48 +104,47 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
if (0 === \count($diff)) {
|
||||
if (0 === count($diff)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->changed = false;
|
||||
|
||||
$buffer = \fopen('php://memory', 'r+b');
|
||||
\fwrite($buffer, $this->header);
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
$this->writeDiffHunks($buffer, $diff);
|
||||
|
||||
if (!$this->changed) {
|
||||
\fclose($buffer);
|
||||
fclose($buffer);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
$diff = \stream_get_contents($buffer, -1, 0);
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
|
||||
\fclose($buffer);
|
||||
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);
|
||||
$last = substr($diff, -1);
|
||||
|
||||
return "\n" !== $last && "\r" !== $last
|
||||
? $diff . "\n"
|
||||
: $diff
|
||||
;
|
||||
: $diff;
|
||||
}
|
||||
|
||||
private function writeDiffHunks($output, array $diff): void
|
||||
{
|
||||
// detect "No newline at end of file" and insert into `$diff` if needed
|
||||
|
||||
$upperLimit = \count($diff);
|
||||
$upperLimit = count($diff);
|
||||
|
||||
if (0 === $diff[$upperLimit - 1][1]) {
|
||||
$lc = \substr($diff[$upperLimit - 1][0], -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]]);
|
||||
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,
|
||||
@@ -148,13 +154,13 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
for ($i = $upperLimit - 1; $i >= 0; --$i) {
|
||||
if (isset($toFind[$diff[$i][1]])) {
|
||||
unset($toFind[$diff[$i][1]]);
|
||||
$lc = \substr($diff[$i][0], -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]]);
|
||||
array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
|
||||
if (!\count($toFind)) {
|
||||
if (!count($toFind)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -163,11 +169,13 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
|
||||
// write hunks to output buffer
|
||||
|
||||
$cutOff = \max($this->commonLineThreshold, $this->contextLines);
|
||||
$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) {
|
||||
@@ -184,8 +192,7 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
if ($sameCount === $cutOff) {
|
||||
$contextStartOffset = ($hunkCapture - $this->contextLines) < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines
|
||||
;
|
||||
: $this->contextLines;
|
||||
|
||||
// note: $contextEndOffset = $this->contextLines;
|
||||
//
|
||||
@@ -249,12 +256,11 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
|
||||
$contextStartOffset = $hunkCapture - $this->contextLines < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines
|
||||
;
|
||||
: $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);
|
||||
$contextEndOffset = min($sameCount, $this->contextLines);
|
||||
|
||||
$fromRange -= $sameCount;
|
||||
$toRange -= $sameCount;
|
||||
@@ -281,32 +287,32 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
int $toRange,
|
||||
$output
|
||||
): void {
|
||||
\fwrite($output, '@@ -' . $fromStart);
|
||||
fwrite($output, '@@ -' . $fromStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $fromRange) {
|
||||
\fwrite($output, ',' . $fromRange);
|
||||
fwrite($output, ',' . $fromRange);
|
||||
}
|
||||
|
||||
\fwrite($output, ' +' . $toStart);
|
||||
fwrite($output, ' +' . $toStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $toRange) {
|
||||
\fwrite($output, ',' . $toRange);
|
||||
fwrite($output, ',' . $toRange);
|
||||
}
|
||||
|
||||
\fwrite($output, " @@\n");
|
||||
fwrite($output, " @@\n");
|
||||
|
||||
for ($i = $diffStartIndex; $i < $diffEndIndex; ++$i) {
|
||||
if ($diff[$i][1] === Differ::ADDED) {
|
||||
$this->changed = true;
|
||||
\fwrite($output, '+' . $diff[$i][0]);
|
||||
fwrite($output, '+' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::REMOVED) {
|
||||
$this->changed = true;
|
||||
\fwrite($output, '-' . $diff[$i][0]);
|
||||
fwrite($output, '-' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::OLD) {
|
||||
\fwrite($output, ' ' . $diff[$i][0]);
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
$this->changed = true;
|
||||
\fwrite($output, $diff[$i][0]);
|
||||
fwrite($output, $diff[$i][0]);
|
||||
}
|
||||
//} elseif ($diff[$i][1] === Differ::DIFF_LINE_END_WARNING) { // custom comment inserted by PHPUnit/diff package
|
||||
// skip
|
||||
@@ -315,4 +321,18 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
-36
@@ -7,9 +7,18 @@
|
||||
* 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 stream_get_contents;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
/**
|
||||
@@ -50,45 +59,44 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
|
||||
public function getDiff(array $diff): string
|
||||
{
|
||||
$buffer = \fopen('php://memory', 'r+b');
|
||||
$buffer = fopen('php://memory', 'r+b');
|
||||
|
||||
if ('' !== $this->header) {
|
||||
\fwrite($buffer, $this->header);
|
||||
fwrite($buffer, $this->header);
|
||||
|
||||
if ("\n" !== \substr($this->header, -1, 1)) {
|
||||
\fwrite($buffer, "\n");
|
||||
if ("\n" !== substr($this->header, -1, 1)) {
|
||||
fwrite($buffer, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (0 !== \count($diff)) {
|
||||
if (0 !== count($diff)) {
|
||||
$this->writeDiffHunks($buffer, $diff);
|
||||
}
|
||||
|
||||
$diff = \stream_get_contents($buffer, -1, 0);
|
||||
$diff = stream_get_contents($buffer, -1, 0);
|
||||
|
||||
\fclose($buffer);
|
||||
fclose($buffer);
|
||||
|
||||
// If the last char is not a linebreak: add it.
|
||||
// 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);
|
||||
$last = substr($diff, -1);
|
||||
|
||||
return "\n" !== $last && "\r" !== $last
|
||||
return 0 !== strlen($diff) && "\n" !== $last && "\r" !== $last
|
||||
? $diff . "\n"
|
||||
: $diff
|
||||
;
|
||||
: $diff;
|
||||
}
|
||||
|
||||
private function writeDiffHunks($output, array $diff): void
|
||||
{
|
||||
// detect "No newline at end of file" and insert into `$diff` if needed
|
||||
|
||||
$upperLimit = \count($diff);
|
||||
$upperLimit = count($diff);
|
||||
|
||||
if (0 === $diff[$upperLimit - 1][1]) {
|
||||
$lc = \substr($diff[$upperLimit - 1][0], -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]]);
|
||||
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,
|
||||
@@ -98,13 +106,13 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
for ($i = $upperLimit - 1; $i >= 0; --$i) {
|
||||
if (isset($toFind[$diff[$i][1]])) {
|
||||
unset($toFind[$diff[$i][1]]);
|
||||
$lc = \substr($diff[$i][0], -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]]);
|
||||
array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
|
||||
}
|
||||
|
||||
if (!\count($toFind)) {
|
||||
if (!count($toFind)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -113,11 +121,13 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
|
||||
// write hunks to output buffer
|
||||
|
||||
$cutOff = \max($this->commonLineThreshold, $this->contextLines);
|
||||
$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) {
|
||||
@@ -134,8 +144,7 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
if ($sameCount === $cutOff) {
|
||||
$contextStartOffset = ($hunkCapture - $this->contextLines) < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines
|
||||
;
|
||||
: $this->contextLines;
|
||||
|
||||
// note: $contextEndOffset = $this->contextLines;
|
||||
//
|
||||
@@ -197,12 +206,11 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
|
||||
$contextStartOffset = $hunkCapture - $this->contextLines < 0
|
||||
? $hunkCapture
|
||||
: $this->contextLines
|
||||
;
|
||||
: $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);
|
||||
$contextEndOffset = min($sameCount, $this->contextLines);
|
||||
|
||||
$fromRange -= $sameCount;
|
||||
$toRange -= $sameCount;
|
||||
@@ -230,34 +238,34 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
|
||||
$output
|
||||
): void {
|
||||
if ($this->addLineNumbers) {
|
||||
\fwrite($output, '@@ -' . $fromStart);
|
||||
fwrite($output, '@@ -' . $fromStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $fromRange) {
|
||||
\fwrite($output, ',' . $fromRange);
|
||||
fwrite($output, ',' . $fromRange);
|
||||
}
|
||||
|
||||
\fwrite($output, ' +' . $toStart);
|
||||
fwrite($output, ' +' . $toStart);
|
||||
|
||||
if (!$this->collapseRanges || 1 !== $toRange) {
|
||||
\fwrite($output, ',' . $toRange);
|
||||
fwrite($output, ',' . $toRange);
|
||||
}
|
||||
|
||||
\fwrite($output, " @@\n");
|
||||
fwrite($output, " @@\n");
|
||||
} else {
|
||||
\fwrite($output, "@@ @@\n");
|
||||
fwrite($output, "@@ @@\n");
|
||||
}
|
||||
|
||||
for ($i = $diffStartIndex; $i < $diffEndIndex; ++$i) {
|
||||
if ($diff[$i][1] === Differ::ADDED) {
|
||||
\fwrite($output, '+' . $diff[$i][0]);
|
||||
fwrite($output, '+' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::REMOVED) {
|
||||
\fwrite($output, '-' . $diff[$i][0]);
|
||||
fwrite($output, '-' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::OLD) {
|
||||
\fwrite($output, ' ' . $diff[$i][0]);
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
|
||||
\fwrite($output, "\n"); // $diff[$i][0]
|
||||
fwrite($output, "\n"); // $diff[$i][0]
|
||||
} else { /* Not changed (old) Differ::OLD or Warning Differ::DIFF_LINE_END_WARNING */
|
||||
\fwrite($output, ' ' . $diff[$i][0]);
|
||||
fwrite($output, ' ' . $diff[$i][0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+21
-17
@@ -7,35 +7,38 @@
|
||||
* 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 count;
|
||||
use function max;
|
||||
use function preg_match;
|
||||
use function preg_split;
|
||||
|
||||
/**
|
||||
* Unified diff parser.
|
||||
*/
|
||||
final class Parser
|
||||
{
|
||||
/**
|
||||
* @param string $string
|
||||
*
|
||||
* @return Diff[]
|
||||
*/
|
||||
public function parse(string $string): array
|
||||
{
|
||||
$lines = \preg_split('(\r\n|\r|\n)', $string);
|
||||
$lines = preg_split('(\r\n|\r|\n)', $string);
|
||||
|
||||
if (!empty($lines) && $lines[\count($lines) - 1] === '') {
|
||||
\array_pop($lines);
|
||||
if (!empty($lines) && $lines[count($lines) - 1] === '') {
|
||||
array_pop($lines);
|
||||
}
|
||||
|
||||
$lineCount = \count($lines);
|
||||
$lineCount = count($lines);
|
||||
$diffs = [];
|
||||
$diff = null;
|
||||
$collected = [];
|
||||
|
||||
for ($i = 0; $i < $lineCount; ++$i) {
|
||||
if (\preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) &&
|
||||
\preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) {
|
||||
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);
|
||||
|
||||
@@ -47,7 +50,7 @@ final class Parser
|
||||
|
||||
++$i;
|
||||
} else {
|
||||
if (\preg_match('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/', $lines[$i])) {
|
||||
if (preg_match('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/', $lines[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -55,7 +58,7 @@ final class Parser
|
||||
}
|
||||
}
|
||||
|
||||
if ($diff !== null && \count($collected)) {
|
||||
if ($diff !== null && count($collected)) {
|
||||
$this->parseFileDiff($diff, $collected);
|
||||
|
||||
$diffs[] = $diff;
|
||||
@@ -66,16 +69,17 @@ final class Parser
|
||||
|
||||
private function parseFileDiff(Diff $diff, array $lines): void
|
||||
{
|
||||
$chunks = [];
|
||||
$chunk = null;
|
||||
$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)) {
|
||||
if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
|
||||
$chunk = new Chunk(
|
||||
(int) $match['start'],
|
||||
isset($match['startrange']) ? \max(1, (int) $match['startrange']) : 1,
|
||||
isset($match['startrange']) ? max(1, (int) $match['startrange']) : 1,
|
||||
(int) $match['end'],
|
||||
isset($match['endrange']) ? \max(1, (int) $match['endrange']) : 1
|
||||
isset($match['endrange']) ? max(1, (int) $match['endrange']) : 1
|
||||
);
|
||||
|
||||
$chunks[] = $chunk;
|
||||
@@ -84,7 +88,7 @@ final class Parser
|
||||
continue;
|
||||
}
|
||||
|
||||
if (\preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
|
||||
if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
|
||||
$type = Line::UNCHANGED;
|
||||
|
||||
if ($match['type'] === '+') {
|
||||
|
||||
+10
-6
@@ -7,9 +7,13 @@
|
||||
* 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
|
||||
{
|
||||
/**
|
||||
@@ -18,10 +22,10 @@ final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCo
|
||||
public function calculate(array $from, array $to): array
|
||||
{
|
||||
$common = [];
|
||||
$fromLength = \count($from);
|
||||
$toLength = \count($to);
|
||||
$fromLength = count($from);
|
||||
$toLength = count($to);
|
||||
$width = $fromLength + 1;
|
||||
$matrix = new \SplFixedArray($width * ($toLength + 1));
|
||||
$matrix = new SplFixedArray($width * ($toLength + 1));
|
||||
|
||||
for ($i = 0; $i <= $fromLength; ++$i) {
|
||||
$matrix[$i] = 0;
|
||||
@@ -34,7 +38,7 @@ final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCo
|
||||
for ($i = 1; $i <= $fromLength; ++$i) {
|
||||
for ($j = 1; $j <= $toLength; ++$j) {
|
||||
$o = ($j * $width) + $i;
|
||||
$matrix[$o] = \max(
|
||||
$matrix[$o] = max(
|
||||
$matrix[$o - 1],
|
||||
$matrix[$o - $width],
|
||||
$from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0
|
||||
@@ -61,6 +65,6 @@ final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCo
|
||||
}
|
||||
}
|
||||
|
||||
return \array_reverse($common);
|
||||
return array_reverse($common);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user