Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some phpstan findings #124

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
parameters:
level: 2
level: 5
paths:
- src
- tests
ignoreErrors:
-
message: '#Method SebastianBergmann\\Diff\\Differ::calculateEstimatedFootprint\(\) never returns float so it can be removed from the return type\.#'
path: src/Differ.php
4 changes: 2 additions & 2 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function diffToArray(array|string $from, array|string $to, ?LongestCommon
reset($to);

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

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

Expand Down
1 change: 0 additions & 1 deletion src/MemoryEfficientLongestCommonSubsequenceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function array_slice;
use function count;
use function in_array;
use function max;

final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Output/AbstractChunkOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract class AbstractChunkOutputBuilder implements DiffOutputBuilderInterface
/**
* Takes input of the diff array and returns the common parts.
* Iterates through diff line by line.
*
* @return array<int, positive-int>
*/
protected function getCommonChunks(array $diff, int $lineThreshold = 5): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Output/DiffOnlyOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
namespace SebastianBergmann\Diff\Output;

use function assert;
use function fclose;
use function fopen;
use function fwrite;
use function is_resource;
use function str_ends_with;
use function stream_get_contents;
use function substr;
Expand All @@ -33,6 +35,7 @@ public function __construct(string $header = "--- Original\n+++ New\n")
public function getDiff(array $diff): string
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));

if ('' !== $this->header) {
fwrite($buffer, $this->header);
Expand Down
4 changes: 4 additions & 0 deletions src/Output/StrictUnifiedDiffOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

use function array_merge;
use function array_splice;
use function assert;
use function count;
use function fclose;
use function fopen;
use function fwrite;
use function is_bool;
use function is_int;
use function is_resource;
use function is_string;
use function max;
use function min;
Expand Down Expand Up @@ -99,6 +101,8 @@ public function getDiff(array $diff): string
$this->changed = false;

$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));

fwrite($buffer, $this->header);

$this->writeDiffHunks($buffer, $diff);
Expand Down
3 changes: 3 additions & 0 deletions src/Output/UnifiedDiffOutputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
namespace SebastianBergmann\Diff\Output;

use function array_splice;
use function assert;
use function count;
use function fclose;
use function fopen;
use function fwrite;
use function is_resource;
use function max;
use function min;
use function str_ends_with;
Expand Down Expand Up @@ -45,6 +47,7 @@ public function __construct(string $header = "--- Original\n+++ New\n", bool $ad
public function getDiff(array $diff): string
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));

if ('' !== $this->header) {
fwrite($buffer, $this->header);
Expand Down
3 changes: 3 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function parse(string $string): array
return $diffs;
}

/**
* @param string[] $lines
*/
private function parseFileDiff(Diff $diff, array $lines): void
{
$chunks = [];
Expand Down
1 change: 0 additions & 1 deletion src/TimeEfficientLongestCommonSubsequenceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use function array_reverse;
use function count;
use function max;
use SplFixedArray;

final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
Expand Down
18 changes: 17 additions & 1 deletion tests/DifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ public static function arrayProvider(): array
];
}

/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* }
* >
*/
public static function textProvider(): array
{
return [
Expand Down Expand Up @@ -255,6 +264,14 @@ public static function textProvider(): array
];
}

/**
* @return array<
* array{
* 0: array<string>,
* 1: string,
* }
* >
*/
public static function provideSplitStringByLinesCases(): array
{
return [
Expand Down Expand Up @@ -368,7 +385,6 @@ public function testSplitStringByLines(array $expected, string $input): void
{
$reflection = new ReflectionObject($this->differ);
$method = $reflection->getMethod('splitStringByLines');
$method->setAccessible(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-op since PHP8.1


$this->assertSame($expected, $method->invoke($this->differ, $input));
}
Expand Down
18 changes: 17 additions & 1 deletion tests/Output/AbstractChunkOutputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
#[UsesClass(TimeEfficientLongestCommonSubsequenceCalculator::class)]
final class AbstractChunkOutputBuilderTest extends TestCase
{
/**
* @return array<
* array{
* 0: array<int, int>,
* 1: string,
* 2: string,
* 3?: int,
* }
* >
*/
public static function provideGetCommonChunks(): array
{
return [
Expand Down Expand Up @@ -122,6 +132,9 @@ public static function provideGetCommonChunks(): array
];
}

/**
* @param array<int, positive-int> $expected
*/
#[DataProvider('provideGetCommonChunks')]
public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void
{
Expand All @@ -132,7 +145,10 @@ public function getDiff(array $diff): string
return '';
}

public function getChunks(array $diff, $lineThreshold)
/**
* @return array<int, positive-int>
*/
public function getChunks(array $diff, int $lineThreshold): array
{
return $this->getCommonChunks($diff, $lineThreshold);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Output/DiffOnlyOutputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
#[UsesClass(TimeEfficientLongestCommonSubsequenceCalculator::class)]
final class DiffOnlyOutputBuilderTest extends TestCase
{
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3?: string,
* }
* >
*/
public static function textForNoNonDiffLinesProvider(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public static function provideBasicDiffGeneration(): array
return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration();
}

public static function provideFilePairs(): array
/**
* @return iterable<string, array{0: string, 1: string}>
*/
public static function provideFilePairs(): iterable
{
$cases = [];
$fromFile = __FILE__;
Expand All @@ -77,9 +80,10 @@ public static function provideFilePairs(): array
continue;
}

$toFile = $file->getPathname();
$cases[sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile))] = [$fromFile, $toFile];
$fromFile = $toFile;
$toFile = $file->getPathname();

yield sprintf("Diff file:\n\"%s\"\nvs.\n\"%s\"\n", realpath($fromFile), realpath($toFile)) => [$fromFile, $toFile];
$fromFile = $toFile;
}

return $cases;
Expand Down Expand Up @@ -119,7 +123,7 @@ public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, st
return;
}

$this->doIntegrationTestGitApply($diff, $from, $to);
$this->doIntegrationTestGitApply($diff, $from);
}

#[DataProvider('provideFilePairs')]
Expand All @@ -145,7 +149,7 @@ public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, strin
#[DataProvider('provideSample')]
public function testIntegrationOfUnitTestCasesGitApply(string $expected, string $from, string $to): void
{
$this->doIntegrationTestGitApply($expected, $from, $to);
$this->doIntegrationTestGitApply($expected, $from);
}

#[DataProvider('provideBasicDiffGeneration')]
Expand Down Expand Up @@ -191,7 +195,7 @@ public function testIntegrationDiffOutputBuilderVersusDiffCommand(string $diff,
$this->assertSame($diff, $output);
}

private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
private function doIntegrationTestGitApply(string $diff, string $from): void
{
$this->assertNotSame('', $diff);
$this->assertValidUnifiedDiffFormat($diff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
use function array_filter;
use function assert;
use function file_put_contents;
use function implode;
use function is_array;
use function is_string;
use function preg_replace;
use function preg_split;
Expand Down Expand Up @@ -43,6 +45,15 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
private string $fileFrom;
private string $filePatch;

/**
* @return array{
* string?: array{
* 0: string,
* 1: string,
* 2: string,
* },
* }
*/
public static function provideDiffWithLineNumbers(): array
{
return array_filter(
Expand Down Expand Up @@ -70,15 +81,15 @@ protected function tearDown(): void
}

#[DataProvider('provideDiffWithLineNumbers')]
public function testDiffWithLineNumbersPath($expected, $from, $to): void
public function testDiffWithLineNumbersPath(string $expected, string $from, string $to): void
{
$this->doIntegrationTestPatch($expected, $from, $to);
}

#[DataProvider('provideDiffWithLineNumbers')]
public function testDiffWithLineNumbersGitApply($expected, $from, $to): void
public function testDiffWithLineNumbersGitApply(string $expected, string $from): void
{
$this->doIntegrationTestGitApply($expected, $from, $to);
$this->doIntegrationTestGitApply($expected, $from);
}

private function doIntegrationTestPatch(string $diff, string $from, string $to): void
Expand Down Expand Up @@ -109,7 +120,7 @@ private function doIntegrationTestPatch(string $diff, string $from, string $to):
);
}

private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
private function doIntegrationTestGitApply(string $diff, string $from): void
{
$this->assertNotSame('', $diff);
$this->assertValidUnifiedDiffFormat($diff);
Expand Down Expand Up @@ -154,7 +165,9 @@ private function cleanUpTempFiles(): void

private static function setDiffFileHeader(string $diff, string $file): string
{
$diffLines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$diffLines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
assert(is_array($diffLines));

$diffLines[0] = preg_replace('#^\-\-\- .*#', '--- /' . $file, $diffLines[0], 1);
$diffLines[1] = preg_replace('#^\+\+\+ .*#', '+++ /' . $file, $diffLines[1], 1);

Expand Down
38 changes: 38 additions & 0 deletions tests/Output/StrictUnifiedDiffOutputBuilderDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@

final class StrictUnifiedDiffOutputBuilderDataProvider
{
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3: array{
* "fromFile": string,
* "toFile": string,
* "collapseRanges"?: bool,
* "fromFileDate"?: string,
* "toFileDate"?: string,
* },
* },
* >
*/
public static function provideOutputBuildingCases(): array
{
return [
Expand Down Expand Up @@ -72,6 +88,19 @@ public static function provideOutputBuildingCases(): array
];
}

/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3: array{
* "fromFile": string,
* "toFile": string,
* },
* },
* >
*/
public static function provideSample(): array
{
return [
Expand All @@ -97,6 +126,15 @@ public static function provideSample(): array
];
}

/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* }
* >
*/
public static function provideBasicDiffGeneration(): array
{
return [
Expand Down
Loading