1
0
Fork 0

Add --strict-psr flag to dump-autoload to fail the process if psr violations were detected, fixes #10241 (#10886)

pull/10893/head
Jordi Boggiano 2022-06-20 13:57:20 +02:00 committed by GitHub
parent c2046566fc
commit e8530699c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -921,6 +921,8 @@ performance.
* **--ignore-platform-req:** ignore a specific platform requirement (`php`, `hhvm`, * **--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. `lib-*` and `ext-*`) and skip the [platform check](07-runtime.md#platform-check) for it.
Multiple requirements can be ignored via wildcard. 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 ## clear-cache / clearcache / cc

View File

@ -12,6 +12,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
use Composer\ClassMapGenerator\ClassMap;
use Composer\ClassMapGenerator\ClassMapGenerator; use Composer\ClassMapGenerator\ClassMapGenerator;
use Composer\Config; use Composer\Config;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
@ -161,7 +162,7 @@ class AutoloadGenerator
/** /**
* @param string $targetDir * @param string $targetDir
* @param bool $scanPsrPackages * @param bool $scanPsrPackages
* @return int * @return ClassMap
* @throws \Seld\JsonLint\ParsingException * @throws \Seld\JsonLint\ParsingException
* @throws \RuntimeException * @throws \RuntimeException
*/ */
@ -445,7 +446,7 @@ EOF;
)); ));
} }
return \count($classMap); return $classMap;
} }
/** /**

View File

@ -41,6 +41,7 @@ class DumpAutoloadCommand extends BaseCommand
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('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-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('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( ->setHelp(
<<<EOT <<<EOT
@ -69,6 +70,10 @@ EOT
$apcuPrefix = $input->getOption('apcu-prefix'); $apcuPrefix = $input->getOption('apcu-prefix');
$apcu = $apcuPrefix !== null || $input->getOption('apcu') || $config->get('apcu-autoloader'); $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) { if ($authoritative) {
$this->getIO()->write('<info>Generating optimized autoload files (authoritative)</info>'); $this->getIO()->write('<info>Generating optimized autoload files (authoritative)</info>');
} elseif ($optimize) { } elseif ($optimize) {
@ -91,7 +96,8 @@ EOT
$generator->setRunScripts(true); $generator->setRunScripts(true);
$generator->setApcu($apcu, $apcuPrefix); $generator->setApcu($apcu, $apcuPrefix);
$generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); $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) { if ($authoritative) {
$this->getIO()->write('<info>Generated optimized autoload files (authoritative) containing '. $numberOfClasses .' classes</info>'); $this->getIO()->write('<info>Generated optimized autoload files (authoritative) containing '. $numberOfClasses .' classes</info>');
@ -101,6 +107,10 @@ EOT
$this->getIO()->write('<info>Generated autoload files</info>'); $this->getIO()->write('<info>Generated autoload files</info>');
} }
if ($input->getOption('strict-psr') && count($classMap->getPsrViolations()) > 0) {
return 1;
}
return 0; return 0;
} }
} }