diff --git a/src/Composer/Command/ValidateCommand.php b/src/Composer/Command/ValidateCommand.php index c08cf2c10..54177576e 100644 --- a/src/Composer/Command/ValidateCommand.php +++ b/src/Composer/Command/ValidateCommand.php @@ -40,14 +40,16 @@ class ValidateCommand extends Command new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), - new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies.'), + new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies'), + new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code for warnings as well as errors'), new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') )) ->setHelp(<<getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkPublish = !$input->getOption('no-check-publish'); $checkLock = !$input->getOption('no-check-lock'); + $isStrict = $input->getOption('strict'); list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); $lockErrors = array(); @@ -91,7 +94,7 @@ EOT $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true); - $exitCode = $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0; + $exitCode = $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 2 : ($isStrict && $warnings ? 1 : 0); if ($input->getOption('with-dependencies')) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); @@ -102,7 +105,8 @@ EOT list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors); - $exitCode = $exitCode === 1 || $errors || ($publishErrors && $checkPublish) ? 1 : 0; + $depCode = $errors || ($publishErrors && $checkPublish) ? 2 : ($isStrict && $warnings ? 1 : 0); + $exitCode = max($depCode, $exitCode); } } } @@ -110,7 +114,7 @@ EOT return $exitCode; } - private function outputResult($io, $name, $errors, $warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false) + private function outputResult($io, $name, &$errors, &$warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false) { if (!$errors && !$publishErrors && !$warnings) { $io->write('' . $name . ' is valid'); @@ -129,25 +133,25 @@ EOT $io->writeError('' . $name . ' is invalid, the following errors/warnings were found:'); } - $messages = array( - 'error' => $errors, - 'warning' => $warnings, - ); - // If checking publish errors, display them as errors, otherwise just show them as warnings if ($checkPublish) { - $messages['error'] = array_merge($messages['error'], $publishErrors); + $errors = array_merge($errors, $publishErrors); } else { - $messages['warning'] = array_merge($messages['warning'], $publishErrors); + $warnings = array_merge($warnings, $publishErrors); } // If checking lock errors, display them as errors, otherwise just show them as warnings if ($checkLock) { - $messages['error'] = array_merge($messages['error'], $lockErrors); + $errors = array_merge($errors, $lockErrors); } else { - $messages['warning'] = array_merge($messages['warning'], $lockErrors); + $warnings = array_merge($warnings, $lockErrors); } + $messages = array( + 'error' => $errors, + 'warning' => $warnings, + ); + foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { $io->writeError('<' . $style . '>' . $msg . '');