From a03331bd219223ce15ddcaae480370d1163ec538 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 19 Sep 2024 11:40:57 +0200 Subject: [PATCH] Add --strict-ambiguous to dump-autoload command (#12119) Fixes #6221 --- doc/03-cli.md | 2 ++ src/Composer/Command/DumpAutoloadCommand.php | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/doc/03-cli.md b/doc/03-cli.md index 8c1c56e16..c99624db3 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -990,6 +990,8 @@ performance. Multiple requirements can be ignored via wildcard. * **--strict-psr:** Return a failed exit code (1) if PSR-4 or PSR-0 mapping errors are present. Requires --optimize to work. +* **--strict-ambiguous:** Return a failed exit code (2) if the same class is found + in multiple files. Requires --optimize to work. ## clear-cache / clearcache / cc diff --git a/src/Composer/Command/DumpAutoloadCommand.php b/src/Composer/Command/DumpAutoloadCommand.php index c30fae7a8..6e6b56a5a 100644 --- a/src/Composer/Command/DumpAutoloadCommand.php +++ b/src/Composer/Command/DumpAutoloadCommand.php @@ -43,6 +43,7 @@ class DumpAutoloadCommand extends BaseCommand 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.'), + new InputOption('strict-ambiguous', null, InputOption::VALUE_NONE, 'Return a failed status code (2) if the same class is found in multiple files. Requires --optimize to work.'), ]) ->setHelp( <<getOption('strict-psr') && !$optimize && !$authoritative) { throw new \InvalidArgumentException('--strict-psr mode only works with optimized autoloader, use --optimize or --classmap-authoritative if you want a strict return value.'); } + if ($input->getOption('strict-ambiguous') && !$optimize && !$authoritative) { + throw new \InvalidArgumentException('--strict-ambiguous mode only works with optimized autoloader, use --optimize or --classmap-authoritative if you want a strict return value.'); + } if ($authoritative) { $this->getIO()->write('Generating optimized autoload files (authoritative)'); @@ -124,6 +128,10 @@ EOT return 1; } + if ($input->getOption('strict-ambiguous') && count($classMap->getAmbiguousClasses()) > 0) { + return 2; + } + return 0; } }