Skip to content

Commit dd8686b

Browse files
committed
Fix some phpstan findings
1 parent b4ccd85 commit dd8686b

22 files changed

+254
-48
lines changed

phpstan.neon

Lines changed: 5 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 17 additions & 1 deletion
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
}

0 commit comments

Comments
 (0)