Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Diff Laravel Package

sebastian/diff

sebastian/diff is a standalone PHP diff library, factored out of PHPUnit. It generates unified, strict unified, or diff-only output, parses unified diffs into object graphs, and supports custom output builders. Install via Composer as a project or development dependency.

View on GitHub
Deep Wiki
Context7

Diff implementation

Frequently asked questions about Diff
How do I install sebastian/diff in a Laravel project?
Run composer require sebastian/diff to add it as a project dependency. If you only need it for tests or other dev tooling, run composer require --dev sebastian/diff instead so it stays out of your production dependency set. There is nothing to publish or configure afterwards: no service provider, no config file, no migrations. The classes are available through Composer autoloading immediately.
Do I need a service provider to use sebastian/diff in Laravel?
No. sebastian/diff is framework-agnostic and ships no service provider, facade, or config file, so you instantiate Differ or Parser directly wherever you need them. That works fine inside controllers, queued jobs, Artisan commands, exception handlers, and console test reporters. The tradeoff is that nothing is resolved from the Laravel container, so if you want Differ injected you write that binding yourself.
Which PHP versions does sebastian/diff 9.x support?
The 9.x line targets a recent PHP floor (PHP 8.3-era), and each major release has dropped older PHP versions. Check the package composer.json platform constraint against every app or package you intend to ship it in before promoting it to a runtime dependency. If part of your estate runs an older PHP, pin an older major or keep the dependency dev-only.
How do I generate a unified diff between two strings in PHP?
Create a Differ with an output builder and call diff() with the original and new strings. For example, $differ = new Differ(new UnifiedDiffOutputBuilder); print $differ->diff('foo', 'bar'); produces a unified diff with --- Original and +++ New headers. Swap in StrictUnifiedDiffOutputBuilder when the output must be applied by patch or git apply.
How do I parse a Git unified diff with sebastian/diff?
Use the Parser class: (new Parser)->parse($diffText) returns an array of Diff objects made up of Chunk and Line objects you can walk programmatically. It expects unified diff input such as output from Git or a vendor patch. A bug that dropped lines resembling file headers was fixed in 9.0.1, so validate parsed results when the diff comes from an external or untrusted source.
What is the difference between UnifiedDiffOutputBuilder and StrictUnifiedDiffOutputBuilder?
UnifiedDiffOutputBuilder produces the compact unified format used by PHPUnit, while StrictUnifiedDiffOutputBuilder produces strict unified diffs with hunk headers that are compatible with patch and git apply. DiffOnlyOutputBuilder emits only the lines that differ, with no headers or context. Pick strict output when tooling must apply the diff, and the plain or diff-only formats when a human is reading it.
Can I write a custom diff output builder with sebastian/diff?
Yes. Implement DiffOutputBuilderInterface and pass an instance to Differ instead of one of the built-in builders. That lets you control headers, hunk context, coloring, or emit HTML or JSON rather than plain text. It avoids forking the library when your only requirement is a different presentation.
Should sebastian/diff be a dev dependency or a runtime dependency in Laravel?
For test-time use, keep it as a dev dependency; it almost certainly already arrives transitively through PHPUnit. If application code calls it at runtime, such as audit logging, config change rendering, or user-facing diffs, add an explicit require entry. Make sure that constraint does not collide with the version PHPUnit has pinned in your lockfile.
Is sebastian/diff safe to use in production with large diffs?
Diffing is line-oriented and scales roughly with input size and divergence, and large or heavily divergent strings can exhaust PHP memory, which is a fatal error rather than a catchable exception. Cap input size before calling Differ and move multi-megabyte comparisons into queued jobs with a raised memory limit. The library also does no binary diffing, streaming, or character-level intra-line highlighting, so those requirements need a different component.
How often does sebastian/diff introduce breaking changes, and what are the alternatives?
It is single-maintainer and released in lockstep with the PHPUnit ecosystem, and major versions have removed public API such as earlier output builders and the LCS interfaces, so expect recurring upgrades on the maintainer's schedule rather than yours. Output formatting has also changed in minor releases, so avoid golden-file tests asserting its exact bytes. If that cadence does not suit you, consider the diff functionality bundled with PHPUnit or a dedicated diff package, though few are as dependency-free.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo