From dc857b4f9148def53cb5c182dbb965aaac76d947 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 31 May 2024 15:08:44 +0200 Subject: [PATCH] Fixed PSR violations for classes not matching the namespace of a rule being hidden, fixes #11957 --- composer.json | 10 +++- composer.lock | 48 +++++++++---------- src/Composer/Autoload/AutoloadGenerator.php | 22 ++++++--- .../Test/Command/DumpAutoloadCommandTest.php | 2 +- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 3513b8c58..1e38b523a 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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" diff --git a/composer.lock b/composer.lock index 9dc770cf1..69a37ac28 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9cc98d4fd8dcc30565a6570841b7ec85", + "content-hash": "1322de6eee2357fec92034b7b41e6512", "packages": [ { "name": "composer/ca-bundle", @@ -84,16 +84,16 @@ }, { "name": "composer/class-map-generator", - "version": "1.1.1", + "version": "1.3.1", "source": { "type": "git", "url": "https://github.com/composer/class-map-generator.git", - "reference": "8286a62d243312ed99b3eee20d5005c961adb311" + "reference": "5bb7f1e135ccc696bdccb7ec945403fcaac144d5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/class-map-generator/zipball/8286a62d243312ed99b3eee20d5005c961adb311", - "reference": "8286a62d243312ed99b3eee20d5005c961adb311", + "url": "https://api.github.com/repos/composer/class-map-generator/zipball/5bb7f1e135ccc696bdccb7ec945403fcaac144d5", + "reference": "5bb7f1e135ccc696bdccb7ec945403fcaac144d5", "shasum": "" }, "require": { @@ -137,7 +137,7 @@ ], "support": { "issues": "https://github.com/composer/class-map-generator/issues", - "source": "https://github.com/composer/class-map-generator/tree/1.1.1" + "source": "https://github.com/composer/class-map-generator/tree/1.3.1" }, "funding": [ { @@ -153,7 +153,7 @@ "type": "tidelift" } ], - "time": "2024-03-15T12:53:41+00:00" + "time": "2024-05-31T15:31:05+00:00" }, { "name": "composer/metadata-minifier", @@ -2015,16 +2015,16 @@ "packages-dev": [ { "name": "phpstan/phpstan", - "version": "1.11.2", + "version": "1.11.3", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "0d5d4294a70deb7547db655c47685d680e39cfec" + "reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/0d5d4294a70deb7547db655c47685d680e39cfec", - "reference": "0d5d4294a70deb7547db655c47685d680e39cfec", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e64220a05c1209fc856d58e789c3b7a32c0bb9a5", + "reference": "e64220a05c1209fc856d58e789c3b7a32c0bb9a5", "shasum": "" }, "require": { @@ -2069,7 +2069,7 @@ "type": "github" } ], - "time": "2024-05-24T13:23:04+00:00" + "time": "2024-05-31T13:53:37+00:00" }, { "name": "phpstan/phpstan-deprecation-rules", @@ -2221,16 +2221,16 @@ }, { "name": "phpstan/phpstan-symfony", - "version": "1.4.1", + "version": "1.4.3", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-symfony.git", - "reference": "d530cfebba55763732bc2421f79d2576d9d7942f" + "reference": "af6ae0f4b91bc080265e80776af26da3e5befb28" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-symfony/zipball/d530cfebba55763732bc2421f79d2576d9d7942f", - "reference": "d530cfebba55763732bc2421f79d2576d9d7942f", + "url": "https://api.github.com/repos/phpstan/phpstan-symfony/zipball/af6ae0f4b91bc080265e80776af26da3e5befb28", + "reference": "af6ae0f4b91bc080265e80776af26da3e5befb28", "shasum": "" }, "require": { @@ -2287,22 +2287,22 @@ "description": "Symfony Framework extensions and rules for PHPStan", "support": { "issues": "https://github.com/phpstan/phpstan-symfony/issues", - "source": "https://github.com/phpstan/phpstan-symfony/tree/1.4.1" + "source": "https://github.com/phpstan/phpstan-symfony/tree/1.4.3" }, - "time": "2024-05-24T14:00:29+00:00" + "time": "2024-05-30T15:01:27+00:00" }, { "name": "symfony/phpunit-bridge", - "version": "v7.0.7", + "version": "v7.1.0", "source": { "type": "git", "url": "https://github.com/symfony/phpunit-bridge.git", - "reference": "0a0b90ba08b9a03e09ad49f8d613bdf3eca3a7a9" + "reference": "1168ef27edb094fe4ba755ec937cf62a6dff84eb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/0a0b90ba08b9a03e09ad49f8d613bdf3eca3a7a9", - "reference": "0a0b90ba08b9a03e09ad49f8d613bdf3eca3a7a9", + "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/1168ef27edb094fe4ba755ec937cf62a6dff84eb", + "reference": "1168ef27edb094fe4ba755ec937cf62a6dff84eb", "shasum": "" }, "require": { @@ -2354,7 +2354,7 @@ "description": "Provides utilities for PHPUnit, especially user deprecation notices management", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/phpunit-bridge/tree/v7.0.7" + "source": "https://github.com/symfony/phpunit-bridge/tree/v7.1.0" }, "funding": [ { @@ -2370,7 +2370,7 @@ "type": "tidelift" } ], - "time": "2024-04-18T09:29:19+00:00" + "time": "2024-05-17T10:55:18+00:00" } ], "aliases": [], diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 844b80207..9da1767ad 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -319,7 +319,7 @@ EOF; EOF; } - $excluded = null; + $excluded = []; if (!empty($autoloads['exclude-from-classmap'])) { $excluded = $autoloads['exclude-from-classmap']; } @@ -348,7 +348,14 @@ EOF; 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); } } } @@ -368,6 +375,9 @@ EOF; ); } } + + // output PSR violations which are not coming from the vendor dir + $classMap->clearPsrViolationsByPath($vendorPath); foreach ($classMap->getPsrViolations() as $msg) { $this->io->writeError("$msg"); } @@ -460,12 +470,12 @@ EOF; } /** - * @param array|null $excluded + * @param array $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; } @@ -602,7 +612,7 @@ EOF; } if (isset($autoloads['classmap'])) { - $excluded = null; + $excluded = []; if (!empty($autoloads['exclude-from-classmap'])) { $excluded = $autoloads['exclude-from-classmap']; } diff --git a/tests/Composer/Test/Command/DumpAutoloadCommandTest.php b/tests/Composer/Test/Command/DumpAutoloadCommandTest.php index fd58156ab..c8c46a064 100644 --- a/tests/Composer/Test/Command/DumpAutoloadCommandTest.php +++ b/tests/Composer/Test/Command/DumpAutoloadCommandTest.php @@ -62,7 +62,7 @@ class DumpAutoloadCommandTest extends TestCase 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