This commit is contained in:
2025-02-04 12:40:43 +03:00
parent 4bcb4c60dd
commit 50343d5a87
372 changed files with 9019 additions and 6684 deletions

View File

@@ -2,12 +2,6 @@
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
## [7.0.0] - 2025-02-07
### Removed
* This component is no longer supported on PHP 8.3
## [6.0.2] - 2024-07-03
### Changed
@@ -27,7 +21,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
* `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()`
* This component is no longer supported on PHP 8.1
* Removed support for PHP 8.1
## [5.1.1] - 2024-03-02
@@ -81,7 +75,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
### Removed
* This component is no longer supported on PHP 7.3, PHP 7.4, and PHP 8.0
* Removed support for PHP 7.3, PHP 7.4, and PHP 8.0
## [4.0.4] - 2020-10-26
@@ -111,7 +105,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
### Removed
* This component is no longer supported on PHP 7.1 and PHP 7.2
* Removed support for PHP 7.1 and PHP 7.2
## [3.0.2] - 2019-02-04
@@ -135,7 +129,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
### Removed
* This component is no longer supported on PHP 7.0
* Removed support for PHP 7.0
### Fixed
@@ -157,7 +151,6 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
* This component is no longer supported on PHP 5.6
[7.0.0]: https://github.com/sebastianbergmann/diff/compare/6.0...main
[6.0.2]: https://github.com/sebastianbergmann/diff/compare/6.0.1...6.0.2
[6.0.1]: https://github.com/sebastianbergmann/diff/compare/6.0.0...6.0.1
[6.0.0]: https://github.com/sebastianbergmann/diff/compare/5.1...6.0.0

View File

@@ -1,6 +1,6 @@
BSD 3-Clause License
Copyright (c) 2002-2025, Sebastian Bergmann
Copyright (c) 2002-2024, Sebastian Bergmann
All rights reserved.
Redistribution and use in source and binary forms, with or without

View File

@@ -1,4 +1,4 @@
[![Latest Stable Version](https://poser.pugx.org/sebastian/diff/v)](https://packagist.org/packages/sebastian/diff)
[![Latest Stable Version](https://poser.pugx.org/sebastian/diff/v/stable.png)](https://packagist.org/packages/sebastian/diff)
[![CI Status](https://github.com/sebastianbergmann/diff/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/diff/actions)
[![codecov](https://codecov.io/gh/sebastianbergmann/diff/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/diff)
@@ -27,17 +27,14 @@ composer require --dev sebastian/diff
The `Differ` class can be used to generate a textual representation of the difference between two strings:
```php
<?php declare(strict_types=1);
<?php
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
$differ = new Differ(new UnifiedDiffOutputBuilder);
$differ = new Differ;
print $differ->diff('foo', 'bar');
```
The code above yields the output below:
```diff
--- Original
+++ New
@@ -46,16 +43,73 @@ The code above yields the output below:
+bar
```
The `UnifiedDiffOutputBuilder` used in the example above generates output in "unified diff"
format and is used by PHPUnit, for example.
There are three output builders available in this package:
The `StrictUnifiedDiffOutputBuilder` generates output in "strict unified diff" format with
hunks, similar to `diff -u` and compatible with `patch` or `git apply`.
#### UnifiedDiffOutputBuilder
The `DiffOnlyOutputBuilder` generates output that only contains the lines that differ.
This is default builder, which generates the output close to udiff and is used by PHPUnit.
If none of these three output builders match your use case then you can implement
`DiffOutputBuilderInterface` to generate custom output.
```php
<?php
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
$builder = new UnifiedDiffOutputBuilder(
"--- Original\n+++ New\n", // custom header
false // do not add line numbers to the diff
);
$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```
#### StrictUnifiedDiffOutputBuilder
Generates (strict) Unified diff's (unidiffs) with hunks,
similar to `diff -u` and compatible with `patch` and `git apply`.
```php
<?php
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
$builder = new StrictUnifiedDiffOutputBuilder([
'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' => '',
'fromFileDate' => null,
'toFile' => '',
'toFileDate' => null,
]);
$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```
#### DiffOnlyOutputBuilder
Output only the lines that differ.
```php
<?php
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;
$builder = new DiffOnlyOutputBuilder(
"--- Original\n+++ New\n"
);
$differ = new Differ($builder);
print $differ->diff('foo', 'bar');
```
#### DiffOutputBuilderInterface
You can pass any output builder to the `Differ` class as longs as it implements the `DiffOutputBuilderInterface`.
#### Parsing diff

View File

@@ -18,20 +18,19 @@
"issues": "https://github.com/sebastianbergmann/diff/issues",
"security": "https://github.com/sebastianbergmann/diff/security/policy"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"platform": {
"php": "8.3.0"
"php": "8.2.0"
},
"optimize-autoloader": true,
"sort-packages": true
},
"require": {
"php": ">=8.3"
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "^12.0-dev",
"phpunit/phpunit": "^11.0",
"symfony/process": "^4.2 || ^5"
},
"autoload": {
@@ -46,7 +45,7 @@
},
"extra": {
"branch-alias": {
"dev-main": "7.0-dev"
"dev-main": "6.0-dev"
}
}
}

View File

@@ -73,6 +73,12 @@ final class Chunk implements IteratorAggregate
*/
public function setLines(array $lines): void
{
foreach ($lines as $line) {
if (!$line instanceof Line) {
throw new InvalidArgumentException;
}
}
$this->lines = $lines;
}

View File

@@ -30,11 +30,11 @@ use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
final class Differ
{
public const int OLD = 0;
public const int ADDED = 1;
public const int REMOVED = 2;
public const int DIFF_LINE_END_WARNING = 3;
public const int NO_LINE_END_EOF_WARNING = 4;
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)
@@ -84,11 +84,11 @@ final class Differ
reset($to);
foreach ($common as $token) {
while ((/* from-token */ reset($from)) !== $token) {
while (($fromToken = reset($from)) !== $token) {
$diff[] = [array_shift($from), self::REMOVED];
}
while ((/* to-token */ reset($to)) !== $token) {
while (($toToken = reset($to)) !== $token) {
$diff[] = [array_shift($to), self::ADDED];
}
@@ -137,7 +137,7 @@ final class Differ
return new TimeEfficientLongestCommonSubsequenceCalculator;
}
private function calculateEstimatedFootprint(array $from, array $to): int
private function calculateEstimatedFootprint(array $from, array $to): float|int
{
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;

View File

@@ -11,9 +11,9 @@ namespace SebastianBergmann\Diff;
final class Line
{
public const int ADDED = 1;
public const int REMOVED = 2;
public const int UNCHANGED = 3;
public const ADDED = 1;
public const REMOVED = 2;
public const UNCHANGED = 3;
private int $type;
private string $content;

View File

@@ -15,6 +15,7 @@ use function array_reverse;
use function array_slice;
use function count;
use function in_array;
use function max;
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
{

View File

@@ -16,8 +16,6 @@ abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
/**
* Takes input of the diff array and returns the common parts.
* Iterates through diff line by line.
*
* @return array<int, positive-int>
*/
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
{

View File

@@ -9,11 +9,9 @@
*/
namespace SebastianBergmann\Diff\Output;
use function assert;
use function fclose;
use function fopen;
use function fwrite;
use function is_resource;
use function str_ends_with;
use function stream_get_contents;
use function substr;
@@ -36,8 +34,6 @@ final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
if ('' !== $this->header) {
fwrite($buffer, $this->header);

View File

@@ -11,14 +11,12 @@ namespace SebastianBergmann\Diff\Output;
use function array_merge;
use function array_splice;
use function assert;
use function count;
use function fclose;
use function fopen;
use function fwrite;
use function is_bool;
use function is_int;
use function is_resource;
use function is_string;
use function max;
use function min;
@@ -101,9 +99,6 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
$this->changed = false;
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
fwrite($buffer, $this->header);
$this->writeDiffHunks($buffer, $diff);

View File

@@ -10,12 +10,10 @@
namespace SebastianBergmann\Diff\Output;
use function array_splice;
use function assert;
use function count;
use function fclose;
use function fopen;
use function fwrite;
use function is_resource;
use function max;
use function min;
use function str_ends_with;
@@ -48,8 +46,6 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
if ('' !== $this->header) {
fwrite($buffer, $this->header);

View File

@@ -9,7 +9,6 @@
*/
namespace SebastianBergmann\Diff;
use const PREG_UNMATCHED_AS_NULL;
use function array_pop;
use function assert;
use function count;
@@ -72,9 +71,6 @@ final class Parser
return $diffs;
}
/**
* @param string[] $lines
*/
private function parseFileDiff(Diff $diff, array $lines): void
{
$chunks = [];

View File

@@ -11,6 +11,7 @@ namespace SebastianBergmann\Diff;
use function array_reverse;
use function count;
use function max;
use SplFixedArray;
final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator