From 4a11a96e20824a972ba96458677654e8bba6ee67 Mon Sep 17 00:00:00 2001 From: Marvin Courcier Date: Mon, 24 Apr 2023 22:15:44 +0200 Subject: [PATCH 1/7] fix readonly final class architecture side effect issue PHP8.2 --- src/Domain/Analyser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Domain/Analyser.php b/src/Domain/Analyser.php index 32e2123e..2dc14c0d 100644 --- a/src/Domain/Analyser.php +++ b/src/Domain/Analyser.php @@ -179,7 +179,7 @@ private function analyseFile(Collector $collector, string $filename): void \is_array($tokens[$i - 2])) { if ($tokens[$i - 2][0] === \T_ABSTRACT) { $collector->addAbstractClass($filename); - } elseif ($tokens[$i - 2][0] === \T_FINAL) { + } elseif ($tokens[$i - 2][0] === \T_FINAL || $tokens[$i - 4][0] === \T_FINAL) { $collector->addConcreteFinalClass($filename); } else { $collector->addConcreteNonFinalClass($filename); From 7f3c15947eea2ab137a0fa8ed978322dc30ad1dc Mon Sep 17 00:00:00 2001 From: Marvin Courcier Date: Mon, 24 Apr 2023 22:20:50 +0200 Subject: [PATCH 2/7] add readonly also for abstract --- src/Domain/Analyser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Domain/Analyser.php b/src/Domain/Analyser.php index 2dc14c0d..9b5a38f5 100644 --- a/src/Domain/Analyser.php +++ b/src/Domain/Analyser.php @@ -177,9 +177,9 @@ private function analyseFile(Collector $collector, string $filename): void $collector->incrementInterfaces(); } elseif (isset($tokens[$i - 2]) && \is_array($tokens[$i - 2])) { - if ($tokens[$i - 2][0] === \T_ABSTRACT) { + if ($tokens[$i - 2][0] === \T_ABSTRACT || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_ABSTRACT) { $collector->addAbstractClass($filename); - } elseif ($tokens[$i - 2][0] === \T_FINAL || $tokens[$i - 4][0] === \T_FINAL) { + } elseif ($tokens[$i - 2][0] === \T_FINAL || $tokens[$i - 2][0] === \T_READONLY && $tokens[$i - 4][0] === \T_FINAL) { $collector->addConcreteFinalClass($filename); } else { $collector->addConcreteNonFinalClass($filename); From 901f96be5aa1d17e898ab5609535c396d6965174 Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Tue, 26 Sep 2023 23:55:10 -0400 Subject: [PATCH 3/7] add tests --- .../Insights/Fixtures/ReadonlyClass.php | 14 ++++ tests/Domain/Insights/ReadonlyClassTest.php | 64 +++++++++++++++++++ tests/Domain/Insights/SyntaxCheckTest.php | 2 + 3 files changed, 80 insertions(+) create mode 100644 tests/Domain/Insights/Fixtures/ReadonlyClass.php create mode 100644 tests/Domain/Insights/ReadonlyClassTest.php diff --git a/tests/Domain/Insights/Fixtures/ReadonlyClass.php b/tests/Domain/Insights/Fixtures/ReadonlyClass.php new file mode 100644 index 00000000..bad034c1 --- /dev/null +++ b/tests/Domain/Insights/Fixtures/ReadonlyClass.php @@ -0,0 +1,14 @@ +analyse([__DIR__ . '/Fixtures/'], $files, PathShortener::extractCommonPath($files)); + $insight = new ForbiddenNormalClasses($collector, []); + + self::assertTrue($insight->hasIssue()); + self::assertIsArray($insight->getDetails()); + self::assertNotEmpty($insight->getDetails()); + } + + public function testSkipFile(): void + { + $fileLocation = __DIR__ . '/Fixtures/ReadonlyClass.php'; + + $collection = $this->runAnalyserOnConfig( + [ + 'add' => [ + Classes::class => [ + ForbiddenNormalClasses::class, + ], + ], + 'config' => [ + ForbiddenNormalClasses::class => [ + 'exclude' => [$fileLocation], + ], + ], + ], + [$fileLocation] + ); + + $classErrors = 0; + + foreach ($collection->allFrom(new Classes()) as $insight) { + if ($insight->hasIssue() && $insight->getInsightClass() === ForbiddenNormalClasses::class) { + $classErrors++; + } + } + + self::assertEquals(0, $classErrors); + } +} diff --git a/tests/Domain/Insights/SyntaxCheckTest.php b/tests/Domain/Insights/SyntaxCheckTest.php index 44496330..996a9ffe 100644 --- a/tests/Domain/Insights/SyntaxCheckTest.php +++ b/tests/Domain/Insights/SyntaxCheckTest.php @@ -51,6 +51,8 @@ public function testHasIssueOnDirectory(): void self::assertTrue($insight->hasIssue()); if (PHP_MAJOR_VERSION === 7) { self::assertCount(2, $details); + } elseif (PHP_VERSION_ID < 80200) { + self::assertCount(4, $details); } else { self::assertCount(3, $details); } From cd524fc86e775017f30438369b0c11ed1e50901b Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Wed, 27 Sep 2023 00:14:21 -0400 Subject: [PATCH 4/7] bump min codesniffer for T_READONLY --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 034bb647..02a57389 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "psr/simple-cache": "^1.0|^2.0|^3.0", "sebastian/diff": "^4.0|^5.0", "slevomat/coding-standard": "^7.0.8|^8.0", - "squizlabs/php_codesniffer": "^3.5", + "squizlabs/php_codesniffer": "^3.7", "symfony/cache": "^4.4|^5.0|^6.0", "symfony/console": "^4.2.12|^5.0|^6.0", "symfony/finder": "^4.2.12|^5.0|^6.0", From aff7cb6472f0f3b5963309b5ef4cf67f1cdf7388 Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Wed, 27 Sep 2023 00:25:59 -0400 Subject: [PATCH 5/7] tests v2 --- tests/Domain/Insights/SyntaxCheckTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Domain/Insights/SyntaxCheckTest.php b/tests/Domain/Insights/SyntaxCheckTest.php index 996a9ffe..d9c1561b 100644 --- a/tests/Domain/Insights/SyntaxCheckTest.php +++ b/tests/Domain/Insights/SyntaxCheckTest.php @@ -50,11 +50,9 @@ public function testHasIssueOnDirectory(): void self::assertTrue($insight->hasIssue()); if (PHP_MAJOR_VERSION === 7) { - self::assertCount(2, $details); - } elseif (PHP_VERSION_ID < 80200) { - self::assertCount(4, $details); - } else { self::assertCount(3, $details); + } else { + self::assertCount(4, $details); } /** @var \NunoMaduro\PhpInsights\Domain\Details $detail */ From 85b1898f32b208a1d2a320eb0390efd98b9416ab Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Wed, 27 Sep 2023 22:18:49 -0400 Subject: [PATCH 6/7] test again --- tests/Domain/Insights/SyntaxCheckTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Domain/Insights/SyntaxCheckTest.php b/tests/Domain/Insights/SyntaxCheckTest.php index d9c1561b..abd02a16 100644 --- a/tests/Domain/Insights/SyntaxCheckTest.php +++ b/tests/Domain/Insights/SyntaxCheckTest.php @@ -49,7 +49,7 @@ public function testHasIssueOnDirectory(): void usort($details, static fn (Details $a, Details $b): int => $a->getFile() <=> $b->getFile()); self::assertTrue($insight->hasIssue()); - if (PHP_MAJOR_VERSION === 7) { + if (PHP_MAJOR_VERSION === 7 || PHP_VERSION_ID >= 80200) { self::assertCount(3, $details); } else { self::assertCount(4, $details); From 0ff714ed407fa45a488869ebfc1469f2da9b4ede Mon Sep 17 00:00:00 2001 From: Chris Gmyr Date: Wed, 27 Sep 2023 22:22:16 -0400 Subject: [PATCH 7/7] update phpunit config --- .gitignore | 3 ++- phpunit.xml | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 8c612a2a..0a4259fd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ .idea/* .env .phpunit.result.cache +.phpunit.cache/ /composer.lock -.php_cs.cache \ No newline at end of file +.php_cs.cache diff --git a/phpunit.xml b/phpunit.xml index 1600bcd4..6c7af36e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,13 +1,14 @@ - - - - ./src - - + + ./tests + + + ./src + +