Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UnifiedDiffOutputBuilder.php

function UnifiedDiffOutputBuilder::getDiff

Overrides DiffOutputBuilderInterface::getDiff

File

vendor/sebastian/diff/src/Output/UnifiedDiffOutputBuilder.php, line 45

Class

UnifiedDiffOutputBuilder
Builds a diff string representation in unified diff format in chunks.

Namespace

SebastianBergmann\Diff\Output

Code

public function getDiff(array $diff) : string {
    $buffer = fopen('php://memory', 'r+b');
    if ('' !== $this->header) {
        fwrite($buffer, $this->header);
        if (!str_ends_with($this->header, "\n")) {
            fwrite($buffer, "\n");
        }
    }
    if (0 !== count($diff)) {
        $this->writeDiffHunks($buffer, $diff);
    }
    $diff = stream_get_contents($buffer, -1, 0);
    fclose($buffer);
    // If the diff is non-empty and last char is not a linebreak: add it.
    // This might happen when both the `from` and `to` do not have a trailing linebreak
    $last = substr($diff, -1);
    return '' !== $diff && "\n" !== $last && "\r" !== $last ? $diff . "\n" : $diff;
}
RSS feed
Powered by Drupal