mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-12 11:11:31 +00:00
resolved conflicts
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
# ChangeLog
|
||||
|
||||
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
|
||||
|
||||
## [5.1.1] - 2024-03-02
|
||||
|
||||
### Changed
|
||||
|
||||
* Do not use implicitly nullable parameters
|
||||
|
||||
## [5.1.0] - 2023-12-22
|
||||
|
||||
### Added
|
||||
|
||||
* `SebastianBergmann\Diff\Chunk::start()`, `SebastianBergmann\Diff\Chunk::startRange()`, `SebastianBergmann\Diff\Chunk::end()`, `SebastianBergmann\Diff\Chunk::endRange()`, and `SebastianBergmann\Diff\Chunk::lines()`
|
||||
* `SebastianBergmann\Diff\Diff::from()`, `SebastianBergmann\Diff\Diff::to()`, and `SebastianBergmann\Diff\Diff::chunks()`
|
||||
* `SebastianBergmann\Diff\Line::content()` and `SebastianBergmann\Diff\Diff::type()`
|
||||
* `SebastianBergmann\Diff\Line::isAdded()`,`SebastianBergmann\Diff\Line::isRemoved()`, and `SebastianBergmann\Diff\Line::isUnchanged()`
|
||||
|
||||
### Changed
|
||||
|
||||
* `SebastianBergmann\Diff\Diff` now implements `IteratorAggregate`, iterating over it yields the aggregated `SebastianBergmann\Diff\Chunk` objects
|
||||
* `SebastianBergmann\Diff\Chunk` now implements `IteratorAggregate`, iterating over it yields the aggregated `SebastianBergmann\Diff\Line` objects
|
||||
|
||||
### Deprecated
|
||||
|
||||
* `SebastianBergmann\Diff\Chunk::getStart()`, `SebastianBergmann\Diff\Chunk::getStartRange()`, `SebastianBergmann\Diff\Chunk::getEnd()`, `SebastianBergmann\Diff\Chunk::getEndRange()`, and `SebastianBergmann\Diff\Chunk::getLines()`
|
||||
* `SebastianBergmann\Diff\Diff::getFrom()`, `SebastianBergmann\Diff\Diff::getTo()`, and `SebastianBergmann\Diff\Diff::getChunks()`
|
||||
* `SebastianBergmann\Diff\Line::getContent()` and `SebastianBergmann\Diff\Diff::getType()`
|
||||
|
||||
## [5.0.3] - 2023-05-01
|
||||
|
||||
### Changed
|
||||
|
||||
* [#119](https://github.com/sebastianbergmann/diff/pull/119): Improve performance of `TimeEfficientLongestCommonSubsequenceCalculator`
|
||||
|
||||
## [5.0.2] - 2023-05-01
|
||||
|
||||
### Changed
|
||||
|
||||
* [#118](https://github.com/sebastianbergmann/diff/pull/118): Improve performance of `MemoryEfficientLongestCommonSubsequenceCalculator`
|
||||
|
||||
## [5.0.1] - 2023-03-23
|
||||
|
||||
### Fixed
|
||||
|
||||
* [#115](https://github.com/sebastianbergmann/diff/pull/115): `Parser::parseFileDiff()` does not handle diffs correctly that only add lines or only remove lines
|
||||
|
||||
## [5.0.0] - 2023-02-03
|
||||
|
||||
### Changed
|
||||
|
||||
* Passing a `DiffOutputBuilderInterface` instance to `Differ::__construct()` is no longer optional
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed support for PHP 7.3, PHP 7.4, and PHP 8.0
|
||||
|
||||
## [4.0.4] - 2020-10-26
|
||||
|
||||
### Fixed
|
||||
|
||||
* `SebastianBergmann\Diff\Exception` now correctly extends `\Throwable`
|
||||
|
||||
## [4.0.3] - 2020-09-28
|
||||
|
||||
### Changed
|
||||
|
||||
* Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3`
|
||||
|
||||
## [4.0.2] - 2020-06-30
|
||||
|
||||
### Added
|
||||
|
||||
* This component is now supported on PHP 8
|
||||
|
||||
## [4.0.1] - 2020-05-08
|
||||
|
||||
### Fixed
|
||||
|
||||
* [#99](https://github.com/sebastianbergmann/diff/pull/99): Regression in unified diff output of identical strings
|
||||
|
||||
## [4.0.0] - 2020-02-07
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed support for PHP 7.1 and PHP 7.2
|
||||
|
||||
## [3.0.2] - 2019-02-04
|
||||
|
||||
### Changed
|
||||
|
||||
* `Chunk::setLines()` now ensures that the `$lines` array only contains `Line` objects
|
||||
|
||||
## [3.0.1] - 2018-06-10
|
||||
|
||||
### Fixed
|
||||
|
||||
* Removed `"minimum-stability": "dev",` from `composer.json`
|
||||
|
||||
## [3.0.0] - 2018-02-01
|
||||
|
||||
* The `StrictUnifiedDiffOutputBuilder` implementation of the `DiffOutputBuilderInterface` was added
|
||||
|
||||
### Changed
|
||||
|
||||
* The default `DiffOutputBuilderInterface` implementation now generates context lines (unchanged lines)
|
||||
|
||||
### Removed
|
||||
|
||||
* Removed support for PHP 7.0
|
||||
|
||||
### Fixed
|
||||
|
||||
* [#70](https://github.com/sebastianbergmann/diff/issues/70): Diffing of arrays no longer works
|
||||
|
||||
## [2.0.1] - 2017-08-03
|
||||
|
||||
### Fixed
|
||||
|
||||
* [#66](https://github.com/sebastianbergmann/diff/pull/66): Restored backwards compatibility for PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3
|
||||
|
||||
## [2.0.0] - 2017-07-11 [YANKED]
|
||||
|
||||
### Added
|
||||
|
||||
* [#64](https://github.com/sebastianbergmann/diff/pull/64): Show line numbers for chunks of a diff
|
||||
|
||||
### Removed
|
||||
|
||||
* This component is no longer supported on PHP 5.6
|
||||
|
||||
[5.1.1]: https://github.com/sebastianbergmann/diff/compare/5.1.0...5.1.1
|
||||
[5.1.0]: https://github.com/sebastianbergmann/diff/compare/5.0.3...5.1.0
|
||||
[5.0.3]: https://github.com/sebastianbergmann/diff/compare/5.0.2...5.0.3
|
||||
[5.0.2]: https://github.com/sebastianbergmann/diff/compare/5.0.1...5.0.2
|
||||
[5.0.1]: https://github.com/sebastianbergmann/diff/compare/5.0.0...5.0.1
|
||||
[5.0.0]: https://github.com/sebastianbergmann/diff/compare/4.0.4...5.0.0
|
||||
[4.0.4]: https://github.com/sebastianbergmann/diff/compare/4.0.3...4.0.4
|
||||
[4.0.3]: https://github.com/sebastianbergmann/diff/compare/4.0.2...4.0.3
|
||||
[4.0.2]: https://github.com/sebastianbergmann/diff/compare/4.0.1...4.0.2
|
||||
[4.0.1]: https://github.com/sebastianbergmann/diff/compare/4.0.0...4.0.1
|
||||
[4.0.0]: https://github.com/sebastianbergmann/diff/compare/3.0.2...4.0.0
|
||||
[3.0.2]: https://github.com/sebastianbergmann/diff/compare/3.0.1...3.0.2
|
||||
[3.0.1]: https://github.com/sebastianbergmann/diff/compare/3.0.0...3.0.1
|
||||
[3.0.0]: https://github.com/sebastianbergmann/diff/compare/2.0...3.0.0
|
||||
[2.0.1]: https://github.com/sebastianbergmann/diff/compare/c341c98ce083db77f896a0aa64f5ee7652915970...2.0.1
|
||||
[2.0.0]: https://github.com/sebastianbergmann/diff/compare/1.4...c341c98ce083db77f896a0aa64f5ee7652915970
|
||||
@@ -0,0 +1,29 @@
|
||||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2002-2024, Sebastian Bergmann
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "sebastian/diff",
|
||||
"description": "Diff implementation",
|
||||
"keywords": ["diff", "udiff", "unidiff", "unified diff"],
|
||||
"homepage": "https://github.com/sebastianbergmann/diff",
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sebastian Bergmann",
|
||||
"email": "sebastian@phpunit.de"
|
||||
},
|
||||
{
|
||||
"name": "Kore Nordmann",
|
||||
"email": "mail@kore-nordmann.de"
|
||||
}
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/sebastianbergmann/diff/issues",
|
||||
"security": "https://github.com/sebastianbergmann/diff/security/policy"
|
||||
},
|
||||
"prefer-stable": true,
|
||||
"config": {
|
||||
"platform": {
|
||||
"php": "8.1.0"
|
||||
},
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.0",
|
||||
"symfony/process": "^6.4"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"src/"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"classmap": [
|
||||
"tests/"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "5.1-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
+97
@@ -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;
|
||||
}
|
||||
}
|
||||
+326
@@ -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
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user