- How do I install sebastian/diff in a Laravel project?
- Run `composer require sebastian/diff` for production use or `composer require --dev sebastian/diff` if only needed for testing. No Laravel-specific setup is required—it’s a standalone PHP library.
- Can I use sebastian/diff with Laravel’s Pest or PHPUnit for test assertions?
- Yes. Use the `Differ` class to generate human-readable diffs for test failures. For Pest, integrate it into custom expectations, and for PHPUnit, use it in listeners or assertions to replace generic failure messages.
- What Laravel versions and PHP versions does sebastian/diff support?
- The package requires PHP 8.4+. It works with any Laravel 10+ app (or newer) since those versions support PHP 8.4+. For older Laravel/PHP stacks, consider `symfony/diff` or `php-diff` as alternatives.
- How do I customize the diff output format beyond UnifiedDiff or StrictUnifiedDiff?
- Implement the `DiffOutputBuilderInterface` to create custom formats like JSON or minimalist CLI output. The package provides extensibility hooks for non-standard use cases, such as API responses or script-friendly diffs.
- Is sebastian/diff suitable for real-time diffing in live collaboration tools?
- No. While it’s efficient for most use cases (e.g., test failures, CI logs), it’s not optimized for high-frequency operations like live collaboration. For real-time needs, consider `diff-match-patch` or client-side libraries.
- How can I integrate sebastian/diff with Laravel Debugbar or Telescope?
- Use the `Differ` class to generate diffs for log entries or query results, then pass them to Debugbar’s `addMessage()` or Telescope’s `record()` methods. This visualizes changes in a user-friendly format.
- Does sebastian/diff support parsing Git diffs into structured objects?
- Yes. The `Parser` class converts unified diff text (e.g., from Git) into an object graph. This is useful for analyzing changes in CI/CD pipelines or CMS tools like Laravel Nova.
- How do I handle malformed diffs or edge cases (e.g., binary files) in production?
- Gracefully fall back to string diffs or implement custom error handling. The `Parser` may throw exceptions for invalid input; wrap it in a try-catch block or validate diffs before parsing.
- Can I use sebastian/diff to compare database records or Eloquent models in Laravel?
- Indirectly, yes. Serialize model attributes to strings (e.g., using `json_encode()`) and pass them to `Differ`. For complex comparisons, consider extending the `Differ` class or using a helper trait in your test suite.
- What are the performance implications of using sebastian/diff in CI/CD pipelines?
- The library is lightweight (~1MB) with minimal overhead. Benchmark in your pipeline to ensure it doesn’t slow down deployments. For large files, consider limiting diff context or pre-filtering changes.