From 6fb7a31dc5eb83a7d67f9119aafc36e248d357e2 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sun, 6 Mar 2022 18:06:34 +0100 Subject: [PATCH] Cleanup --- src/Chunk.php | 29 ++----- src/Diff.php | 20 ++--- src/Differ.php | 85 ++----------------- src/Line.php | 12 +-- src/Output/DiffOnlyOutputBuilder.php | 5 +- src/Output/StrictUnifiedDiffOutputBuilder.php | 25 ++---- src/Output/UnifiedDiffOutputBuilder.php | 22 ++--- tests/ChunkTest.php | 5 +- tests/DifferTest.php | 50 +---------- tests/LineTest.php | 5 +- tests/LongestCommonSubsequenceTestCase.php | 38 +++------ .../Output/AbstractChunkOutputBuilderTest.php | 2 +- ...nifiedDiffOutputBuilderIntegrationTest.php | 8 +- ...nifiedDiffOutputBuilderIntegrationTest.php | 6 +- tests/ParserTest.php | 5 +- .../UnifiedDiffAssertTraitIntegrationTest.php | 2 +- 16 files changed, 65 insertions(+), 254 deletions(-) diff --git a/src/Chunk.php b/src/Chunk.php index 16ae34f4..240b8dd5 100644 --- a/src/Chunk.php +++ b/src/Chunk.php @@ -11,30 +11,15 @@ final class Chunk { - /** - * @var int - */ - private $start; + private int $start; - /** - * @var int - */ - private $startRange; + private int $startRange; - /** - * @var int - */ - private $end; + private int $end; - /** - * @var int - */ - private $endRange; + private int $endRange; - /** - * @var Line[] - */ - private $lines; + private array $lines; public function __construct(int $start = 0, int $startRange = 1, int $end = 0, int $endRange = 1, array $lines = []) { @@ -66,7 +51,7 @@ public function getEndRange(): int } /** - * @return Line[] + * @psalm-return list */ public function getLines(): array { @@ -74,7 +59,7 @@ public function getLines(): array } /** - * @param Line[] $lines + * @psalm-param list $lines */ public function setLines(array $lines): void { diff --git a/src/Diff.php b/src/Diff.php index 17b2084f..e4147baf 100644 --- a/src/Diff.php +++ b/src/Diff.php @@ -11,23 +11,17 @@ final class Diff { - /** - * @var string - */ - private $from; + private string $from; - /** - * @var string - */ - private $to; + private string $to; /** - * @var Chunk[] + * @psalm-var list */ - private $chunks; + private array $chunks; /** - * @param Chunk[] $chunks + * @psalm-param list $chunks */ public function __construct(string $from, string $to, array $chunks = []) { @@ -47,7 +41,7 @@ public function getTo(): string } /** - * @return Chunk[] + * @psalm-return list */ public function getChunks(): array { @@ -55,7 +49,7 @@ public function getChunks(): array } /** - * @param Chunk[] $chunks + * @psalm-param list $chunks */ public function setChunks(array $chunks): void { diff --git a/src/Differ.php b/src/Differ.php index 3b8946e2..91dc2a5c 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -18,17 +18,13 @@ use function count; use function current; use function end; -use function get_class; -use function gettype; use function is_array; -use function is_object; use function is_string; use function key; use function min; use function preg_split; use function prev; use function reset; -use function sprintf; use function substr; use SebastianBergmann\Diff\Output\DiffOutputBuilderInterface; use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; @@ -45,44 +41,14 @@ final class Differ public const NO_LINE_END_EOF_WARNING = 4; - /** - * @var DiffOutputBuilderInterface - */ - private $outputBuilder; - - /** - * @param DiffOutputBuilderInterface $outputBuilder - * - * @throws InvalidArgumentException - */ - public function __construct($outputBuilder = null) + private DiffOutputBuilderInterface|UnifiedDiffOutputBuilder $outputBuilder; + + public function __construct(DiffOutputBuilderInterface $outputBuilder) { - if ($outputBuilder instanceof DiffOutputBuilderInterface) { - $this->outputBuilder = $outputBuilder; - } elseif (null === $outputBuilder) { - $this->outputBuilder = new UnifiedDiffOutputBuilder; - } elseif (is_string($outputBuilder)) { - // PHPUnit 6.1.4, 6.2.0, 6.2.1, 6.2.2, and 6.2.3 support - // @see https://github.com/sebastianbergmann/phpunit/issues/2734#issuecomment-314514056 - // @deprecated - $this->outputBuilder = new UnifiedDiffOutputBuilder($outputBuilder); - } else { - throw new InvalidArgumentException( - sprintf( - 'Expected builder to be an instance of DiffOutputBuilderInterface, or a string, got %s.', - is_object($outputBuilder) ? 'instance of "' . get_class($outputBuilder) . '"' : gettype($outputBuilder) . ' "' . $outputBuilder . '"' - ) - ); - } + $this->outputBuilder = $outputBuilder; } - /** - * Returns the diff between two arrays or strings as string. - * - * @param array|string $from - * @param array|string $to - */ - public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null): string + public function diff(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): string { $diff = $this->diffToArray( $this->normalizeDiffInput($from), @@ -93,33 +59,14 @@ public function diff($from, $to, LongestCommonSubsequenceCalculator $lcs = null) return $this->outputBuilder->getDiff($diff); } - /** - * Returns the diff between two arrays or strings as array. - * - * Each array element contains two elements: - * - [0] => mixed $token - * - [1] => 2|1|0 - * - * - 2: REMOVED: $token was removed from $from - * - 1: ADDED: $token was added to $from - * - 0: OLD: $token is not changed in $to - * - * @param array|string $from - * @param array|string $to - * @param LongestCommonSubsequenceCalculator $lcs - */ - public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs = null): array + public function diffToArray(array|string $from, array|string $to, LongestCommonSubsequenceCalculator $lcs = null): array { if (is_string($from)) { $from = $this->splitStringByLines($from); - } elseif (!is_array($from)) { - throw new InvalidArgumentException('"from" must be an array or string.'); } if (is_string($to)) { $to = $this->splitStringByLines($to); - } elseif (!is_array($to)) { - throw new InvalidArgumentException('"to" must be an array or string.'); } [$from, $to, $start, $end] = self::getArrayDiffParted($from, $to); @@ -172,12 +119,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs return $diff; } - /** - * Casts variable to string if it is not a string or array. - * - * @return array|string - */ - private function normalizeDiffInput($input) + private function normalizeDiffInput(array|string $input) { if (!is_array($input) && !is_string($input)) { return (string) $input; @@ -186,9 +128,6 @@ private function normalizeDiffInput($input) return $input; } - /** - * Checks if input is string, if so it will split it line-by-line. - */ private function splitStringByLines(string $input): array { 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 return new TimeEfficientLongestCommonSubsequenceCalculator; } - /** - * Calculates the estimated memory footprint for the DP-based method. - * - * @return float|int - */ - private function calculateEstimatedFootprint(array $from, array $to) + private function calculateEstimatedFootprint(array $from, array $to): float|int { $itemSize = PHP_INT_SIZE === 4 ? 76 : 144; return $itemSize * min(count($from), count($to)) ** 2; } - /** - * Returns true if line ends don't match in a diff. - */ private function detectUnmatchedLineEndings(array $diff): bool { $newLineBreaks = ['' => true]; diff --git a/src/Line.php b/src/Line.php index 7e2dc7da..a3aceb06 100644 --- a/src/Line.php +++ b/src/Line.php @@ -17,15 +17,9 @@ final class Line public const UNCHANGED = 3; - /** - * @var int - */ - private $type; - - /** - * @var string - */ - private $content; + private int $type; + + private string $content; public function __construct(int $type = self::UNCHANGED, string $content = '') { diff --git a/src/Output/DiffOnlyOutputBuilder.php b/src/Output/DiffOnlyOutputBuilder.php index f79a935c..5efc9ebd 100644 --- a/src/Output/DiffOnlyOutputBuilder.php +++ b/src/Output/DiffOnlyOutputBuilder.php @@ -22,10 +22,7 @@ */ final class DiffOnlyOutputBuilder implements DiffOutputBuilderInterface { - /** - * @var string - */ - private $header; + private string $header; public function __construct(string $header = "--- Original\n+++ New\n") { diff --git a/src/Output/StrictUnifiedDiffOutputBuilder.php b/src/Output/StrictUnifiedDiffOutputBuilder.php index 3deadb12..a9488bde 100644 --- a/src/Output/StrictUnifiedDiffOutputBuilder.php +++ b/src/Output/StrictUnifiedDiffOutputBuilder.php @@ -33,7 +33,7 @@ */ final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface { - private static $default = [ + private static array $default = [ 'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1` 'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed) '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 'toFileDate' => null, ]; - /** - * @var bool - */ - private $changed; + private bool $changed; - /** - * @var bool - */ - private $collapseRanges; + private bool $collapseRanges; /** - * @var int >= 0 + * @psalm-var positive-int */ - private $commonLineThreshold; + private int $commonLineThreshold; - /** - * @var string - */ - private $header; + private string $header; /** - * @var int >= 0 + * @psalm-var positive-int */ - private $contextLines; + private int $contextLines; public function __construct(array $options = []) { diff --git a/src/Output/UnifiedDiffOutputBuilder.php b/src/Output/UnifiedDiffOutputBuilder.php index 2026165e..ed8fb6e2 100644 --- a/src/Output/UnifiedDiffOutputBuilder.php +++ b/src/Output/UnifiedDiffOutputBuilder.php @@ -26,30 +26,18 @@ */ final class UnifiedDiffOutputBuilder extends AbstractChunkOutputBuilder { - /** - * @var bool - */ - private $collapseRanges = true; + private bool $collapseRanges = true; - /** - * @var int >= 0 - */ - private $commonLineThreshold = 6; + private int $commonLineThreshold = 6; /** - * @var int >= 0 + * @psalm-var positive-int */ private $contextLines = 3; - /** - * @var string - */ - private $header; + private string $header; - /** - * @var bool - */ - private $addLineNumbers; + private bool $addLineNumbers; public function __construct(string $header = "--- Original\n+++ New\n", bool $addLineNumbers = false) { diff --git a/tests/ChunkTest.php b/tests/ChunkTest.php index 453603bc..d3164d8b 100644 --- a/tests/ChunkTest.php +++ b/tests/ChunkTest.php @@ -18,10 +18,7 @@ */ final class ChunkTest extends TestCase { - /** - * @var Chunk - */ - private $chunk; + private Chunk $chunk; protected function setUp(): void { diff --git a/tests/DifferTest.php b/tests/DifferTest.php index 23251039..8d86b6cb 100644 --- a/tests/DifferTest.php +++ b/tests/DifferTest.php @@ -11,8 +11,7 @@ use PHPUnit\Framework\TestCase; use ReflectionObject; -use SplFileInfo; -use stdClass; +use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; /** * @covers \SebastianBergmann\Diff\Differ @@ -24,14 +23,11 @@ */ final class DifferTest extends TestCase { - /** - * @var Differ - */ - private $differ; + private Differ $differ; protected function setUp(): void { - $this->differ = new Differ; + $this->differ = new Differ(new UnifiedDiffOutputBuilder); } /** @@ -72,14 +68,6 @@ public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcs $this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator)); } - public function testTypesOtherThanArrayAndStringCanBePassed(): void - { - $this->assertSame( - "--- Original\n+++ New\n@@ @@\n-1\n+2\n", - $this->differ->diff(1, 2) - ); - } - public function testArrayDiffs(): void { $this->assertSame( @@ -320,22 +308,6 @@ public function textProvider(): array ]; } - public function testDiffToArrayInvalidFromType(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('#^"from" must be an array or string\.$#'); - - $this->differ->diffToArray(null, ''); - } - - public function testDiffInvalidToType(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('#^"to" must be an array or string\.$#'); - - $this->differ->diffToArray('', new stdClass); - } - /** * @dataProvider provideSplitStringByLinesCases */ @@ -413,20 +385,4 @@ public function provideSplitStringByLinesCases(): array ], ]; } - - public function testConstructorInvalidArgInt(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/^Expected builder to be an instance of DiffOutputBuilderInterface, or a string, got integer "1"\.$/'); - - new Differ(1); - } - - public function testConstructorInvalidArgObject(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/^Expected builder to be an instance of DiffOutputBuilderInterface, or a string, got instance of "SplFileInfo"\.$/'); - - new Differ(new SplFileInfo(__FILE__)); - } } diff --git a/tests/LineTest.php b/tests/LineTest.php index 20a398ca..62eb0715 100644 --- a/tests/LineTest.php +++ b/tests/LineTest.php @@ -16,10 +16,7 @@ */ final class LineTest extends TestCase { - /** - * @var Line - */ - private $line; + private Line $line; protected function setUp(): void { diff --git a/tests/LongestCommonSubsequenceTestCase.php b/tests/LongestCommonSubsequenceTestCase.php index d173688e..27d2054e 100644 --- a/tests/LongestCommonSubsequenceTestCase.php +++ b/tests/LongestCommonSubsequenceTestCase.php @@ -11,41 +11,25 @@ use function array_reverse; use function array_slice; -use function ini_get; -use function ini_set; use function range; use PHPUnit\Framework\TestCase; abstract class LongestCommonSubsequenceTestCase extends TestCase { - /** - * @var LongestCommonSubsequenceCalculator - */ - private $implementation; - - /** - * @var string - */ - private $memoryLimit; + private LongestCommonSubsequenceCalculator $implementation; /** - * @var int[] + * @psalm-var list */ - private $stress_sizes = [1, 2, 3, 100, 500, 1000, 2000]; + private array $stressSizes = [1, 2, 3, 100, 500, 1000, 2000]; protected function setUp(): void { - $this->memoryLimit = ini_get('memory_limit'); - ini_set('memory_limit', '-1'); + $this->iniSet('memory_limit', '-1'); $this->implementation = $this->createImplementation(); } - protected function tearDown(): void - { - ini_set('memory_limit', $this->memoryLimit); - } - public function testBothEmpty(): void { $from = []; @@ -83,7 +67,7 @@ public function testIsStrictComparison(): void public function testEqualSequences(): void { - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $range = range(1, $size); $from = $range; $to = $range; @@ -105,7 +89,7 @@ public function testDistinctSequences(): void $common = $this->implementation->calculate($from, $to); $this->assertSame([], $common); - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = range(1, $size); $to = range($size + 1, $size * 2); $common = $this->implementation->calculate($from, $to); @@ -127,7 +111,7 @@ public function testCommonSubsequence(): void $common = $this->implementation->calculate($from, $to); $this->assertSame($expected, $common); - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = $size < 2 ? [1] : range(1, $size + 1, 2); $to = $size < 3 ? [1] : range(1, $size + 1, 3); $expected = $size < 6 ? [1] : range(1, $size + 1, 6); @@ -139,7 +123,7 @@ public function testCommonSubsequence(): void public function testSingleElementSubsequenceAtStart(): void { - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = range(1, $size); $to = array_slice($from, 0, 1); $common = $this->implementation->calculate($from, $to); @@ -150,7 +134,7 @@ public function testSingleElementSubsequenceAtStart(): void public function testSingleElementSubsequenceAtMiddle(): void { - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = range(1, $size); $to = array_slice($from, (int) ($size / 2), 1); $common = $this->implementation->calculate($from, $to); @@ -161,7 +145,7 @@ public function testSingleElementSubsequenceAtMiddle(): void public function testSingleElementSubsequenceAtEnd(): void { - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = range(1, $size); $to = array_slice($from, $size - 1, 1); $common = $this->implementation->calculate($from, $to); @@ -178,7 +162,7 @@ public function testReversedSequences(): void $common = $this->implementation->calculate($from, $to); $this->assertSame($expected, $common); - foreach ($this->stress_sizes as $size) { + foreach ($this->stressSizes as $size) { $from = range(1, $size); $to = array_reverse($from); $common = $this->implementation->calculate($from, $to); diff --git a/tests/Output/AbstractChunkOutputBuilderTest.php b/tests/Output/AbstractChunkOutputBuilderTest.php index d64662a8..336c6b7b 100644 --- a/tests/Output/AbstractChunkOutputBuilderTest.php +++ b/tests/Output/AbstractChunkOutputBuilderTest.php @@ -40,7 +40,7 @@ public function getChunks(array $diff, $lineThreshold) $this->assertSame( $expected, - $output->getChunks((new Differ)->diffToArray($from, $to), $lineThreshold) + $output->getChunks((new Differ(new UnifiedDiffOutputBuilder))->diffToArray($from, $to), $lineThreshold) ); } diff --git a/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php b/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php index 27599fcc..074f771a 100644 --- a/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php +++ b/tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php @@ -42,13 +42,13 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase { use UnifiedDiffAssertTrait; - private $dir; + private string $dir; - private $fileFrom; + private string $fileFrom; - private $fileTo; + private string $fileTo; - private $filePatch; + private string $filePatch; protected function setUp(): void { diff --git a/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php b/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php index 6a4da760..262adeae 100644 --- a/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php +++ b/tests/Output/Integration/UnifiedDiffOutputBuilderIntegrationTest.php @@ -38,11 +38,11 @@ final class UnifiedDiffOutputBuilderIntegrationTest extends TestCase { use UnifiedDiffAssertTrait; - private $dir; + private string $dir; - private $fileFrom; + private string $fileFrom; - private $filePatch; + private string $filePatch; protected function setUp(): void { diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 73be2af9..03b7ce4f 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -22,10 +22,7 @@ */ final class ParserTest extends TestCase { - /** - * @var Parser - */ - private $parser; + private Parser $parser; protected function setUp(): void { diff --git a/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php b/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php index f7461567..709bd717 100644 --- a/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php +++ b/tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php @@ -30,7 +30,7 @@ final class UnifiedDiffAssertTraitIntegrationTest extends TestCase { use UnifiedDiffAssertTrait; - private $filePatch; + private string $filePatch; protected function setUp(): void {