Skip to content

Commit cd85830

Browse files
Fix CS/WS issues
1 parent 07ffdad commit cd85830

21 files changed

+488
-337
lines changed

src/Differ.php

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
99
*/
1010
namespace SebastianBergmann\Diff;
1111

12+
use const PHP_INT_SIZE;
13+
use const PREG_SPLIT_DELIM_CAPTURE;
14+
use const PREG_SPLIT_NO_EMPTY;
15+
use function array_shift;
16+
use function array_unshift;
17+
use function array_values;
18+
use function count;
19+
use function current;
20+
use function end;
21+
use function get_class;
22+
use function gettype;
23+
use function is_array;
24+
use function is_object;
25+
use function is_string;
26+
use function key;
27+
use function min;
28+
use function preg_split;
29+
use function prev;
30+
use function reset;
31+
use function sprintf;
32+
use function substr;
1233
use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
1334
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
1435

@@ -40,16 +61,16 @@ public function __construct($outputBuilder = null)
4061
$this->outputBuilder = $outputBuilder;
4162
} elseif (null === $outputBuilder) {
4263
$this->outputBuilder = new UnifiedDiffOutputBuilder;
43-
} elseif (\is_string($outputBuilder)) {
64+
} elseif (is_string($outputBuilder)) {
4465
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
4566
// @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
4667
// @deprecated
4768
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
4869
} else {
4970
throw new InvalidArgumentException(
50-
\sprintf(
71+
sprintf(
5172
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
52-
\is_object($outputBuilder) ? 'instance of "' . \get_class($outputBuilder) . '"' : \gettype($outputBuilder) . ' "' . $outputBuilder . '"'
73+
is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"'
5374
)
5475
);
5576
}
@@ -89,15 +110,15 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
89110
*/
90111
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
91112
{
92-
if (\is_string($from)) {
113+
if (is_string($from)) {
93114
$from = $this->splitStringByLines($from);
94-
} elseif (!\is_array($from)) {
115+
} elseif (!is_array($from)) {
95116
throw new InvalidArgumentException('"from" must be an array or string.');
96117
}
97118

98-
if (\is_string($to)) {
119+
if (is_string($to)) {
99120
$to = $this->splitStringByLines($to);
100-
} elseif (!\is_array($to)) {
121+
} elseif (!is_array($to)) {
101122
throw new InvalidArgumentException('"to" must be an array or string.');
102123
}
103124

@@ -107,36 +128,36 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
107128
$lcs = $this->selectLcsImplementation($from, $to);
108129
}
109130

110-
$common = $lcs->calculate(\array_values($from), \array_values($to));
131+
$common = $lcs->calculate(array_values($from), array_values($to));
111132
$diff = [];
112133

113134
foreach ($start as $token) {
114135
$diff[] = [$token, self::OLD];
115136
}
116137

117-
\reset($from);
118-
\reset($to);
138+
reset($from);
139+
reset($to);
119140

120141
foreach ($common as $token) {
121-
while (($fromToken = \reset($from)) !== $token) {
122-
$diff[] = [\array_shift($from), self::REMOVED];
142+
while (($fromToken = reset($from)) !== $token) {
143+
$diff[] = [array_shift($from), self::REMOVED];
123144
}
124145

125-
while (($toToken = \reset($to)) !== $token) {
126-
$diff[] = [\array_shift($to), self::ADDED];
146+
while (($toToken = reset($to)) !== $token) {
147+
$diff[] = [array_shift($to), self::ADDED];
127148
}
128149

129150
$diff[] = [$token, self::OLD];
130151

131-
\array_shift($from);
132-
\array_shift($to);
152+
array_shift($from);
153+
array_shift($to);
133154
}
134155

135-
while (($token = \array_shift($from)) !== null) {
156+
while (($token = array_shift($from)) !== null) {
136157
$diff[] = [$token, self::REMOVED];
137158
}
138159

139-
while (($token = \array_shift($to)) !== null) {
160+
while (($token = array_shift($to)) !== null) {
140161
$diff[] = [$token, self::ADDED];
141162
}
142163

@@ -145,7 +166,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
145166
}
146167

147168
if ($this->detectUnmatchedLineEndings($diff)) {
148-
\array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
169+
array_unshift($diff, ["#Warning: Strings contain different line endings!\n", self::DIFF_LINE_END_WARNING]);
149170
}
150171

151172
return $diff;
@@ -158,7 +179,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
158179
*/
159180
private function normalizeDiffInput($input)
160181
{
161-
if (!\is_array($input) && !\is_string($input)) {
182+
if (!is_array($input) && !is_string($input)) {
162183
return (string) $input;
163184
}
164185

@@ -170,7 +191,7 @@ private function normalizeDiffInput($input)
170191
*/
171192
private function splitStringByLines(string $input): array
172193
{
173-
return \preg_split('/(.*\R)/', $input, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
194+
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
174195
}
175196

176197
private function selectLcsImplementation(array $from, array $to): LongestCommonSubsequenceCalculator
@@ -195,9 +216,9 @@ private function selectLcsImplementation(array $from, array $to): LongestCommonS
195216
*/
196217
private function calculateEstimatedFootprint(array $from, array $to)
197218
{
198-
$itemSize = \PHP_INT_SIZE === 4 ? 76 : 144;
219+
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
199220

200-
return $itemSize * \min(\count($from), \count($to)) ** 2;
221+
return $itemSize * min(count($from), count($to)) ** 2;
201222
}
202223

203224
/**
@@ -243,11 +264,11 @@ private function detectUnmatchedLineEndings(array $diff): bool
243264

244265
private function getLinebreak($line): string
245266
{
246-
if (!\is_string($line)) {
267+
if (!is_string($line)) {
247268
return '';
248269
}
249270

250-
$lc = \substr($line, -1);
271+
$lc = substr($line, -1);
251272

252273
if ("\r" === $lc) {
253274
return "\r";
@@ -257,7 +278,7 @@ private function getLinebreak($line): string
257278
return '';
258279
}
259280

260-
if ("\r\n" === \substr($line, -2)) {
281+
if ("\r\n" === substr($line, -2)) {
261282
return "\r\n";
262283
}
263284

@@ -269,10 +290,10 @@ private static function getArrayDiffParted(array &$from, array &$to): array
269290
$start = [];
270291
$end = [];
271292

272-
\reset($to);
293+
reset($to);
273294

274295
foreach ($from as $k => $v) {
275-
$toK = \key($to);
296+
$toK = key($to);
276297

277298
if ($toK === $k && $v === $to[$k]) {
278299
$start[$k] = $v;
@@ -283,19 +304,19 @@ private static function getArrayDiffParted(array &$from, array &$to): array
283304
}
284305
}
285306

286-
\end($from);
287-
\end($to);
307+
end($from);
308+
end($to);
288309

289310
do {
290-
$fromK = \key($from);
291-
$toK = \key($to);
311+
$fromK = key($from);
312+
$toK = key($to);
292313

293-
if (null === $fromK || null === $toK || \current($from) !== \current($to)) {
314+
if (null === $fromK || null === $toK || current($from) !== current($to)) {
294315
break;
295316
}
296317

297-
\prev($from);
298-
\prev($to);
318+
prev($from);
319+
prev($to);
299320

300321
$end = [$fromK => $from[$fromK]] + $end;
301322
unset($from[$fromK], $to[$toK]);

src/Exception/ConfigurationException.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@
99
*/
1010
namespace SebastianBergmann\Diff;
1111

12+
use function get_class;
13+
use function gettype;
14+
use function is_object;
15+
use function sprintf;
16+
use Exception;
17+
1218
final class ConfigurationException extends InvalidArgumentException
1319
{
1420
public function __construct(
1521
string $option,
1622
string $expected,
1723
$value,
1824
int $code = 0,
19-
\Exception $previous = null
25+
Exception $previous = null
2026
) {
2127
parent::__construct(
22-
\sprintf(
28+
sprintf(
2329
'Option "%s" must be %s, got "%s".',
2430
$option,
2531
$expected,
26-
\is_object($value) ? \get_class($value) : (null === $value ? '<null>' : \gettype($value) . '#' . $value)
32+
is_object($value) ? get_class($value) : (null === $value ? '<null>' : gettype($value) . '#' . $value)
2733
),
2834
$code,
2935
$previous

src/MemoryEfficientLongestCommonSubsequenceCalculator.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,41 @@
99
*/
1010
namespace SebastianBergmann\Diff;
1111

12+
use function array_fill;
13+
use function array_merge;
14+
use function array_reverse;
15+
use function array_slice;
16+
use function count;
17+
use function in_array;
18+
use function max;
19+
1220
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
1321
{
1422
/**
1523
* {@inheritdoc}
1624
*/
1725
public function calculate(array $from, array $to): array
1826
{
19-
$cFrom = \count($from);
20-
$cTo = \count($to);
27+
$cFrom = count($from);
28+
$cTo = count($to);
2129

2230
if ($cFrom === 0) {
2331
return [];
2432
}
2533

2634
if ($cFrom === 1) {
27-
if (\in_array($from[0], $to, true)) {
35+
if (in_array($from[0], $to, true)) {
2836
return [$from[0]];
2937
}
3038

3139
return [];
3240
}
3341

3442
$i = (int) ($cFrom / 2);
35-
$fromStart = \array_slice($from, 0, $i);
36-
$fromEnd = \array_slice($from, $i);
43+
$fromStart = array_slice($from, 0, $i);
44+
$fromEnd = array_slice($from, $i);
3745
$llB = $this->length($fromStart, $to);
38-
$llE = $this->length(\array_reverse($fromEnd), \array_reverse($to));
46+
$llE = $this->length(array_reverse($fromEnd), array_reverse($to));
3947
$jMax = 0;
4048
$max = 0;
4149

@@ -48,20 +56,20 @@ public function calculate(array $from, array $to): array
4856
}
4957
}
5058

51-
$toStart = \array_slice($to, 0, $jMax);
52-
$toEnd = \array_slice($to, $jMax);
59+
$toStart = array_slice($to, 0, $jMax);
60+
$toEnd = array_slice($to, $jMax);
5361

54-
return \array_merge(
62+
return array_merge(
5563
$this->calculate($fromStart, $toStart),
5664
$this->calculate($fromEnd, $toEnd)
5765
);
5866
}
5967

6068
private function length(array $from, array $to): array
6169
{
62-
$current = \array_fill(0, \count($to) + 1, 0);
63-
$cFrom = \count($from);
64-
$cTo = \count($to);
70+
$current = array_fill(0, count($to) + 1, 0);
71+
$cFrom = count($from);
72+
$cTo = count($to);
6573

6674
for ($i = 0; $i < $cFrom; $i++) {
6775
$prev = $current;
@@ -70,7 +78,7 @@ private function length(array $from, array $to): array
7078
if ($from[$i] === $to[$j]) {
7179
$current[$j + 1] = $prev[$j] + 1;
7280
} else {
73-
$current[$j + 1] = \max($current[$j], $prev[$j + 1]);
81+
$current[$j + 1] = max($current[$j], $prev[$j + 1]);
7482
}
7583
}
7684
}

src/Output/AbstractChunkOutputBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace SebastianBergmann\Diff\Output;
1111

12+
use function count;
13+
1214
abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
1315
{
1416
/**
@@ -17,7 +19,7 @@ abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
1719
*/
1820
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
1921
{
20-
$diffSize = \count($diff);
22+
$diffSize = count($diff);
2123
$capturing = false;
2224
$chunkStart = 0;
2325
$chunkSize = 0;

0 commit comments

Comments
 (0)