ergebnis / differ
Provides a composer package with a differ for creating a unified diff of two strings.
Requires
- php: ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.56.1
- ergebnis/data-provider: ^3.7.1
- ergebnis/license: ^2.9.0
- ergebnis/php-cs-fixer-config: ^6.64.1
- ergebnis/phpstan-rules: ^2.17.1
- ergebnis/phpunit-slow-test-detector: ^2.27.0
- ergebnis/rector-rules: ^1.19.0
- fakerphp/faker: ^1.24.1
- infection/infection: ~0.26.6
- phpbench/phpbench: ^1.2.14
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.2.16
- phpstan/phpstan-deprecation-rules: ^2.0.5
- phpstan/phpstan-phpunit: ^2.1.1
- phpstan/phpstan-strict-rules: ^2.1.1
- phpunit/phpunit: ^9.6.38
- rector/rector: ^2.7.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-12 17:56:25 UTC
README
This project provides a composer package with a differ for creating a unified diff of two strings.
Installation
Run
composer require ergebnis/differ
Usage
Create a MyersDiffer, then call diff() with a From and a To, each made of a Label and a Text, to receive a Diff of the lines of both Texts:
<?php declare(strict_types=1); use Ergebnis\Differ; $differ = new Differ\MyersDiffer(); $diff = $differ->diff( Differ\From::create( Differ\Label::fromString('original'), Differ\Text::fromString("foo\nbar\nbaz\n"), ), Differ\To::create( Differ\Label::fromString('changed'), Differ\Text::fromString("foo\nqux\nbaz\n"), ), ); echo $diff->toString();
This prints
--- original +++ changed @@ -1,3 +1,3 @@ foo -bar +qux baz
When both Texts are identical, Diff::isEmpty() returns true and Diff::toString() returns an empty string.
MyersDiffer implements the Differ interface, which you can use as a type declaration.
Label::fromString() throws an InvalidLabel exception when the value contains a line break, because a line break would break the header of the diff.
When both Texts end with a new line, Diff::toString() returns the same unified diff as sebastian/diff with its StrictUnifiedDiffOutputBuilder.
Changelog
The maintainers of this project record notable changes to this project in a changelog.
Contributing
The maintainers of this project suggest following the contribution guide.
Code of Conduct
The maintainers of this project ask contributors to follow the code of conduct.
General Support Policy
The maintainers of this project provide limited support.
PHP Version Support Policy
This project currently supports the following PHP versions:
- PHP 7.4 (has reached its end of life on November 28, 2022)
- PHP 8.0 (has reached its end of life on November 26, 2023)
- PHP 8.1 (has reached its end of life on December 31, 2025)
- PHP 8.2
- PHP 8.3
- PHP 8.4
- PHP 8.5
- PHP 8.6
The maintainers of this project add support for a PHP version following its initial release and may drop support for a PHP version when it has reached its end of life.
Security Policy
This project has a security policy.
License
This project uses the MIT license.
Credits
The algorithm for computing the diff in MyersDiffer has been adopted from SebastianBergmann\Diff\MyersDiff and SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder (originally licensed under BSD-3-Clause by Sebastian Bergmann), which implement the linear-space variant of the algorithm described by Eugene W. Myers in "An O(ND) Difference Algorithm and Its Variations".
Social
Follow @localheinz and @ergebnis on X.