Skip to content

Commit 899600c

Browse files
committed
Tidy codes
Signed-off-by: Jack Cherng <[email protected]>
1 parent a81078f commit 899600c

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

src/Renderer/Text/Context.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ protected function renderWorker(Differ $differ): string
5050
$ret .=
5151
"***************\n" .
5252
$this->renderHunkHeader('*', $i1, $i2) .
53-
$this->renderHunkOld($hunk, $differ) .
53+
$this->renderHunkOld($differ, $hunk) .
5454
$this->renderHunkHeader('-', $j1, $j2) .
55-
$this->renderHunkNew($hunk, $differ);
55+
$this->renderHunkNew($differ, $hunk);
5656
}
5757

5858
return $ret;
@@ -76,10 +76,10 @@ protected function renderHunkHeader(string $delimiter, int $a1, int $a2): string
7676
/**
7777
* Render the old hunk.
7878
*
79-
* @param int[][] $hunk the hunk
8079
* @param Differ $differ the differ object
80+
* @param int[][] $hunk the hunk
8181
*/
82-
protected function renderHunkOld(array $hunk, Differ $differ): string
82+
protected function renderHunkOld(Differ $differ, array $hunk): string
8383
{
8484
$ret = '';
8585

@@ -97,10 +97,10 @@ protected function renderHunkOld(array $hunk, Differ $differ): string
9797
/**
9898
* Render the new hunk.
9999
*
100-
* @param int[][] $hunk the hunk
101100
* @param Differ $differ the differ object
101+
* @param int[][] $hunk the hunk
102102
*/
103-
protected function renderHunkNew(array $hunk, Differ $differ): string
103+
protected function renderHunkNew(Differ $differ, array $hunk): string
104104
{
105105
$ret = '';
106106

src/Renderer/Text/Unified.php

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,70 @@ protected function renderWorker(Differ $differ): string
3030
$ret = '';
3131

3232
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+
}
4536

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+
}
5339

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;
5649

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];
6054

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
6557
}
6658

67-
return $ret;
59+
return
60+
'@@ ' .
61+
'-' . ($i1 + 1) . ',' . ($i2 - $i1) . ' ' .
62+
'+' . ($j1 + 1) . ',' . ($j2 - $j1) . ' ' .
63+
"@@\n";
6864
}
6965

7066
/**
71-
* Render the hunk header.
67+
* Render the hunk content.
7268
*
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
7771
*/
78-
protected function renderHunkHeader(int $a1, int $a2, int $b1, int $b2): string
72+
protected function renderHunkBlocks(Differ $differ, array $hunk): string
7973
{
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;
8197
}
8298

8399
/**

0 commit comments

Comments
 (0)