Skip to content

Commit

Permalink
add getRawPsrViolations (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech authored Nov 25, 2024
1 parent c1c70e6 commit 9ab27fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array<array{warning: string, className: string}>>
*/
public function getRawPsrViolations(): array
{
return $this->psrViolations;
}
}
26 changes: 26 additions & 0 deletions tests/ClassMapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9ab27fb

Please sign in to comment.