Skip to content

Commit

Permalink
Fixed PSR violations for classes not matching the namespace of a rule…
Browse files Browse the repository at this point in the history
… being hidden, fixes #11957
  • Loading branch information
Seldaek committed May 31, 2024
1 parent c1be804 commit dc857b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"require": {
"php": "^7.2.5 || ^8.0",
"composer/ca-bundle": "^1.0",
"composer/class-map-generator": "^1.0",
"composer/class-map-generator": "^1.3.1",
"composer/metadata-minifier": "^1.0",
"composer/semver": "^3.3",
"composer/spdx-licenses": "^1.5.7",
Expand Down Expand Up @@ -81,7 +81,13 @@
"autoload-dev": {
"psr-4": {
"Composer\\Test\\": "tests/Composer/Test/"
}
},
"exclude-from-classmap": [
"tests/Composer/Test/Fixtures/",
"tests/Composer/Test/Autoload/Fixtures",
"tests/Composer/Test/Autoload/MinimumVersionSupport",
"tests/Composer/Test/Plugin/Fixtures"
]
},
"bin": [
"bin/composer"
Expand Down
48 changes: 24 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions src/Composer/Autoload/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public static function autoload(\$class)
EOF;
}

$excluded = null;
$excluded = [];
if (!empty($autoloads['exclude-from-classmap'])) {
$excluded = $autoloads['exclude-from-classmap'];
}
Expand Down Expand Up @@ -348,7 +348,14 @@ public static function autoload(\$class)
continue;
}

$classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded), $group['type'], $namespace);
// if the vendor dir is contained within a psr-0/psr-4 dir being scanned we exclude it
if (str_contains($vendorPath, $dir.'/')) {
$exclusionRegex = $this->buildExclusionRegex($dir, array_merge($excluded, [$vendorPath.'/']));
} else {
$exclusionRegex = $this->buildExclusionRegex($dir, $excluded);
}

$classMapGenerator->scanPaths($dir, $exclusionRegex, $group['type'], $namespace);
}
}
}
Expand All @@ -368,6 +375,9 @@ public static function autoload(\$class)
);
}
}

// output PSR violations which are not coming from the vendor dir
$classMap->clearPsrViolationsByPath($vendorPath);
foreach ($classMap->getPsrViolations() as $msg) {
$this->io->writeError("<warning>$msg</warning>");
}
Expand Down Expand Up @@ -460,12 +470,12 @@ public static function autoload(\$class)
}

/**
* @param array<string>|null $excluded
* @param array<string> $excluded
* @return non-empty-string|null
*/
private function buildExclusionRegex(string $dir, ?array $excluded): ?string
private function buildExclusionRegex(string $dir, array $excluded): ?string
{
if (null === $excluded) {
if ([] === $excluded) {
return null;
}

Expand Down Expand Up @@ -602,7 +612,7 @@ public function createLoader(array $autoloads, ?string $vendorDir = null)
}

if (isset($autoloads['classmap'])) {
$excluded = null;
$excluded = [];
if (!empty($autoloads['exclude-from-classmap'])) {
$excluded = $autoloads['exclude-from-classmap'];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Composer/Test/Command/DumpAutoloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testFailsUsingStrictPsrIfClassMapViolationsAreFound(): void
self::assertSame(1, $appTester->run(['command' => 'dump-autoload', '--optimize' => true, '--strict-psr' => true]));

$output = $appTester->getDisplay(true);
self::assertMatchesRegularExpression('/Class Application\\\Src\\\Foo located in .*? does not comply with psr-4 autoloading standard. Skipping./', $output);
self::assertMatchesRegularExpression('#Class Application\\\\Src\\\\Foo located in .*? does not comply with psr-4 autoloading standard \(rule: Application\\\\ => \./src\)\. Skipping\.#', $output);
}

public function testUsingClassmapAuthoritative(): void
Expand Down

0 comments on commit dc857b4

Please sign in to comment.