Skip to content

Commit

Permalink
Detect other PSR violations where namespace is missing or not a subna…
Browse files Browse the repository at this point in the history
…mespace of configured one (#8)
  • Loading branch information
PrinsFrank committed May 31, 2024
1 parent a5758da commit a05a230
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,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, '\\');
Expand Down
14 changes: 14 additions & 0 deletions tests/ClassMapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ 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();
self::assertSame(
[
'Class ClassWithoutNameSpace located in ./tests/Fixtures/psrViolations/ClassWithoutNameSpace.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()
);
}

public function testCreateMapWithDirectoryExcluded(): void
{
$expected = array(
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/psrViolations/ClassWithCorrectNameSpace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ExpectedNamespace;

class ClassWithCorrectNameSpace {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ExpectedNamespace\UnexpectedSubNamespace;

class ClassWithIncorrectSubNamespace {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace UnexpectedNamespace;

class ClassWithNameSpaceOutsideConfiguredScope {}
3 changes: 3 additions & 0 deletions tests/Fixtures/psrViolations/ClassWithoutNameSpace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class ClassWithoutNameSpace {}

0 comments on commit a05a230

Please sign in to comment.