Parser.php 2.9 KB

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