Skip to content

Commit 52c7d4e

Browse files
djmattyg007sebastianbergmann
authored andcommitted
Add ability to hide non-diff lines
Sometimes it's desirable to only show lines that have changed, even if it means stripping all context from the diff.
1 parent 07d5cbe commit 52c7d4e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Differ.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Differ
2424
*/
2525
private $header;
2626

27+
/**
28+
* @var bool
29+
*/
30+
private $showNonDiffLines = true;
31+
2732
/**
2833
* @param string $header
2934
*/
@@ -32,6 +37,14 @@ public function __construct($header = "--- Original\n+++ New\n")
3237
$this->header = $header;
3338
}
3439

40+
/**
41+
* @param bool $show
42+
*/
43+
public function setShowNonDiffLines($show)
44+
{
45+
$this->showNonDiffLines = $show;
46+
}
47+
3548
/**
3649
* Returns the diff between two arrays or strings as string.
3750
*
@@ -90,15 +103,17 @@ public function diff($from, $to, LongestCommonSubsequence $lcs = null)
90103
}
91104

92105
if ($newChunk) {
93-
$buffer .= "@@ @@\n";
106+
if ($this->showNonDiffLines === true) {
107+
$buffer .= "@@ @@\n";
108+
}
94109
$newChunk = false;
95110
}
96111

97112
if ($diff[$i][1] === 1 /* ADDED */) {
98113
$buffer .= '+' . $diff[$i][0] . "\n";
99114
} elseif ($diff[$i][1] === 2 /* REMOVED */) {
100115
$buffer .= '-' . $diff[$i][0] . "\n";
101-
} else {
116+
} elseif ($this->showNonDiffLines === true) {
102117
$buffer .= ' ' . $diff[$i][0] . "\n";
103118
}
104119
}

0 commit comments

Comments
 (0)