Parser.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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;
  11. use function array_pop;
  12. use function assert;
  13. use function count;
  14. use function max;
  15. use function preg_match;
  16. use function preg_split;
  17. /**
  18. * Unified diff parser.
  19. */
  20. final class Parser
  21. {
  22. /**
  23. * @return Diff[]
  24. */
  25. public function parse(string $string): array
  26. {
  27. $lines = preg_split('(\r\n|\r|\n)', $string);
  28. if (!empty($lines) && $lines[count($lines) - 1] === '') {
  29. array_pop($lines);
  30. }
  31. $lineCount = count($lines);
  32. $diffs = [];
  33. $diff = null;
  34. $collected = [];
  35. for ($i = 0; $i < $lineCount; $i++) {
  36. if (preg_match('#^---\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i], $fromMatch) &&
  37. preg_match('#^\\+\\+\\+\\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i + 1], $toMatch)) {
  38. if ($diff !== null) {
  39. $this->parseFileDiff($diff, $collected);
  40. $diffs[] = $diff;
  41. $collected = [];
  42. }
  43. assert(!empty($fromMatch['file']));
  44. assert(!empty($toMatch['file']));
  45. $diff = new Diff($fromMatch['file'], $toMatch['file']);
  46. $i++;
  47. } else {
  48. if (preg_match('/^(?:diff --git |index [\da-f.]+|[+-]{3} [ab])/', $lines[$i])) {
  49. continue;
  50. }
  51. $collected[] = $lines[$i];
  52. }
  53. }
  54. if ($diff !== null && count($collected)) {
  55. $this->parseFileDiff($diff, $collected);
  56. $diffs[] = $diff;
  57. }
  58. return $diffs;
  59. }
  60. private function parseFileDiff(Diff $diff, array $lines): void
  61. {
  62. $chunks = [];
  63. $chunk = null;
  64. $diffLines = [];
  65. foreach ($lines as $line) {
  66. if (preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match, PREG_UNMATCHED_AS_NULL)) {
  67. $chunk = new Chunk(
  68. (int) $match['start'],
  69. isset($match['startrange']) ? max(0, (int) $match['startrange']) : 1,
  70. (int) $match['end'],
  71. isset($match['endrange']) ? max(0, (int) $match['endrange']) : 1,
  72. );
  73. $chunks[] = $chunk;
  74. $diffLines = [];
  75. continue;
  76. }
  77. if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
  78. $type = Line::UNCHANGED;
  79. if ($match['type'] === '+') {
  80. $type = Line::ADDED;
  81. } elseif ($match['type'] === '-') {
  82. $type = Line::REMOVED;
  83. }
  84. $diffLines[] = new Line($type, $match['line']);
  85. $chunk?->setLines($diffLines);
  86. }
  87. }
  88. $diff->setChunks($chunks);
  89. }
  90. }