You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
7.5 KiB

3 years ago
  1. # sebastian/diff
  2. [![CI Status](https://github.com/sebastianbergmann/diff/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/diff/actions)
  3. [![Type Coverage](https://shepherd.dev/github/sebastianbergmann/diff/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/diff)
  4. Diff implementation for PHP, factored out of PHPUnit into a stand-alone component.
  5. ## Installation
  6. You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
  7. ```
  8. composer require sebastian/diff
  9. ```
  10. If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
  11. ```
  12. composer require --dev sebastian/diff
  13. ```
  14. ### Usage
  15. #### Generating diff
  16. The `Differ` class can be used to generate a textual representation of the difference between two strings:
  17. ```php
  18. <?php
  19. use SebastianBergmann\Diff\Differ;
  20. $differ = new Differ;
  21. print $differ->diff('foo', 'bar');
  22. ```
  23. The code above yields the output below:
  24. ```diff
  25. --- Original
  26. +++ New
  27. @@ @@
  28. -foo
  29. +bar
  30. ```
  31. There are three output builders available in this package:
  32. #### UnifiedDiffOutputBuilder
  33. This is default builder, which generates the output close to udiff and is used by PHPUnit.
  34. ```php
  35. <?php
  36. use SebastianBergmann\Diff\Differ;
  37. use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
  38. $builder = new UnifiedDiffOutputBuilder(
  39. "--- Original\n+++ New\n", // custom header
  40. false // do not add line numbers to the diff
  41. );
  42. $differ = new Differ($builder);
  43. print $differ->diff('foo', 'bar');
  44. ```
  45. #### StrictUnifiedDiffOutputBuilder
  46. Generates (strict) Unified diff's (unidiffs) with hunks,
  47. similar to `diff -u` and compatible with `patch` and `git apply`.
  48. ```php
  49. <?php
  50. use SebastianBergmann\Diff\Differ;
  51. use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
  52. $builder = new StrictUnifiedDiffOutputBuilder([
  53. 'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
  54. 'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
  55. 'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
  56. 'fromFile' => null,
  57. 'fromFileDate' => null,
  58. 'toFile' => null,
  59. 'toFileDate' => null,
  60. ]);
  61. $differ = new Differ($builder);
  62. print $differ->diff('foo', 'bar');
  63. ```
  64. #### DiffOnlyOutputBuilder
  65. Output only the lines that differ.
  66. ```php
  67. <?php
  68. use SebastianBergmann\Diff\Differ;
  69. use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder;
  70. $builder = new DiffOnlyOutputBuilder(
  71. "--- Original\n+++ New\n"
  72. );
  73. $differ = new Differ($builder);
  74. print $differ->diff('foo', 'bar');
  75. ```
  76. #### DiffOutputBuilderInterface
  77. You can pass any output builder to the `Differ` class as longs as it implements the `DiffOutputBuilderInterface`.
  78. #### Parsing diff
  79. The `Parser` class can be used to parse a unified diff into an object graph:
  80. ```php
  81. use SebastianBergmann\Diff\Parser;
  82. use SebastianBergmann\Git;
  83. $git = new Git('/usr/local/src/money');
  84. $diff = $git->getDiff(
  85. '948a1a07768d8edd10dcefa8315c1cbeffb31833',
  86. 'c07a373d2399f3e686234c4f7f088d635eb9641b'
  87. );
  88. $parser = new Parser;
  89. print_r($parser->parse($diff));
  90. ```
  91. The code above yields the output below:
  92. Array
  93. (
  94. [0] => SebastianBergmann\Diff\Diff Object
  95. (
  96. [from:SebastianBergmann\Diff\Diff:private] => a/tests/MoneyTest.php
  97. [to:SebastianBergmann\Diff\Diff:private] => b/tests/MoneyTest.php
  98. [chunks:SebastianBergmann\Diff\Diff:private] => Array
  99. (
  100. [0] => SebastianBergmann\Diff\Chunk Object
  101. (
  102. [start:SebastianBergmann\Diff\Chunk:private] => 87
  103. [startRange:SebastianBergmann\Diff\Chunk:private] => 7
  104. [end:SebastianBergmann\Diff\Chunk:private] => 87
  105. [endRange:SebastianBergmann\Diff\Chunk:private] => 7
  106. [lines:SebastianBergmann\Diff\Chunk:private] => Array
  107. (
  108. [0] => SebastianBergmann\Diff\Line Object
  109. (
  110. [type:SebastianBergmann\Diff\Line:private] => 3
  111. [content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::add
  112. )
  113. [1] => SebastianBergmann\Diff\Line Object
  114. (
  115. [type:SebastianBergmann\Diff\Line:private] => 3
  116. [content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::newMoney
  117. )
  118. [2] => SebastianBergmann\Diff\Line Object
  119. (
  120. [type:SebastianBergmann\Diff\Line:private] => 3
  121. [content:SebastianBergmann\Diff\Line:private] => */
  122. )
  123. [3] => SebastianBergmann\Diff\Line Object
  124. (
  125. [type:SebastianBergmann\Diff\Line:private] => 2
  126. [content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyWithSameCurrencyObjectCanBeAdded()
  127. )
  128. [4] => SebastianBergmann\Diff\Line Object
  129. (
  130. [type:SebastianBergmann\Diff\Line:private] => 1
  131. [content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyObjectWithSameCurrencyCanBeAdded()
  132. )
  133. [5] => SebastianBergmann\Diff\Line Object
  134. (
  135. [type:SebastianBergmann\Diff\Line:private] => 3
  136. [content:SebastianBergmann\Diff\Line:private] => {
  137. )
  138. [6] => SebastianBergmann\Diff\Line Object
  139. (
  140. [type:SebastianBergmann\Diff\Line:private] => 3
  141. [content:SebastianBergmann\Diff\Line:private] => $a = new Money(1, new Currency('EUR'));
  142. )
  143. [7] => SebastianBergmann\Diff\Line Object
  144. (
  145. [type:SebastianBergmann\Diff\Line:private] => 3
  146. [content:SebastianBergmann\Diff\Line:private] => $b = new Money(2, new Currency('EUR'));
  147. )
  148. )
  149. )
  150. )
  151. )
  152. )