comparison vendor/sebastian/diff/src/Parser.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 7a779792577d
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php declare(strict_types=1);
2 /* 2 /*
3 * This file is part of sebastian/diff. 3 * This file is part of sebastian/diff.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
11 namespace SebastianBergmann\Diff; 11 namespace SebastianBergmann\Diff;
12 12
13 /** 13 /**
14 * Unified diff parser. 14 * Unified diff parser.
15 */ 15 */
16 class Parser 16 final class Parser
17 { 17 {
18 /** 18 /**
19 * @param string $string 19 * @param string $string
20 * 20 *
21 * @return Diff[] 21 * @return Diff[]
22 */ 22 */
23 public function parse($string) 23 public function parse(string $string): array
24 { 24 {
25 $lines = \preg_split('(\r\n|\r|\n)', $string); 25 $lines = \preg_split('(\r\n|\r|\n)', $string);
26 26
27 if (!empty($lines) && $lines[\count($lines) - 1] == '') { 27 if (!empty($lines) && $lines[\count($lines) - 1] === '') {
28 \array_pop($lines); 28 \array_pop($lines);
29 } 29 }
30 30
31 $lineCount = \count($lines); 31 $lineCount = \count($lines);
32 $diffs = array(); 32 $diffs = [];
33 $diff = null; 33 $diff = null;
34 $collected = array(); 34 $collected = [];
35 35
36 for ($i = 0; $i < $lineCount; ++$i) { 36 for ($i = 0; $i < $lineCount; ++$i) {
37 if (\preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) && 37 if (\preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) &&
38 \preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) { 38 \preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) {
39 if ($diff !== null) { 39 if ($diff !== null) {
40 $this->parseFileDiff($diff, $collected); 40 $this->parseFileDiff($diff, $collected);
41 41
42 $diffs[] = $diff; 42 $diffs[] = $diff;
43 $collected = array(); 43 $collected = [];
44 } 44 }
45 45
46 $diff = new Diff($fromMatch['file'], $toMatch['file']); 46 $diff = new Diff($fromMatch['file'], $toMatch['file']);
47 47
48 ++$i; 48 ++$i;
62 } 62 }
63 63
64 return $diffs; 64 return $diffs;
65 } 65 }
66 66
67 /**
68 * @param Diff $diff
69 * @param array $lines
70 */
71 private function parseFileDiff(Diff $diff, array $lines) 67 private function parseFileDiff(Diff $diff, array $lines)
72 { 68 {
73 $chunks = array(); 69 $chunks = [];
74 $chunk = null; 70 $chunk = null;
75 71
76 foreach ($lines as $line) { 72 foreach ($lines as $line) {
77 if (\preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) { 73 if (\preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
78 $chunk = new Chunk( 74 $chunk = new Chunk(
79 $match['start'], 75 (int) $match['start'],
80 isset($match['startrange']) ? \max(1, $match['startrange']) : 1, 76 isset($match['startrange']) ? \max(1, (int) $match['startrange']) : 1,
81 $match['end'], 77 (int) $match['end'],
82 isset($match['endrange']) ? \max(1, $match['endrange']) : 1 78 isset($match['endrange']) ? \max(1, (int) $match['endrange']) : 1
83 ); 79 );
84 80
85 $chunks[] = $chunk; 81 $chunks[] = $chunk;
86 $diffLines = array(); 82 $diffLines = [];
87 83
88 continue; 84 continue;
89 } 85 }
90 86
91 if (\preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) { 87 if (\preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {