Skip to content

Commit da621b8

Browse files
SpacePossumsebastianbergmann
authored andcommitted
Fix some phpstan findings
1 parent b4ccd85 commit da621b8

22 files changed

+254
-48
lines changed

phpstan.neon

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
parameters:
2-
level: 2
2+
level: 5
33
paths:
44
- src
55
- tests
6+
ignoreErrors:
7+
-
8+
message: '#Method SebastianBergmann\\Diff\\Differ::calculateEstimatedFootprint\(\) never returns float so it can be removed from the return type\.#'
9+
path: src/Differ.php

src/Differ.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public function diffToArray(array|string $from, array|string $to, ?LongestCommon
8484
reset($to);
8585

8686
foreach ($common as $token) {
87-
while (($fromToken = reset($from)) !== $token) {
87+
while ((/* from-token */ reset($from)) !== $token) {
8888
$diff[] = [array_shift($from), self::REMOVED];
8989
}
9090

91-
while (($toToken = reset($to)) !== $token) {
91+
while ((/* to-token */ reset($to)) !== $token) {
9292
$diff[] = [array_shift($to), self::ADDED];
9393
}
9494

src/MemoryEfficientLongestCommonSubsequenceCalculator.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use function array_slice;
1616
use function count;
1717
use function in_array;
18-
use function max;
1918

2019
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
2120
{

src/Output/AbstractChunkOutputBuilder.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
1616
/**
1717
* Takes input of the diff array and returns the common parts.
1818
* Iterates through diff line by line.
19+
*
20+
* @return array<int, positive-int>
1921
*/
2022
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
2123
{

src/Output/DiffOnlyOutputBuilder.php

+3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010
namespace SebastianBergmann\Diff\Output;
1111

12+
use function assert;
1213
use function fclose;
1314
use function fopen;
1415
use function fwrite;
16+
use function is_resource;
1517
use function str_ends_with;
1618
use function stream_get_contents;
1719
use function substr;
@@ -33,6 +35,7 @@ public function __construct(string $header = "--- Original\n+++ New\n")
3335
public function getDiff(array $diff): string
3436
{
3537
$buffer = fopen('php://memory', 'r+b');
38+
assert(is_resource($buffer));
3639

3740
if ('' !== $this->header) {
3841
fwrite($buffer, $this->header);

src/Output/StrictUnifiedDiffOutputBuilder.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
use function array_merge;
1313
use function array_splice;
14+
use function assert;
1415
use function count;
1516
use function fclose;
1617
use function fopen;
1718
use function fwrite;
1819
use function is_bool;
1920
use function is_int;
21+
use function is_resource;
2022
use function is_string;
2123
use function max;
2224
use function min;
@@ -99,6 +101,8 @@ public function getDiff(array $diff): string
99101
$this->changed = false;
100102

101103
$buffer = fopen('php://memory', 'r+b');
104+
assert(is_resource($buffer));
105+
102106
fwrite($buffer, $this->header);
103107

104108
$this->writeDiffHunks($buffer, $diff);

src/Output/UnifiedDiffOutputBuilder.php

+3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
namespace SebastianBergmann\Diff\Output;
1111

1212
use function array_splice;
13+
use function assert;
1314
use function count;
1415
use function fclose;
1516
use function fopen;
1617
use function fwrite;
18+
use function is_resource;
1719
use function max;
1820
use function min;
1921
use function str_ends_with;
@@ -45,6 +47,7 @@ public function __construct(string $header = "--- Original\n+++ New\n", bool $ad
4547
public function getDiff(array $diff): string
4648
{
4749
$buffer = fopen('php://memory', 'r+b');
50+
assert(is_resource($buffer));
4851

4952
if ('' !== $this->header) {
5053
fwrite($buffer, $this->header);

src/Parser.php

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function parse(string $string): array
7171
return $diffs;
7272
}
7373

74+
/**
75+
* @param string[] $lines
76+
*/
7477
private function parseFileDiff(Diff $diff, array $lines): void
7578
{
7679
$chunks = [];

src/TimeEfficientLongestCommonSubsequenceCalculator.php

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use function array_reverse;
1313
use function count;
14-
use function max;
1514
use SplFixedArray;
1615

1716
final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator

tests/DifferTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ public static function arrayProvider(): array
189189
];
190190
}
191191

192+
/**
193+
* @return array<
194+
* array{
195+
* 0: string,
196+
* 1: string,
197+
* 2: string,
198+
* }
199+
* >
200+
*/
192201
public static function textProvider(): array
193202
{
194203
return [
@@ -255,6 +264,14 @@ public static function textProvider(): array
255264
];
256265
}
257266

267+
/**
268+
* @return array<
269+
* array{
270+
* 0: array<string>,
271+
* 1: string,
272+
* }
273+
* >
274+
*/
258275
public static function provideSplitStringByLinesCases(): array
259276
{
260277
return [
@@ -368,7 +385,6 @@ public function testSplitStringByLines(array $expected, string $input): void
368385
{
369386
$reflection = new ReflectionObject($this->differ);
370387
$method = $reflection->getMethod('splitStringByLines');
371-
$method->setAccessible(true);
372388

373389
$this->assertSame($expected, $method->invoke($this->differ, $input));
374390
}

tests/Output/AbstractChunkOutputBuilderTest.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#[UsesClass(TimeEfficientLongestCommonSubsequenceCalculator::class)]
2323
final class AbstractChunkOutputBuilderTest extends TestCase
2424
{
25+
/**
26+
* @return array<
27+
* array{
28+
* 0: array<int, int>,
29+
* 1: string,
30+
* 2: string,
31+
* 3?: int,
32+
* }
33+
* >
34+
*/
2535
public static function provideGetCommonChunks(): array
2636
{
2737
return [
@@ -122,6 +132,9 @@ public static function provideGetCommonChunks(): array
122132
];
123133
}
124134

135+
/**
136+
* @param array<int, positive-int> $expected
137+
*/
125138
#[DataProvider('provideGetCommonChunks')]
126139
public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void
127140
{
@@ -132,7 +145,10 @@ public function getDiff(array $diff): string
132145
return '';
133146
}
134147

135-
public function getChunks(array $diff, $lineThreshold)
148+
/**
149+
* @return array<int, positive-int>
150+
*/
151+
public function getChunks(array $diff, int $lineThreshold): array
136152
{
137153
return $this->getCommonChunks($diff, $lineThreshold);
138154
}

tests/Output/DiffOnlyOutputBuilderTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
#[UsesClass(TimeEfficientLongestCommonSubsequenceCalculator::class)]
2222
final class DiffOnlyOutputBuilderTest extends TestCase
2323
{
24+
/**
25+
* @return array<
26+
* array{
27+
* 0: string,
28+
* 1: string,
29+
* 2: string,
30+
* 3?: string,
31+
* }
32+
* >
33+
*/
2434
public static function textForNoNonDiffLinesProvider(): array
2535
{
2636
return [

tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public static function provideBasicDiffGeneration(): array
6363
return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration();
6464
}
6565

66-
public static function provideFilePairs(): array
66+
/**
67+
* @return iterable<string, array{0: string, 1: string}>
68+
*/
69+
public static function provideFilePairs(): iterable
6770
{
6871
$cases = [];
6972
$fromFile = __FILE__;
@@ -77,9 +80,10 @@ public static function provideFilePairs(): array
7780
continue;
7881
}
7982

80-
$toFile = $file->getPathname();
81-
$cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile))] = [$fromFile, $toFile];
82-
$fromFile = $toFile;
83+
$toFile = $file->getPathname();
84+
85+
yield sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile)) => [$fromFile, $toFile];
86+
$fromFile = $toFile;
8387
}
8488

8589
return $cases;
@@ -119,7 +123,7 @@ public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, st
119123
return;
120124
}
121125

122-
$this->doIntegrationTestGitApply($diff, $from, $to);
126+
$this->doIntegrationTestGitApply($diff, $from);
123127
}
124128

125129
#[DataProvider('provideFilePairs')]
@@ -145,7 +149,7 @@ public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, strin
145149
#[DataProvider('provideSample')]
146150
public function testIntegrationOfUnitTestCasesGitApply(string $expected, string $from, string $to): void
147151
{
148-
$this->doIntegrationTestGitApply($expected, $from, $to);
152+
$this->doIntegrationTestGitApply($expected, $from);
149153
}
150154

151155
#[DataProvider('provideBasicDiffGeneration')]
@@ -191,7 +195,7 @@ public function testIntegrationDiffOutputBuilderVersusDiffCommand(string $diff,
191195
$this->assertSame($diff, $output);
192196
}
193197

194-
private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
198+
private function doIntegrationTestGitApply(string $diff, string $from): void
195199
{
196200
$this->assertNotSame('', $diff);
197201
$this->assertValidUnifiedDiffFormat($diff);

tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use const PREG_SPLIT_DELIM_CAPTURE;
1414
use const PREG_SPLIT_NO_EMPTY;
1515
use function array_filter;
16+
use function assert;
1617
use function file_put_contents;
1718
use function implode;
19+
use function is_array;
1820
use function is_string;
1921
use function preg_replace;
2022
use function preg_split;
@@ -43,6 +45,15 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
4345
private string $fileFrom;
4446
private string $filePatch;
4547

48+
/**
49+
* @return array{
50+
* string?: array{
51+
* 0: string,
52+
* 1: string,
53+
* 2: string,
54+
* },
55+
* }
56+
*/
4657
public static function provideDiffWithLineNumbers(): array
4758
{
4859
return array_filter(
@@ -70,15 +81,15 @@ protected function tearDown(): void
7081
}
7182

7283
#[DataProvider('provideDiffWithLineNumbers')]
73-
public function testDiffWithLineNumbersPath($expected, $from, $to): void
84+
public function testDiffWithLineNumbersPath(string $expected, string $from, string $to): void
7485
{
7586
$this->doIntegrationTestPatch($expected, $from, $to);
7687
}
7788

7889
#[DataProvider('provideDiffWithLineNumbers')]
79-
public function testDiffWithLineNumbersGitApply($expected, $from, $to): void
90+
public function testDiffWithLineNumbersGitApply(string $expected, string $from): void
8091
{
81-
$this->doIntegrationTestGitApply($expected, $from, $to);
92+
$this->doIntegrationTestGitApply($expected, $from);
8293
}
8394

8495
private function doIntegrationTestPatch(string $diff, string $from, string $to): void
@@ -109,7 +120,7 @@ private function doIntegrationTestPatch(string $diff, string $from, string $to):
109120
);
110121
}
111122

112-
private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
123+
private function doIntegrationTestGitApply(string $diff, string $from): void
113124
{
114125
$this->assertNotSame('', $diff);
115126
$this->assertValidUnifiedDiffFormat($diff);
@@ -154,7 +165,9 @@ private function cleanUpTempFiles(): void
154165

155166
private static function setDiffFileHeader(string $diff, string $file): string
156167
{
157-
$diffLines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
168+
$diffLines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
169+
assert(is_array($diffLines));
170+
158171
$diffLines[0] = preg_replace('#^\-\-\- .*#', '--- /' . $file, $diffLines[0], 1);
159172
$diffLines[1] = preg_replace('#^\+\+\+ .*#', '+++ /' . $file, $diffLines[1], 1);
160173

tests/Output/StrictUnifiedDiffOutputBuilderDataProvider.php

+38
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111

1212
final class StrictUnifiedDiffOutputBuilderDataProvider
1313
{
14+
/**
15+
* @return array<
16+
* array{
17+
* 0: string,
18+
* 1: string,
19+
* 2: string,
20+
* 3: array{
21+
* "fromFile": string,
22+
* "toFile": string,
23+
* "collapseRanges"?: bool,
24+
* "fromFileDate"?: string,
25+
* "toFileDate"?: string,
26+
* },
27+
* },
28+
* >
29+
*/
1430
public static function provideOutputBuildingCases(): array
1531
{
1632
return [
@@ -72,6 +88,19 @@ public static function provideOutputBuildingCases(): array
7288
];
7389
}
7490

91+
/**
92+
* @return array<
93+
* array{
94+
* 0: string,
95+
* 1: string,
96+
* 2: string,
97+
* 3: array{
98+
* "fromFile": string,
99+
* "toFile": string,
100+
* },
101+
* },
102+
* >
103+
*/
75104
public static function provideSample(): array
76105
{
77106
return [
@@ -97,6 +126,15 @@ public static function provideSample(): array
97126
];
98127
}
99128

129+
/**
130+
* @return array<
131+
* array{
132+
* 0: string,
133+
* 1: string,
134+
* 2: string,
135+
* }
136+
* >
137+
*/
100138
public static function provideBasicDiffGeneration(): array
101139
{
102140
return [

0 commit comments

Comments
 (0)