From 9956a257ad99d2edffc1fa908d3d3e5461f522e6 Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:34:02 +0200 Subject: [PATCH 1/4] Add test case for current strictPSR scenario --- tests/ClassMapGeneratorTest.php | 12 ++++++++++++ .../psrViolations/ClassWithIncorrectSubNamespace.php | 5 +++++ 2 files changed, 17 insertions(+) create mode 100644 tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php diff --git a/tests/ClassMapGeneratorTest.php b/tests/ClassMapGeneratorTest.php index 393a69e..6e4e5ca 100644 --- a/tests/ClassMapGeneratorTest.php +++ b/tests/ClassMapGeneratorTest.php @@ -240,6 +240,18 @@ public function testCreateMapDoesNotHitRegexBacktraceLimit(): void self::assertEqualsNormalized($expected, $result); } + public function testGetPSR4Violations(): void + { + $this->generator->scanPaths(__DIR__ . '/Fixtures/psrViolations', null, 'psr-4', 'ExpectedNamespace\\'); + $classMap = $this->generator->getClassMap(); + static::assertSame( + [ + 'Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard. Skipping.', + ], + $classMap->getPsrViolations() + ); + } + /** * @param array $expected * @param array $actual diff --git a/tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php b/tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php new file mode 100644 index 0000000..03f7025 --- /dev/null +++ b/tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php @@ -0,0 +1,5 @@ + Date: Tue, 30 Apr 2024 13:52:50 +0200 Subject: [PATCH 2/4] Add test cases for scenarios that should fail/pass on invalid PSR-4 namespaces --- tests/ClassMapGeneratorTest.php | 2 ++ tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php | 5 +++++ .../ClassWithNameSpaceOutsideConfiguredScope.php | 5 +++++ tests/Fixtures/psrViolations/ClassWithoutNameSpace.php | 3 +++ 4 files changed, 15 insertions(+) create mode 100644 tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php create mode 100644 tests/Fixtures/psrViolations/ClassWithNameSpaceOutsideConfiguredScope.php create mode 100644 tests/Fixtures/psrViolations/ClassWithoutNameSpace.php diff --git a/tests/ClassMapGeneratorTest.php b/tests/ClassMapGeneratorTest.php index 6e4e5ca..4728bf7 100644 --- a/tests/ClassMapGeneratorTest.php +++ b/tests/ClassMapGeneratorTest.php @@ -246,7 +246,9 @@ public function testGetPSR4Violations(): void $classMap = $this->generator->getClassMap(); static::assertSame( [ + 'Class ClassWithoutNameSpace located in ./tests/Fixtures/psrViolations/ClassWithoutNameSpace.php does not comply with psr-4 autoloading standard. Skipping.', 'Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard. Skipping.', + 'Class UnexpectedNamespace\ClassWithNameSpaceOutsideConfiguredScope located in ./tests/Fixtures/psrViolations/ClassWithNameSpaceOutsideConfiguredScope.php does not comply with psr-4 autoloading standard. Skipping.', ], $classMap->getPsrViolations() ); diff --git a/tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php b/tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php new file mode 100644 index 0000000..3491cbb --- /dev/null +++ b/tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php @@ -0,0 +1,5 @@ + Date: Tue, 30 Apr 2024 13:54:47 +0200 Subject: [PATCH 3/4] Remove silent skip of namespaces without common root (Will still be silently passing if valid classes are found, will only show up when requesting PSR violations) --- src/ClassMapGenerator.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ClassMapGenerator.php b/src/ClassMapGenerator.php index 53f9a42..a67ce0e 100644 --- a/src/ClassMapGenerator.php +++ b/src/ClassMapGenerator.php @@ -216,10 +216,6 @@ private function filterByNamespace(array $classes, string $filePath, string $bas $realSubPath = substr($realSubPath, 0, $dotPosition === false ? PHP_INT_MAX : $dotPosition); foreach ($classes as $class) { - // silently skip if ns doesn't have common root - if ('' !== $baseNamespace && 0 !== strpos($class, $baseNamespace)) { - continue; - } // transform class name to file path and validate if ('psr-0' === $namespaceType) { $namespaceLength = strrpos($class, '\\'); From f48baf4ee46cf175cb5a5d284ac1ee48f1bd2a0b Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Fri, 3 May 2024 15:39:57 +0200 Subject: [PATCH 4/4] Swap expected PSR violations order to the order the errors occur in --- tests/ClassMapGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ClassMapGeneratorTest.php b/tests/ClassMapGeneratorTest.php index 24612f2..98d3094 100644 --- a/tests/ClassMapGeneratorTest.php +++ b/tests/ClassMapGeneratorTest.php @@ -247,8 +247,8 @@ public function testGetPSR4Violations(): void self::assertSame( [ 'Class ClassWithoutNameSpace located in ./tests/Fixtures/psrViolations/ClassWithoutNameSpace.php does not comply with psr-4 autoloading standard. Skipping.', - 'Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard. Skipping.', 'Class UnexpectedNamespace\ClassWithNameSpaceOutsideConfiguredScope located in ./tests/Fixtures/psrViolations/ClassWithNameSpaceOutsideConfiguredScope.php does not comply with psr-4 autoloading standard. Skipping.', + 'Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard. Skipping.', ], $classMap->getPsrViolations() );