@@ -30,54 +30,70 @@ protected function renderWorker(Differ $differ): string
30
30
$ ret = '' ;
31
31
32
32
foreach ($ differ ->getGroupedOpcodes () as $ hunk ) {
33
- $ lastBlockIdx = \count ($ hunk ) - 1 ;
34
-
35
- $ i1 = $ hunk [0 ][1 ];
36
- $ i2 = $ hunk [$ lastBlockIdx ][2 ];
37
- $ j1 = $ hunk [0 ][3 ];
38
- $ j2 = $ hunk [$ lastBlockIdx ][4 ];
39
-
40
- if ($ i1 === 0 && $ i2 === 0 ) {
41
- $ i1 = $ i2 = -1 ; // trick
42
- }
43
-
44
- $ ret .= $ this ->renderHunkHeader ($ i1 + 1 , $ i2 - $ i1 , $ j1 + 1 , $ j2 - $ j1 );
33
+ $ ret .= $ this ->renderHunkHeader ($ differ , $ hunk );
34
+ $ ret .= $ this ->renderHunkBlocks ($ differ , $ hunk );
35
+ }
45
36
46
- foreach ($ hunk as [$ op , $ i1 , $ i2 , $ j1 , $ j2 ]) {
47
- // note that although we are in a OP_EQ situation,
48
- // the old and the new may not be exactly the same
49
- // because of ignoreCase, ignoreWhitespace, etc
50
- if ($ op === SequenceMatcher::OP_EQ ) {
51
- // we could only pick either the old or the new to show
52
- $ ret .= $ this ->renderContext (' ' , $ differ ->getNew ($ j1 , $ j2 ));
37
+ return $ ret ;
38
+ }
53
39
54
- continue ;
55
- }
40
+ /**
41
+ * Render the hunk header.
42
+ *
43
+ * @param Differ $differ the differ
44
+ * @param int[][] $hunk the hunk
45
+ */
46
+ protected function renderHunkHeader (Differ $ differ , array $ hunk ): string
47
+ {
48
+ $ lastBlockIdx = \count ($ hunk ) - 1 ;
56
49
57
- if ($ op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL )) {
58
- $ ret .= $ this ->renderContext ('- ' , $ differ ->getOld ($ i1 , $ i2 ));
59
- }
50
+ $ i1 = $ hunk [0 ][1 ];
51
+ $ i2 = $ hunk [$ lastBlockIdx ][2 ];
52
+ $ j1 = $ hunk [0 ][3 ];
53
+ $ j2 = $ hunk [$ lastBlockIdx ][4 ];
60
54
61
- if ($ op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS )) {
62
- $ ret .= $ this ->renderContext ('+ ' , $ differ ->getNew ($ j1 , $ j2 ));
63
- }
64
- }
55
+ if ($ i1 === 0 && $ i2 === 0 ) {
56
+ $ i1 = $ i2 = -1 ; // trick
65
57
}
66
58
67
- return $ ret ;
59
+ return
60
+ '@@ ' .
61
+ '- ' . ($ i1 + 1 ) . ', ' . ($ i2 - $ i1 ) . ' ' .
62
+ '+ ' . ($ j1 + 1 ) . ', ' . ($ j2 - $ j1 ) . ' ' .
63
+ "@@ \n" ;
68
64
}
69
65
70
66
/**
71
- * Render the hunk header .
67
+ * Render the hunk content .
72
68
*
73
- * @param int $a1 the a1
74
- * @param int $a2 the a2
75
- * @param int $b1 the b1
76
- * @param int $b2 the b2
69
+ * @param Differ $differ the differ
70
+ * @param int[][] $hunk the hunk
77
71
*/
78
- protected function renderHunkHeader ( int $ a1 , int $ a2 , int $ b1 , int $ b2 ): string
72
+ protected function renderHunkBlocks ( Differ $ differ , array $ hunk ): string
79
73
{
80
- return "@@ - {$ a1 }, {$ a2 } + {$ b1 }, {$ b2 } @@ \n" ;
74
+ $ html = '' ;
75
+
76
+ foreach ($ hunk as [$ op , $ i1 , $ i2 , $ j1 , $ j2 ]) {
77
+ // note that although we are in a OP_EQ situation,
78
+ // the old and the new may not be exactly the same
79
+ // because of ignoreCase, ignoreWhitespace, etc
80
+ if ($ op === SequenceMatcher::OP_EQ ) {
81
+ // we could only pick either the old or the new to show
82
+ $ html .= $ this ->renderContext (' ' , $ differ ->getNew ($ j1 , $ j2 ));
83
+
84
+ continue ;
85
+ }
86
+
87
+ if ($ op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL )) {
88
+ $ html .= $ this ->renderContext ('- ' , $ differ ->getOld ($ i1 , $ i2 ));
89
+ }
90
+
91
+ if ($ op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS )) {
92
+ $ html .= $ this ->renderContext ('+ ' , $ differ ->getNew ($ j1 , $ j2 ));
93
+ }
94
+ }
95
+
96
+ return $ html ;
81
97
}
82
98
83
99
/**
0 commit comments