@@ -32,6 +32,14 @@ final class Context extends AbstractText
32
32
SequenceMatcher::OP_REP => '! ' ,
33
33
];
34
34
35
+ /**
36
+ * @var int the union of OPs that indicate there is a change
37
+ */
38
+ const OP_BLOCK_CHANGED =
39
+ SequenceMatcher::OP_INS |
40
+ SequenceMatcher::OP_DEL |
41
+ SequenceMatcher::OP_REP ;
42
+
35
43
/**
36
44
* {@inheritdoc}
37
45
*/
@@ -42,6 +50,7 @@ protected function renderWorker(Differ $differ): string
42
50
foreach ($ differ ->getGroupedOpcodes () as $ hunk ) {
43
51
$ lastBlockIdx = \count ($ hunk ) - 1 ;
44
52
53
+ // note that these line number variables are 0-based
45
54
$ i1 = $ hunk [0 ][1 ];
46
55
$ i2 = $ hunk [$ lastBlockIdx ][2 ];
47
56
$ j1 = $ hunk [0 ][3 ];
@@ -82,16 +91,21 @@ protected function renderHunkHeader(string $delimiter, int $a1, int $a2): string
82
91
protected function renderHunkOld (Differ $ differ , array $ hunk ): string
83
92
{
84
93
$ ret = '' ;
94
+ $ hasChangeInHunk = false ;
85
95
86
96
foreach ($ hunk as [$ op , $ i1 , $ i2 , $ j1 , $ j2 ]) {
87
97
if ($ op === SequenceMatcher::OP_INS ) {
88
98
continue ;
89
99
}
90
100
101
+ if ($ op & self ::OP_BLOCK_CHANGED ) {
102
+ $ hasChangeInHunk = true ;
103
+ }
104
+
91
105
$ ret .= $ this ->renderContext (self ::TAG_MAP [$ op ], $ differ ->getOld ($ i1 , $ i2 ));
92
106
}
93
107
94
- return $ ret ;
108
+ return $ hasChangeInHunk ? $ ret : '' ;
95
109
}
96
110
97
111
/**
@@ -103,16 +117,21 @@ protected function renderHunkOld(Differ $differ, array $hunk): string
103
117
protected function renderHunkNew (Differ $ differ , array $ hunk ): string
104
118
{
105
119
$ ret = '' ;
120
+ $ hasChangeInHunk = false ;
106
121
107
122
foreach ($ hunk as [$ op , $ i1 , $ i2 , $ j1 , $ j2 ]) {
108
123
if ($ op === SequenceMatcher::OP_DEL ) {
109
124
continue ;
110
125
}
111
126
127
+ if ($ op & self ::OP_BLOCK_CHANGED ) {
128
+ $ hasChangeInHunk = true ;
129
+ }
130
+
112
131
$ ret .= $ this ->renderContext (self ::TAG_MAP [$ op ], $ differ ->getNew ($ j1 , $ j2 ));
113
132
}
114
133
115
- return $ ret ;
134
+ return $ hasChangeInHunk ? $ ret : '' ;
116
135
}
117
136
118
137
/**
0 commit comments