Skip to content

Commit 6fb7a31

Browse files
Cleanup
1 parent c159f3d commit 6fb7a31

16 files changed

+65
-254
lines changed

src/Chunk.php

+7-22
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,15 @@
1111

1212
final class Chunk
1313
{
14-
/**
15-
* @var int
16-
*/
17-
private $start;
14+
private int $start;
1815

19-
/**
20-
* @var int
21-
*/
22-
private $startRange;
16+
private int $startRange;
2317

24-
/**
25-
* @var int
26-
*/
27-
private $end;
18+
private int $end;
2819

29-
/**
30-
* @var int
31-
*/
32-
private $endRange;
20+
private int $endRange;
3321

34-
/**
35-
* @var Line[]
36-
*/
37-
private $lines;
22+
private array $lines;
3823

3924
public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = [])
4025
{
@@ -66,15 +51,15 @@ public function getEndRange(): int
6651
}
6752

6853
/**
69-
* @return Line[]
54+
* @psalm-return list<Line>
7055
*/
7156
public function getLines(): array
7257
{
7358
return $this->lines;
7459
}
7560

7661
/**
77-
* @param Line[] $lines
62+
* @psalm-param list<Line> $lines
7863
*/
7964
public function setLines(array $lines): void
8065
{

src/Diff.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,17 @@
1111

1212
final class Diff
1313
{
14-
/**
15-
* @var string
16-
*/
17-
private $from;
14+
private string $from;
1815

19-
/**
20-
* @var string
21-
*/
22-
private $to;
16+
private string $to;
2317

2418
/**
25-
* @var Chunk[]
19+
* @psalm-var list<Chunk>
2620
*/
27-
private $chunks;
21+
private array $chunks;
2822

2923
/**
30-
* @param Chunk[] $chunks
24+
* @psalm-param list<Chunk> $chunks
3125
*/
3226
public function __construct(string $from, string $to, array $chunks = [])
3327
{
@@ -47,15 +41,15 @@ public function getTo(): string
4741
}
4842

4943
/**
50-
* @return Chunk[]
44+
* @psalm-return list<Chunk>
5145
*/
5246
public function getChunks(): array
5347
{
5448
return $this->chunks;
5549
}
5650

5751
/**
58-
* @param Chunk[] $chunks
52+
* @psalm-param list<Chunk> $chunks
5953
*/
6054
public function setChunks(array $chunks): void
6155
{

src/Differ.php

+8-77
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
1818
use function count;
1919
use function current;
2020
use function end;
21-
use function get_class;
22-
use function gettype;
2321
use function is_array;
24-
use function is_object;
2522
use function is_string;
2623
use function key;
2724
use function min;
2825
use function preg_split;
2926
use function prev;
3027
use function reset;
31-
use function sprintf;
3228
use function substr;
3329
use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface;
3430
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
@@ -45,44 +41,14 @@ final class Differ
4541

4642
public const NO_LINE_END_EOF_WARNING = 4;
4743

48-
/**
49-
* @var DiffOutputBuilderInterface
50-
*/
51-
private $outputBuilder;
52-
53-
/**
54-
* @param DiffOutputBuilderInterface $outputBuilder
55-
*
56-
* @throws InvalidArgumentException
57-
*/
58-
public function __construct($outputBuilder = null)
44+
private DiffOutputBuilderInterface|UnifiedDiffOutputBuilder $outputBuilder;
45+
46+
public function __construct(DiffOutputBuilderInterface $outputBuilder)
5947
{
60-
if ($outputBuilder instanceof DiffOutputBuilderInterface) {
61-
$this->outputBuilder = $outputBuilder;
62-
} elseif (null === $outputBuilder) {
63-
$this->outputBuilder = new UnifiedDiffOutputBuilder;
64-
} elseif (is_string($outputBuilder)) {
65-
// PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support
66-
// @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056
67-
// @deprecated
68-
$this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder);
69-
} else {
70-
throw new InvalidArgumentException(
71-
sprintf(
72-
'Expected builder to be an instance of DiffOutputBuilderInterface, <null> or a string, got %s.',
73-
is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"'
74-
)
75-
);
76-
}
48+
$this->outputBuilder = $outputBuilder;
7749
}
7850

79-
/**
80-
* Returns the diff between two arrays or strings as string.
81-
*
82-
* @param array|string $from
83-
* @param array|string $to
84-
*/
85-
public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string
51+
public function diff(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): string
8652
{
8753
$diff = $this->diffToArray(
8854
$this->normalizeDiffInput($from),
@@ -93,33 +59,14 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null)
9359
return $this->outputBuilder->getDiff($diff);
9460
}
9561

96-
/**
97-
* Returns the diff between two arrays or strings as array.
98-
*
99-
* Each array element contains two elements:
100-
* - [0] => mixed $token
101-
* - [1] => 2|1|0
102-
*
103-
* - 2: REMOVED: $token was removed from $from
104-
* - 1: ADDED: $token was added to $from
105-
* - 0: OLD: $token is not changed in $to
106-
*
107-
* @param array|string $from
108-
* @param array|string $to
109-
* @param LongestCommonSubsequenceCalculator $lcs
110-
*/
111-
public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array
62+
public function diffToArray(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): array
11263
{
11364
if (is_string($from)) {
11465
$from = $this->splitStringByLines($from);
115-
} elseif (!is_array($from)) {
116-
throw new InvalidArgumentException('"from" must be an array or string.');
11766
}
11867

11968
if (is_string($to)) {
12069
$to = $this->splitStringByLines($to);
121-
} elseif (!is_array($to)) {
122-
throw new InvalidArgumentException('"to" must be an array or string.');
12370
}
12471

12572
[$from, $to, $start, $end] = self::getArrayDiffParted($from, $to);
@@ -172,12 +119,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
172119
return $diff;
173120
}
174121

175-
/**
176-
* Casts variable to string if it is not a string or array.
177-
*
178-
* @return array|string
179-
*/
180-
private function normalizeDiffInput($input)
122+
private function normalizeDiffInput(array|string $input)
181123
{
182124
if (!is_array($input) && !is_string($input)) {
183125
return (string) $input;
@@ -186,9 +128,6 @@ private function normalizeDiffInput($input)
186128
return $input;
187129
}
188130

189-
/**
190-
* Checks if input is string, if so it will split it line-by-line.
191-
*/
192131
private function splitStringByLines(string $input): array
193132
{
194133
return preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
@@ -209,21 +148,13 @@ private function selectLcsImplementation(array $from, array $to): LongestCommonS
209148
return new TimeEfficientLongestCommonSubsequenceCalculator;
210149
}
211150

212-
/**
213-
* Calculates the estimated memory footprint for the DP-based method.
214-
*
215-
* @return float|int
216-
*/
217-
private function calculateEstimatedFootprint(array $from, array $to)
151+
private function calculateEstimatedFootprint(array $from, array $to): float|int
218152
{
219153
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
220154

221155
return $itemSize * min(count($from), count($to)) ** 2;
222156
}
223157

224-
/**
225-
* Returns true if line ends don't match in a diff.
226-
*/
227158
private function detectUnmatchedLineEndings(array $diff): bool
228159
{
229160
$newLineBreaks = ['' => true];

src/Line.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ final class Line
1717

1818
public const UNCHANGED = 3;
1919

20-
/**
21-
* @var int
22-
*/
23-
private $type;
24-
25-
/**
26-
* @var string
27-
*/
28-
private $content;
20+
private int $type;
21+
22+
private string $content;
2923

3024
public function __construct(int $type = self::UNCHANGED, string $content = '')
3125
{

src/Output/DiffOnlyOutputBuilder.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
*/
2323
final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
2424
{
25-
/**
26-
* @var string
27-
*/
28-
private $header;
25+
private string $header;
2926

3027
public function __construct(string $header = "--- Original\n+++ New\n")
3128
{

src/Output/StrictUnifiedDiffOutputBuilder.php

+8-17
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
3535
{
36-
private static $default = [
36+
private static array $default = [
3737
'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1`
3838
'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed)
3939
'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3
@@ -43,30 +43,21 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
4343
'toFileDate' => null,
4444
];
4545

46-
/**
47-
* @var bool
48-
*/
49-
private $changed;
46+
private bool $changed;
5047

51-
/**
52-
* @var bool
53-
*/
54-
private $collapseRanges;
48+
private bool $collapseRanges;
5549

5650
/**
57-
* @var int >= 0
51+
* @psalm-var positive-int
5852
*/
59-
private $commonLineThreshold;
53+
private int $commonLineThreshold;
6054

61-
/**
62-
* @var string
63-
*/
64-
private $header;
55+
private string $header;
6556

6657
/**
67-
* @var int >= 0
58+
* @psalm-var positive-int
6859
*/
69-
private $contextLines;
60+
private int $contextLines;
7061

7162
public function __construct(array $options = [])
7263
{

src/Output/UnifiedDiffOutputBuilder.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,18 @@
2626
*/
2727
final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
2828
{
29-
/**
30-
* @var bool
31-
*/
32-
private $collapseRanges = true;
29+
private bool $collapseRanges = true;
3330

34-
/**
35-
* @var int >= 0
36-
*/
37-
private $commonLineThreshold = 6;
31+
private int $commonLineThreshold = 6;
3832

3933
/**
40-
* @var int >= 0
34+
* @psalm-var positive-int
4135
*/
4236
private $contextLines = 3;
4337

44-
/**
45-
* @var string
46-
*/
47-
private $header;
38+
private string $header;
4839

49-
/**
50-
* @var bool
51-
*/
52-
private $addLineNumbers;
40+
private bool $addLineNumbers;
5341

5442
public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false)
5543
{

tests/ChunkTest.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
*/
1919
final class ChunkTest extends TestCase
2020
{
21-
/**
22-
* @var Chunk
23-
*/
24-
private $chunk;
21+
private Chunk $chunk;
2522

2623
protected function setUp(): void
2724
{

0 commit comments

Comments
 (0)