From 6af82965e8289328262aa164aaec9a583b6d5e0f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 19 Jun 2022 21:54:51 +0200 Subject: [PATCH] Add --strict-psr flag to dump-autoload to fail the process if psr violations were detected, fixes #10241 --- doc/03-cli.md | 2 ++ src/Composer/Autoload/AutoloadGenerator.php | 5 +++-- src/Composer/Command/DumpAutoloadCommand.php | 12 +++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 4a6a8d3732dd..039e6a8bd854 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -921,6 +921,8 @@ performance. * **--ignore-platform-req:** ignore a specific platform requirement (`php`, `hhvm`, `lib-*` and `ext-*`) and skip the [platform check](07-runtime.md#platform-check) for it. Multiple requirements can be ignored via wildcard. +* **--strict-psr:** Return a failed status code (1) if PSR-4 or PSR-0 mapping errors + are present. Requires --optimize to work. ## clear-cache / clearcache / cc diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 60b7388d7d07..8a51596d6aab 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -12,6 +12,7 @@ namespace Composer\Autoload; +use Composer\ClassMapGenerator\ClassMap; use Composer\ClassMapGenerator\ClassMapGenerator; use Composer\Config; use Composer\EventDispatcher\EventDispatcher; @@ -161,7 +162,7 @@ public function setPlatformRequirementFilter(PlatformRequirementFilterInterface /** * @param string $targetDir * @param bool $scanPsrPackages - * @return int + * @return ClassMap * @throws \Seld\JsonLint\ParsingException * @throws \RuntimeException */ @@ -445,7 +446,7 @@ public static function autoload(\$class) )); } - return \count($classMap); + return $classMap; } /** diff --git a/src/Composer/Command/DumpAutoloadCommand.php b/src/Composer/Command/DumpAutoloadCommand.php index 9ec6abed0dbd..888ef347a4c6 100644 --- a/src/Composer/Command/DumpAutoloadCommand.php +++ b/src/Composer/Command/DumpAutoloadCommand.php @@ -41,6 +41,7 @@ protected function configure() new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables autoload-dev rules. Composer will by default infer this automatically according to the last install or update --no-dev state.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), + new InputOption('strict-psr', null, InputOption::VALUE_NONE, 'Return a failed status code (1) if PSR-4 or PSR-0 mapping errors are present. Requires --optimize to work.'), )) ->setHelp( <<getOption('apcu-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu') || $config->get('apcu-autoloader'); + if ($input->getOption('strict-psr') && !$optimize) { + throw new \InvalidArgumentException('--strict-psr mode only works with optimized autoloader, use --optimize if you want a strict return value.'); + } + if ($authoritative) { $this->getIO()->write('Generating optimized autoload files (authoritative)'); } elseif ($optimize) { @@ -91,7 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $generator->setRunScripts(true); $generator->setApcu($apcu, $apcuPrefix); $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); - $numberOfClasses = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); + $classMap = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); + $numberOfClasses = count($classMap); if ($authoritative) { $this->getIO()->write('Generated optimized autoload files (authoritative) containing '. $numberOfClasses .' classes'); @@ -101,6 +107,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->getIO()->write('Generated autoload files'); } + if ($input->getOption('strict-psr') && count($classMap->getPsrViolations()) > 0) { + return 1; + } + return 0; } }