diff --git a/src/ClassMap.php b/src/ClassMap.php index 0d06366..797efc6 100644 --- a/src/ClassMap.php +++ b/src/ClassMap.php @@ -176,4 +176,16 @@ public function count(): int { return \count($this->map); } + + /** + * Get the raw psr violations + * + * This is a map of filepath to an associative array of the warning string + * and the offending class name. + * @return array> + */ + public function getRawPsrViolations(): array + { + return $this->psrViolations; + } } diff --git a/tests/ClassMapGeneratorTest.php b/tests/ClassMapGeneratorTest.php index 1b286b5..615e18a 100644 --- a/tests/ClassMapGeneratorTest.php +++ b/tests/ClassMapGeneratorTest.php @@ -277,6 +277,32 @@ public function testGetPSR4Violations(): void ); } + public function testGetRawPSR4Violations(): void + { + $this->generator->scanPaths(__DIR__ . '/Fixtures/psrViolations', null, 'psr-4', 'ExpectedNamespace\\'); + $classMap = $this->generator->getClassMap(); + $rawViolations = $classMap->getRawPsrViolations(); + + $classWithoutNameSpaceFilepath = __DIR__ . '/Fixtures/psrViolations/ClassWithoutNameSpace.php'; + $classWithIncorrectSubNamespaceFilepath = __DIR__ . '/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php'; + $classWithNameSpaceOutsideConfiguredScopeFilepath = __DIR__ . '/Fixtures/psrViolations/ClassWithNameSpaceOutsideConfiguredScope.php'; + + self::assertArrayHasKey($classWithoutNameSpaceFilepath, $rawViolations); + self::assertCount(1, $rawViolations[$classWithoutNameSpaceFilepath]); + self::assertSame('Class ClassWithoutNameSpace located in ./tests/Fixtures/psrViolations/ClassWithoutNameSpace.php does not comply with psr-4 autoloading standard (rule: ExpectedNamespace\ => ./tests/Fixtures/psrViolations). Skipping.', $rawViolations[$classWithoutNameSpaceFilepath][0]['warning']); + self::assertSame('ClassWithoutNameSpace', $rawViolations[$classWithoutNameSpaceFilepath][0]['className']); + + self::assertArrayHasKey($classWithIncorrectSubNamespaceFilepath, $rawViolations); + self::assertCount(1, $rawViolations[$classWithIncorrectSubNamespaceFilepath]); + self::assertSame('Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard (rule: ExpectedNamespace\ => ./tests/Fixtures/psrViolations). Skipping.', $rawViolations[$classWithIncorrectSubNamespaceFilepath][0]['warning']); + self::assertSame('ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace', $rawViolations[$classWithIncorrectSubNamespaceFilepath][0]['className']); + + self::assertArrayHasKey($classWithNameSpaceOutsideConfiguredScopeFilepath, $rawViolations); + self::assertCount(1, $rawViolations[$classWithNameSpaceOutsideConfiguredScopeFilepath]); + self::assertSame('Class UnexpectedNamespace\ClassWithNameSpaceOutsideConfiguredScope located in ./tests/Fixtures/psrViolations/ClassWithNameSpaceOutsideConfiguredScope.php does not comply with psr-4 autoloading standard (rule: ExpectedNamespace\ => ./tests/Fixtures/psrViolations). Skipping.', $rawViolations[$classWithNameSpaceOutsideConfiguredScopeFilepath][0]['warning']); + self::assertSame('UnexpectedNamespace\ClassWithNameSpaceOutsideConfiguredScope', $rawViolations[$classWithNameSpaceOutsideConfiguredScopeFilepath][0]['className']); + } + public function testCreateMapWithDirectoryExcluded(): void { $expected = array(