1# sebastian/diff
2
3Diff implementation for PHP, factored out of PHPUnit into a stand-alone component.
4
5## Installation
6
7You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
8
9    composer require sebastian/diff
10
11If 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:
12
13    composer require --dev sebastian/diff
14
15### Usage
16
17The `Differ` class can be used to generate a textual representation of the difference between two strings:
18
19```php
20use SebastianBergmann\Diff\Differ;
21
22$differ = new Differ;
23print $differ->diff('foo', 'bar');
24```
25
26The code above yields the output below:
27
28    --- Original
29    +++ New
30    @@ @@
31    -foo
32    +bar
33
34The `Parser` class can be used to parse a unified diff into an object graph:
35
36```php
37use SebastianBergmann\Diff\Parser;
38use SebastianBergmann\Git;
39
40$git = new Git('/usr/local/src/money');
41
42$diff = $git->getDiff(
43  '948a1a07768d8edd10dcefa8315c1cbeffb31833',
44  'c07a373d2399f3e686234c4f7f088d635eb9641b'
45);
46
47$parser = new Parser;
48
49print_r($parser->parse($diff));
50```
51
52The code above yields the output below:
53
54    Array
55    (
56        [0] => SebastianBergmann\Diff\Diff Object
57            (
58                [from:SebastianBergmann\Diff\Diff:private] => a/tests/MoneyTest.php
59                [to:SebastianBergmann\Diff\Diff:private] => b/tests/MoneyTest.php
60                [chunks:SebastianBergmann\Diff\Diff:private] => Array
61                    (
62                        [0] => SebastianBergmann\Diff\Chunk Object
63                            (
64                                [start:SebastianBergmann\Diff\Chunk:private] => 87
65                                [startRange:SebastianBergmann\Diff\Chunk:private] => 7
66                                [end:SebastianBergmann\Diff\Chunk:private] => 87
67                                [endRange:SebastianBergmann\Diff\Chunk:private] => 7
68                                [lines:SebastianBergmann\Diff\Chunk:private] => Array
69                                    (
70                                        [0] => SebastianBergmann\Diff\Line Object
71                                            (
72                                                [type:SebastianBergmann\Diff\Line:private] => 3
73                                                [content:SebastianBergmann\Diff\Line:private] =>      * @covers SebastianBergmann\Money\Money::add
74                                            )
75
76                                        [1] => SebastianBergmann\Diff\Line Object
77                                            (
78                                                [type:SebastianBergmann\Diff\Line:private] => 3
79                                                [content:SebastianBergmann\Diff\Line:private] =>      * @covers SebastianBergmann\Money\Money::newMoney
80                                            )
81
82                                        [2] => SebastianBergmann\Diff\Line Object
83                                            (
84                                                [type:SebastianBergmann\Diff\Line:private] => 3
85                                                [content:SebastianBergmann\Diff\Line:private] =>      */
86                                            )
87
88                                        [3] => SebastianBergmann\Diff\Line Object
89                                            (
90                                                [type:SebastianBergmann\Diff\Line:private] => 2
91                                                [content:SebastianBergmann\Diff\Line:private] =>     public function testAnotherMoneyWithSameCurrencyObjectCanBeAdded()
92                                            )
93
94                                        [4] => SebastianBergmann\Diff\Line Object
95                                            (
96                                                [type:SebastianBergmann\Diff\Line:private] => 1
97                                                [content:SebastianBergmann\Diff\Line:private] =>     public function testAnotherMoneyObjectWithSameCurrencyCanBeAdded()
98                                            )
99
100                                        [5] => SebastianBergmann\Diff\Line Object
101                                            (
102                                                [type:SebastianBergmann\Diff\Line:private] => 3
103                                                [content:SebastianBergmann\Diff\Line:private] =>     {
104                                            )
105
106                                        [6] => SebastianBergmann\Diff\Line Object
107                                            (
108                                                [type:SebastianBergmann\Diff\Line:private] => 3
109                                                [content:SebastianBergmann\Diff\Line:private] =>         $a = new Money(1, new Currency('EUR'));
110                                            )
111
112                                        [7] => SebastianBergmann\Diff\Line Object
113                                            (
114                                                [type:SebastianBergmann\Diff\Line:private] => 3
115                                                [content:SebastianBergmann\Diff\Line:private] =>         $b = new Money(2, new Currency('EUR'));
116                                            )
117
118                                    )
119
120                            )
121
122                    )
123
124            )
125
126    )
127