upgrade to laravel 8.x

This commit is contained in:
2021-05-30 08:20:41 +00:00
parent 41b78a0599
commit 3ad6ed8bfa
2333 changed files with 113904 additions and 59058 deletions
@@ -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
View File
@@ -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
View File
@@ -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]);
}
}
}