php-diff

repository·v7·Indexed 19 days ago

https://github.com/jfcherng/php-diff

A comprehensive PHP library for generating differences between two strings or files. It supports multiple output formats, including plain text (Unified, Context, JSON) and rich HTML (Side-by-Side, Inline, Combined). The library provides granular control over diff detail levels (line, word, char) and includes dedicated configuration objects DifferOptions and RendererOptions for managing comparison behavior and visual output.

Tokens
7.8K
Snippets
27
Records
51
Agent score
66%

What's inside php-diff

  1. Note on class inheritance in v4

    v7
    Starting with version 4, non-abstract classes in the library are marked with the final keyword. This means you can no longer extend these classes via inheritance. If your implementation relies on extending library classes, you should consider using composition instead to ensure compatibility with future updates.
  2. Configure detail levels for HTML diffing

    v7

    When using HTML-based renderers, you can control the granularity of the diff using the detailLevel option. Supported levels include:

    • line (Default): Shows differences at the line level.
    • word: Shows differences at the word level.
    • char: Shows differences at the character level.
    • none: Shows no detailed diff within lines.
  3. Install php-diff via Composer

    v7

    Install the jfcherng/php-diff package using Composer to generate diffs between two strings in various formats (Text and HTML).

    composer require jfcherng/php-diff
  4. Migrate SequenceMatcher to the new package

    v7

    When upgrading to v4, the Jfcherng\Diff\Utility\SequenceMatcher class has been moved to a standalone package. To continue using it, you must install the new package and update your namespace references.

    Migration Steps:

    1. Install the new package: jfcherng/php-sequence-matcher.
    2. Update your code to use the new namespace: Jfcherng\Diff\SequenceMatcher (instead of Jfcherng\Diff\Utility\SequenceMatcher).
    # Install the new package via composer
    composer require jfcherng/php-sequence-matcher
    
    # Update your use statements
    # FROM:
    use Jfcherng\Diff\Utility\SequenceMatcher;
    # TO:
    use Jfcherng\Diff\SequenceMatcher;
  5. Migrate configuration options from v2 to v3

    v7

    When upgrading to version 3, the location and availability of certain configuration options have changed:

    • Removed from $diffOptions: charLevelDiff and separateBlock are no longer valid keys in the $diffOptions array.
    • Added to $templateOptions:
      • detailLevel: This replaces the functionality of the old charLevelDiff. Refer to the documentation for specific valid values.
      • separateBlock: This is the new location for the separateBlock option, which was previously in $diffOptions.
  6. Update Factory namespaces in v4

    v7

    In version 4, all factory classes previously located under the Jfcherng\Diff\Utility\ namespace have been moved to the Jfcherng\Diff\Factory\ namespace.

    Example Mapping:

    • Jfcherng\Diff\Utility\RendererFactory $\rightarrow$ Jfcherng\Diff\Factory\RendererFactory
    // Example of namespace change
    // FROM:
    use Jfcherng\Diff\Utility\RendererFactory;
    // TO:
    use Jfcherng\Diff\Factory\RendererFactory;
  7. Migrate Diff method names from AB/FromTo/BaseChanged to OldNew

    v7

    In version 5, method names involving a, b, from, to, or base, changed were renamed to old, new to improve consistency. If you are upgrading from a version prior to v5, update your calls to the following methods:

    • Diff::setAB() $\rightarrow$ Diff::setOldNew()
    • Diff::setA() $\rightarrow$ Diff::setOld()
    • Diff::setB() $\rightarrow$ Diff::setNew()
    • Diff::getA() $\rightarrow$ Diff::getOld()
    • Diff::getB() $\rightarrow$ Diff::getNew()
    // Old v4 style
    $diff->setAB($oldText, $newText);
    $old = $diff->getA();
    
    // New v5 style
    $diff->setOldNew($oldText, $newText);
    $old = $diff->getOld();
  8. Update Renderer usage pattern in v6+

    v7

    In version 6 and later, the responsibility for rendering has shifted. The Differ class no longer contains a render() method. Instead, you must use a Renderer instance to perform the rendering by passing the Differ object into the renderer's render() method.

    use Jfcherng//
    use Jfcherng//
    
    $differ = new Differ(explode("\n", $old), explode("\n", $new), $diffOptions);
    $renderer = RendererFactory::make($rendererName, $rendererOptions);
    $result = $renderer->render($differ); // <-- this line has been changed
  9. Run the php-diff demo in a Web Environment

    v7

    To view the web-based demonstration of the library, you must first install the project dependencies from the root directory using composer install. Once dependencies are installed, you can start the built-in PHP development server and access the demo via your browser.

    1. From the project root, run: composer run-script server.
    2. Open your browser and navigate to http://localhost:12388/demo_web.php.
    # From the project root
    composer install
    composer run-script server