Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect other PSR violations where namespace is missing or not a subnamespace of configured one #8

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
PrinsFrank marked this conversation as resolved.
Show resolved Hide resolved
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 {}
Loading