Skip to content
Snippets Groups Projects
Commit da621b83 authored by SpacePossum's avatar SpacePossum Committed by Sebastian Bergmann
Browse files

Fix some phpstan findings

parent b4ccd857
No related branches found
No related tags found
No related merge requests found
Showing
with 240 additions and 36 deletions
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
\ No newline at end of file
......@@ -84,11 +84,11 @@ final class Differ
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];
}
......
......@@ -15,7 +15,6 @@ use function array_reverse;
use function array_slice;
use function count;
use function in_array;
use function max;
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
{
......
......@@ -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
{
......
......@@ -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;
......@@ -33,6 +35,7 @@ final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface
public function getDiff(array $diff): string
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
if ('' !== $this->header) {
fwrite($buffer, $this->header);
......
......@@ -11,12 +11,14 @@ namespace SebastianBergmann\Diff\Output;
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;
......@@ -99,6 +101,8 @@ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface
$this->changed = false;
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
fwrite($buffer, $this->header);
$this->writeDiffHunks($buffer, $diff);
......
......@@ -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;
......@@ -45,6 +47,7 @@ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder
public function getDiff(array $diff): string
{
$buffer = fopen('php://memory', 'r+b');
assert(is_resource($buffer));
if ('' !== $this->header) {
fwrite($buffer, $this->header);
......
......@@ -71,6 +71,9 @@ final class Parser
return $diffs;
}
/**
* @param string[] $lines
*/
private function parseFileDiff(Diff $diff, array $lines): void
{
$chunks = [];
......
......@@ -11,7 +11,6 @@ namespace SebastianBergmann\Diff;
use function array_reverse;
use function count;
use function max;
use SplFixedArray;
final class TimeEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator
......
......@@ -189,6 +189,15 @@ final class DifferTest extends TestCase
];
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* }
* >
*/
public static function textProvider(): array
{
return [
......@@ -255,6 +264,14 @@ EOF
];
}
/**
* @return array<
* array{
* 0: array<string>,
* 1: string,
* }
* >
*/
public static function provideSplitStringByLinesCases(): array
{
return [
......@@ -368,7 +385,6 @@ EOF
{
$reflection = new ReflectionObject($this->differ);
$method = $reflection->getMethod('splitStringByLines');
$method->setAccessible(true);
$this->assertSame($expected, $method->invoke($this->differ, $input));
}
......
......@@ -22,6 +22,16 @@ use SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator;
#[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 [
......@@ -122,6 +132,9 @@ final class AbstractChunkOutputBuilderTest extends TestCase
];
}
/**
* @param array<int, positive-int> $expected
*/
#[DataProvider('provideGetCommonChunks')]
public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void
{
......@@ -132,7 +145,10 @@ final class AbstractChunkOutputBuilderTest extends TestCase
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);
}
......
......@@ -21,6 +21,16 @@ use SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator;
#[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 [
......
......@@ -63,7 +63,10 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration();
}
public static function provideFilePairs(): array
/**
* @return iterable<string, array{0: string, 1: string}>
*/
public static function provideFilePairs(): iterable
{
$cases = [];
$fromFile = __FILE__;
......@@ -77,9 +80,10 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
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;
......@@ -119,7 +123,7 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
return;
}
$this->doIntegrationTestGitApply($diff, $from, $to);
$this->doIntegrationTestGitApply($diff, $from);
}
#[DataProvider('provideFilePairs')]
......@@ -145,7 +149,7 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
#[DataProvider('provideSample')]
public function testIntegrationOfUnitTestCasesGitApply(string $expected, string $from, string $to): void
{
$this->doIntegrationTestGitApply($expected, $from, $to);
$this->doIntegrationTestGitApply($expected, $from);
}
#[DataProvider('provideBasicDiffGeneration')]
......@@ -191,7 +195,7 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
$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);
......
......@@ -13,8 +13,10 @@ use const ARRAY_FILTER_USE_KEY;
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;
......@@ -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(
......@@ -70,15 +81,15 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
}
#[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
......@@ -109,7 +120,7 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
);
}
private function doIntegrationTestGitApply(string $diff, string $from, string $to): void
private function doIntegrationTestGitApply(string $diff, string $from): void
{
$this->assertNotSame('', $diff);
$this->assertValidUnifiedDiffFormat($diff);
......@@ -154,7 +165,9 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase
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);
......
......@@ -11,6 +11,22 @@ namespace SebastianBergmann\Diff\Output;
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 [
......@@ -72,6 +88,19 @@ final class StrictUnifiedDiffOutputBuilderDataProvider
];
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3: array{
* "fromFile": string,
* "toFile": string,
* },
* },
* >
*/
public static function provideSample(): array
{
return [
......@@ -97,6 +126,15 @@ final class StrictUnifiedDiffOutputBuilderDataProvider
];
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* }
* >
*/
public static function provideBasicDiffGeneration(): array
{
return [
......
......@@ -47,6 +47,19 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase
return StrictUnifiedDiffOutputBuilderDataProvider::provideBasicDiffGeneration();
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3?: array{
* commonLineThreshold?: int,
* contextLines?: int,
* },
* }
* >
*/
public static function provideConfiguredDiffGeneration(): array
{
return [
......@@ -189,57 +202,75 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase
];
}
/**
* @return array<
* array{
* 0: string,
* 1: array<mixed>,
* }
* >
*/
public static function provideInvalidConfiguration(): array
{
$time = time();
return [
[
['collapseRanges' => 1],
'Option "collapseRanges" must be a bool, got "integer#1".',
['collapseRanges' => 1],
],
[
['contextLines' => 'a'],
'Option "contextLines" must be an int >= 0, got "string#a".',
['contextLines' => 'a'],
],
[
['commonLineThreshold' => -2],
'Option "commonLineThreshold" must be an int > 0, got "integer#-2".',
['commonLineThreshold' => -2],
],
[
['commonLineThreshold' => 0],
'Option "commonLineThreshold" must be an int > 0, got "integer#0".',
['commonLineThreshold' => 0],
],
[
['fromFile' => new SplFileInfo(__FILE__)],
'Option "fromFile" must be a string, got "SplFileInfo".',
['fromFile' => new SplFileInfo(__FILE__)],
],
[
['fromFile' => null],
'Option "fromFile" must be a string, got "<null>".',
['fromFile' => null],
],
[
'Option "toFile" must be a string, got "integer#1".',
[
'fromFile' => __FILE__,
'toFile' => 1,
],
'Option "toFile" must be a string, got "integer#1".',
],
[
'Option "toFileDate" must be a string or <null>, got "integer#' . $time . '".',
[
'fromFile' => __FILE__,
'toFile' => __FILE__,
'toFileDate' => $time,
],
'Option "toFileDate" must be a string or <null>, got "integer#' . $time . '".',
],
[
[],
'Option "fromFile" must be a string, got "<null>".',
[],
],
];
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3: int,
* }
* >
*/
public static function provideCommonLineThresholdCases(): array
{
return [
......@@ -277,6 +308,17 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase
];
}
/**
* @return array<
* array{
* 0: string,
* 1: string,
* 2: string,
* 3: int,
* 4?: int
* }
* >
*/
public static function provideContextLineConfigurationCases(): array
{
$from = "A\nB\nC\nD\nE\nF\nX\nG\nH\nI\nJ\nK\nL\nM\n";
......@@ -534,6 +576,7 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase
$this->assertSame($expected, $diff);
}
/** @param array<mixed> $config */
#[DataProvider('provideConfiguredDiffGeneration')]
public function testConfiguredDiffGeneration(string $expected, string $from, string $to, array $config = []): void
{
......@@ -585,13 +628,14 @@ final class StrictUnifiedDiffOutputBuilderTest extends TestCase
);
}
/** @param array<mixed> $invalidConfig */
#[DataProvider('provideInvalidConfiguration')]
public function testInvalidConfiguration(array $options, string $message): void
public function testInvalidConfiguration(string $expectedMessage, array $invalidConfig): void
{
$this->expectException(ConfigurationException::class);
$this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($message, '#')));
$this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($expectedMessage, '#')));
new StrictUnifiedDiffOutputBuilder($options);
new StrictUnifiedDiffOutputBuilder($invalidConfig);
}
#[DataProvider('provideCommonLineThresholdCases')]
......
......@@ -11,6 +11,15 @@ namespace SebastianBergmann\Diff\Output;
final class UnifiedDiffOutputBuilderDataProvider
{
/**
* @return array{
* string?: array{
* 0: string,
* 1: string,
* 2: string,
* },
* }
*/
public static function provideDiffWithLineNumbers(): array
{
return [
......
......@@ -22,6 +22,11 @@ use SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator;
#[UsesClass(TimeEfficientLongestCommonSubsequenceCalculator::class)]
final class UnifiedDiffOutputBuilderTest extends TestCase
{
/**
* @return array<
* string[],
* >
*/
public static function headerProvider(): array
{
return [
......@@ -52,11 +57,25 @@ final class UnifiedDiffOutputBuilderTest extends TestCase
];
}
/**
* @return array{
* string?: array{
* 0: string,
* 1: string,
* 2: string,
* },
* }
*/
public static function provideDiffWithLineNumbers(): array
{
return UnifiedDiffOutputBuilderDataProvider::provideDiffWithLineNumbers();
}
/**
* @return array<
* array{0: string, 1: string}
* >
*/
public static function provideStringsThatAreTheSame(): array
{
return [
......
......@@ -9,6 +9,8 @@
*/
namespace SebastianBergmann\Diff;
use function assert;
use function is_array;
use function unserialize;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
......@@ -26,12 +28,24 @@ final class ParserTest extends TestCase
{
private Parser $parser;
/**
* @return array<
* array{
* 0: string,
* 1: array{0: Diff},
* },
* >
*/
public static function diffProvider(): array
{
$diff = unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin'));
assert(is_array($diff));
assert($diff[0] instanceof Diff);
return [
[
"--- old.txt 2014-11-04 08:51:02.661868729 +0300\n+++ new.txt 2014-11-04 08:51:02.665868730 +0300\n@@ -1,3 +1,4 @@\n+2222111\n 1111111\n 1111111\n 1111111\n@@ -5,10 +6,8 @@\n 1111111\n 1111111\n 1111111\n +1121211\n 1111111\n -1111111\n -1111111\n -2222222\n 2222222\n 2222222\n 2222222\n@@ -17,5 +16,6 @@\n 2222222\n 2222222\n 2222222\n +2122212\n 2222222\n 2222222\n",
unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')),
$diff,
],
];
}
......@@ -100,7 +114,7 @@ PATCH;
$this->assertEquals('b/Foo Bar.txt', $diffs[0]->to());
}
public function testParseWithSpacesInFileNamesAndTimesamp(): void
public function testParseWithSpacesInFileNamesAndTimestamp(): void
{
$content = <<<'PATCH'
diff --git a/Foo Bar.txt b/Foo Bar.txt
......@@ -159,7 +173,7 @@ END;
$this->assertSame(Line::REMOVED, $line->type());
}
public function testParseDiffForMulitpleFiles(): void
public function testParseDiffForMultipleFiles(): void
{
$content = <<<'END'
diff --git a/Test.txt b/Test.txt
......
......@@ -11,7 +11,9 @@ namespace SebastianBergmann\Diff\Utils;
use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;
use function assert;
use function count;
use function is_array;
use function preg_match;
use function preg_split;
use function sprintf;
......@@ -41,7 +43,9 @@ trait UnifiedDiffAssertTrait
throw new UnexpectedValueException(sprintf('Expected diff to end with a line break, got "%s".', $last));
}
$lines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$lines = preg_split('/(.*\R)/', $diff, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
assert(is_array($lines));
$lineCount = count($lines);
$lineNumber = $diffLineFromNumber = $diffLineToNumber = 1;
$fromStart = $fromTillOffset = $toStart = $toTillOffset = -1;
......@@ -242,7 +246,7 @@ trait UnifiedDiffAssertTrait
$matches,
);
if (1 !== $match || ($matchesCount = count($matches)) < 4) {
if (1 !== $match || count($matches) < 4) {
throw new UnexpectedValueException(sprintf('Date of header line does not match expected pattern, got "%s". %s', $date, $message));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment