Skip to content

Commit 99703b2

Browse files
SpacePossumsebastianbergmann
authored andcommitted
use consts
1 parent dea104f commit 99703b2

File tree

5 files changed

+92
-94
lines changed

5 files changed

+92
-94
lines changed

src/Differ.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
*/
1919
final class Differ
2020
{
21+
const OLD = 0;
22+
const ADDED = 1;
23+
const REMOVED = 2;
24+
const DIFF_LINE_END_WARNING = 3;
25+
const NO_LINE_END_EOF_WARNING = 4;
26+
2127
/**
2228
* @var DiffOutputBuilderInterface
2329
*/
@@ -60,9 +66,11 @@ public function __construct($outputBuilder = null)
6066
*/
6167
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
6268
{
63-
$from = $this->normalizeDiffInput($from);
64-
$to = $this->normalizeDiffInput($to);
65-
$diff = $this->diffToArray($from, $to, $lcs);
69+
$diff = $this->diffToArray(
70+
$this->normalizeDiffInput($from),
71+
$this->normalizeDiffInput($to),
72+
$lcs
73+
);
6674

6775
return $this->outputBuilder->getDiff($diff);
6876
}
@@ -124,41 +132,41 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
124132
$diff = [];
125133

126134
foreach ($start as $token) {
127-
$diff[] = [$token, 0 /* OLD */];
135+
$diff[] = [$token, self::OLD];
128136
}
129137

130138
\reset($from);
131139
\reset($to);
132140

133141
foreach ($common as $token) {
134142
while (($fromToken = \reset($from)) !== $token) {
135-
$diff[] = [\array_shift($from), 2 /* REMOVED */];
143+
$diff[] = [\array_shift($from), self::REMOVED];
136144
}
137145

138146
while (($toToken = \reset($to)) !== $token) {
139-
$diff[] = [\array_shift($to), 1 /* ADDED */];
147+
$diff[] = [\array_shift($to), self::ADDED];
140148
}
141149

142-
$diff[] = [$token, 0 /* OLD */];
150+
$diff[] = [$token, self::OLD];
143151

144152
\array_shift($from);
145153
\array_shift($to);
146154
}
147155

148156
while (($token = \array_shift($from)) !== null) {
149-
$diff[] = [$token, 2 /* REMOVED */];
157+
$diff[] = [$token, self::REMOVED];
150158
}
151159

152160
while (($token = \array_shift($to)) !== null) {
153-
$diff[] = [$token, 1 /* ADDED */];
161+
$diff[] = [$token, self::ADDED];
154162
}
155163

156164
foreach ($end as $token) {
157-
$diff[] = [$token, 0 /* OLD */];
165+
$diff[] = [$token, self::OLD];
158166
}
159167

160168
if ($this->detectUnmatchedLineEndings($diff)) {
161-
\array_unshift($diff, ["#Warning: Strings contain different line endings!\n", 3]);
169+
\array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
162170
}
163171

164172
return $diff;
@@ -225,13 +233,13 @@ private function detectUnmatchedLineEndings(array $diff): bool
225233
$oldLineBreaks = ['' => true];
226234

227235
foreach ($diff as $entry) {
228-
if (0 === $entry[1]) { /* OLD */
236+
if (self::OLD === $entry[1]) {
229237
$ln = $this->getLinebreak($entry[0]);
230238
$oldLineBreaks[$ln] = true;
231239
$newLineBreaks[$ln] = true;
232-
} elseif (1 === $entry[1]) { /* ADDED */
240+
} elseif (self::ADDED === $entry[1]) {
233241
$newLineBreaks[$this->getLinebreak($entry[0])] = true;
234-
} elseif (2 === $entry[1]) { /* REMOVED */
242+
} elseif (self::REMOVED === $entry[1]) {
235243
$oldLineBreaks[$this->getLinebreak($entry[0])] = true;
236244
}
237245
}

src/Output/DiffOnlyOutputBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace SebastianBergmann\Diff\Output;
1212

13+
use SebastianBergmann\Diff\Differ;
14+
1315
/**
1416
* Builds a diff string representation in a loose unified diff format
1517
* listing only changes lines. Does not include line numbers.
@@ -38,11 +40,11 @@ public function getDiff(array $diff): string
3840
}
3941

4042
foreach ($diff as $diffEntry) {
41-
if ($diffEntry[1] === 1 /* ADDED */) {
43+
if ($diffEntry[1] === Differ::ADDED) {
4244
\fwrite($buffer, '+' . $diffEntry[0]);
43-
} elseif ($diffEntry[1] === 2 /* REMOVED */) {
45+
} elseif ($diffEntry[1] === Differ::REMOVED) {
4446
\fwrite($buffer, '-' . $diffEntry[0]);
45-
} elseif ($diffEntry[1] === 3 /* WARNING */) {
47+
} elseif ($diffEntry[1] === Differ::DIFF_LINE_END_WARNING) {
4648
\fwrite($buffer, ' ' . $diffEntry[0]);
4749

4850
continue; // Warnings should not be tested for line break, it will always be there

src/Output/StrictUnifiedDiffOutputBuilder.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace SebastianBergmann\Diff\Output;
1212

1313
use SebastianBergmann\Diff\ConfigurationException;
14+
use SebastianBergmann\Diff\Differ;
1415

1516
/**
1617
* Strict Unified diff output builder.
@@ -19,11 +20,6 @@
1920
*/
2021
final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
2122
{
22-
/**
23-
* @var int
24-
*/
25-
private static $noNewlineAtOEFid = 998877;
26-
2723
/**
2824
* @var bool
2925
*/
@@ -142,7 +138,7 @@ private function writeDiffHunks($output, array $diff)
142138
if (0 === $diff[$upperLimit - 1][1]) {
143139
$lc = \substr($diff[$upperLimit - 1][0], -1);
144140
if ("\n" !== $lc) {
145-
\array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", self::$noNewlineAtOEFid]]);
141+
\array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
146142
}
147143
} else {
148144
// search back for the last `+` and `-` line,
@@ -153,7 +149,7 @@ private function writeDiffHunks($output, array $diff)
153149
unset($toFind[$diff[$i][1]]);
154150
$lc = \substr($diff[$i][0], -1);
155151
if ("\n" !== $lc) {
156-
\array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", self::$noNewlineAtOEFid]]);
152+
\array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
157153
}
158154

159155
if (!\count($toFind)) {
@@ -223,7 +219,7 @@ private function writeDiffHunks($output, array $diff)
223219

224220
$sameCount = 0;
225221

226-
if ($entry[1] === self::$noNewlineAtOEFid) {
222+
if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) {
227223
continue;
228224
}
229225

@@ -233,11 +229,11 @@ private function writeDiffHunks($output, array $diff)
233229
$hunkCapture = $i;
234230
}
235231

236-
if (1 === $entry[1]) { // added
232+
if (Differ::ADDED === $entry[1]) { // added
237233
++$toRange;
238234
}
239235

240-
if (2 === $entry[1]) { // removed
236+
if (Differ::REMOVED === $entry[1]) { // removed
241237
++$fromRange;
242238
}
243239
}
@@ -297,19 +293,19 @@ private function writeHunk(
297293
\fwrite($output, " @@\n");
298294

299295
for ($i = $diffStartIndex; $i < $diffEndIndex; ++$i) {
300-
if ($diff[$i][1] === 1) { // added
296+
if ($diff[$i][1] === Differ::ADDED) {
301297
$this->changed = true;
302298
\fwrite($output, '+' . $diff[$i][0]);
303-
} elseif ($diff[$i][1] === 2) { // removed
299+
} elseif ($diff[$i][1] === Differ::REMOVED) {
304300
$this->changed = true;
305301
\fwrite($output, '-' . $diff[$i][0]);
306-
} elseif ($diff[$i][1] === 0) { // same
302+
} elseif ($diff[$i][1] === Differ::OLD) {
307303
\fwrite($output, ' ' . $diff[$i][0]);
308-
} elseif ($diff[$i][1] === self::$noNewlineAtOEFid) {
304+
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
309305
$this->changed = true;
310306
\fwrite($output, $diff[$i][0]);
311307
}
312-
//} elseif ($diff[$i][1] === 3) { // custom comment inserted by PHPUnit/diff package
308+
//} elseif ($diff[$i][1] === Differ::DIFF_LINE_END_WARNING) { // custom comment inserted by PHPUnit/diff package
313309
// skip
314310
//} else {
315311
// unknown/invalid

src/Output/UnifiedDiffOutputBuilder.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010

1111
namespace SebastianBergmann\Diff\Output;
1212

13+
use SebastianBergmann\Diff\Differ;
14+
1315
/**
1416
* Builds a diff string representation in unified diff format in chunks.
1517
*/
1618
final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
1719
{
18-
/**
19-
* @var int
20-
*/
21-
private static $noNewlineAtOEFid = 998877;
22-
2320
/**
2421
* @var bool
2522
*/
@@ -77,7 +74,7 @@ public function getDiff(array $diff): string
7774
return "\n" !== $last && "\r" !== $last
7875
? $diff . "\n"
7976
: $diff
80-
;
77+
;
8178
}
8279

8380
private function writeDiffHunks($output, array $diff)
@@ -89,7 +86,7 @@ private function writeDiffHunks($output, array $diff)
8986
if (0 === $diff[$upperLimit - 1][1]) {
9087
$lc = \substr($diff[$upperLimit - 1][0], -1);
9188
if ("\n" !== $lc) {
92-
\array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", self::$noNewlineAtOEFid]]);
89+
\array_splice($diff, $upperLimit, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
9390
}
9491
} else {
9592
// search back for the last `+` and `-` line,
@@ -100,7 +97,7 @@ private function writeDiffHunks($output, array $diff)
10097
unset($toFind[$diff[$i][1]]);
10198
$lc = \substr($diff[$i][0], -1);
10299
if ("\n" !== $lc) {
103-
\array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", self::$noNewlineAtOEFid]]);
100+
\array_splice($diff, $i + 1, 0, [["\n\\ No newline at end of file\n", Differ::NO_LINE_END_EOF_WARNING]]);
104101
}
105102

106103
if (!\count($toFind)) {
@@ -170,19 +167,19 @@ private function writeDiffHunks($output, array $diff)
170167

171168
$sameCount = 0;
172169

173-
if ($entry[1] === self::$noNewlineAtOEFid) {
170+
if ($entry[1] === Differ::NO_LINE_END_EOF_WARNING) {
174171
continue;
175172
}
176173

177174
if (false === $hunkCapture) {
178175
$hunkCapture = $i;
179176
}
180177

181-
if (1 === $entry[1]) { // added
178+
if (Differ::ADDED === $entry[1]) {
182179
++$toRange;
183180
}
184181

185-
if (2 === $entry[1]) { // removed
182+
if (Differ::REMOVED === $entry[1]) {
186183
++$fromRange;
187184
}
188185
}
@@ -246,15 +243,15 @@ private function writeHunk(
246243
}
247244

248245
for ($i = $diffStartIndex; $i < $diffEndIndex; ++$i) {
249-
if ($diff[$i][1] === 1) { // added
246+
if ($diff[$i][1] === Differ::ADDED) {
250247
\fwrite($output, '+' . $diff[$i][0]);
251-
} elseif ($diff[$i][1] === 2) { // removed
248+
} elseif ($diff[$i][1] === Differ::REMOVED) {
252249
\fwrite($output, '-' . $diff[$i][0]);
253-
} elseif ($diff[$i][1] === 0) { // same
250+
} elseif ($diff[$i][1] === Differ::OLD) {
254251
\fwrite($output, ' ' . $diff[$i][0]);
255-
} elseif ($diff[$i][1] === self::$noNewlineAtOEFid) {
252+
} elseif ($diff[$i][1] === Differ::NO_LINE_END_EOF_WARNING) {
256253
\fwrite($output, "\n"); // $diff[$i][0]
257-
} else { /* Not changed (old) 0 or Warning 3 */
254+
} else { /* Not changed (old) Differ::OLD or Warning Differ::DIFF_LINE_END_WARNING */
258255
\fwrite($output, ' ' . $diff[$i][0]);
259256
}
260257
}

0 commit comments

Comments
 (0)