comparison vendor/sebastian/diff/src/Output/DiffOnlyOutputBuilder.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php declare(strict_types=1);
2 /*
3 * This file is part of sebastian/diff.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10 namespace SebastianBergmann\Diff\Output;
11
12 /**
13 * Builds a diff string representation in a loose unified diff format
14 * listing only changes lines. Does not include line numbers.
15 */
16 final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
17 {
18 /**
19 * @var string
20 */
21 private $header;
22
23 public function __construct(string $header = "--- Original\n+++ New\n")
24 {
25 $this->header = $header;
26 }
27
28 public function getDiff(array $diff): string
29 {
30 $buffer = \fopen('php://memory', 'r+b');
31
32 if ('' !== $this->header) {
33 \fwrite($buffer, $this->header);
34 if ("\n" !== \substr($this->header, -1, 1)) {
35 \fwrite($buffer, "\n");
36 }
37 }
38
39 foreach ($diff as $diffEntry) {
40 if ($diffEntry[1] === 1 /* ADDED */) {
41 \fwrite($buffer, '+' . $diffEntry[0]);
42 } elseif ($diffEntry[1] === 2 /* REMOVED */) {
43 \fwrite($buffer, '-' . $diffEntry[0]);
44 } elseif ($diffEntry[1] === 3 /* WARNING */) {
45 \fwrite($buffer, ' ' . $diffEntry[0]);
46
47 continue; // Warnings should not be tested for line break, it will always be there
48 } else { /* Not changed (old) 0 */
49 continue; // we didn't write the non changs line, so do not add a line break either
50 }
51
52 $lc = \substr($diffEntry[0], -1);
53 if ($lc !== "\n" && $lc !== "\r") {
54 \fwrite($buffer, "\n"); // \No newline at end of file
55 }
56 }
57
58 $diff = \stream_get_contents($buffer, -1, 0);
59 \fclose($buffer);
60
61 return $diff;
62 }
63 }