comparison vendor/sebastian/diff/src/Parser.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the Diff package. 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 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
20 * 20 *
21 * @return Diff[] 21 * @return Diff[]
22 */ 22 */
23 public function parse($string) 23 public function parse($string)
24 { 24 {
25 $lines = preg_split('(\r\n|\r|\n)', $string); 25 $lines = \preg_split('(\r\n|\r|\n)', $string);
26 $lineCount = count($lines); 26
27 if (!empty($lines) && $lines[\count($lines) - 1] == '') {
28 \array_pop($lines);
29 }
30
31 $lineCount = \count($lines);
27 $diffs = array(); 32 $diffs = array();
28 $diff = null; 33 $diff = null;
29 $collected = array(); 34 $collected = array();
30 35
31 for ($i = 0; $i < $lineCount; ++$i) { 36 for ($i = 0; $i < $lineCount; ++$i) {
32 if (preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) && 37 if (\preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) &&
33 preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) { 38 \preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) {
34 if ($diff !== null) { 39 if ($diff !== null) {
35 $this->parseFileDiff($diff, $collected); 40 $this->parseFileDiff($diff, $collected);
41
36 $diffs[] = $diff; 42 $diffs[] = $diff;
37 $collected = array(); 43 $collected = array();
38 } 44 }
39 45
40 $diff = new Diff($fromMatch['file'], $toMatch['file']); 46 $diff = new Diff($fromMatch['file'], $toMatch['file']);
47
41 ++$i; 48 ++$i;
42 } else { 49 } else {
43 if (preg_match('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/', $lines[$i])) { 50 if (\preg_match('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/', $lines[$i])) {
44 continue; 51 continue;
45 } 52 }
53
46 $collected[] = $lines[$i]; 54 $collected[] = $lines[$i];
47 } 55 }
48 } 56 }
49 57
50 if (count($collected) && ($diff !== null)) { 58 if ($diff !== null && \count($collected)) {
51 $this->parseFileDiff($diff, $collected); 59 $this->parseFileDiff($diff, $collected);
60
52 $diffs[] = $diff; 61 $diffs[] = $diff;
53 } 62 }
54 63
55 return $diffs; 64 return $diffs;
56 } 65 }
60 * @param array $lines 69 * @param array $lines
61 */ 70 */
62 private function parseFileDiff(Diff $diff, array $lines) 71 private function parseFileDiff(Diff $diff, array $lines)
63 { 72 {
64 $chunks = array(); 73 $chunks = array();
74 $chunk = null;
65 75
66 foreach ($lines as $line) { 76 foreach ($lines as $line) {
67 if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) { 77 if (\preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
68 $chunk = new Chunk( 78 $chunk = new Chunk(
69 $match['start'], 79 $match['start'],
70 isset($match['startrange']) ? max(1, $match['startrange']) : 1, 80 isset($match['startrange']) ? \max(1, $match['startrange']) : 1,
71 $match['end'], 81 $match['end'],
72 isset($match['endrange']) ? max(1, $match['endrange']) : 1 82 isset($match['endrange']) ? \max(1, $match['endrange']) : 1
73 ); 83 );
74 84
75 $chunks[] = $chunk; 85 $chunks[] = $chunk;
76 $diffLines = array(); 86 $diffLines = array();
87
77 continue; 88 continue;
78 } 89 }
79 90
80 if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) { 91 if (\preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
81 $type = Line::UNCHANGED; 92 $type = Line::UNCHANGED;
82 93
83 if ($match['type'] == '+') { 94 if ($match['type'] === '+') {
84 $type = Line::ADDED; 95 $type = Line::ADDED;
85 } elseif ($match['type'] == '-') { 96 } elseif ($match['type'] === '-') {
86 $type = Line::REMOVED; 97 $type = Line::REMOVED;
87 } 98 }
88 99
89 $diffLines[] = new Line($type, $match['line']); 100 $diffLines[] = new Line($type, $match['line']);
90 101
91 if (isset($chunk)) { 102 if (null !== $chunk) {
92 $chunk->setLines($diffLines); 103 $chunk->setLines($diffLines);
93 } 104 }
94 } 105 }
95 } 106 }
96 107