From c78850ab987134ed974d1c862bd94e7e81b8b2ae Mon Sep 17 00:00:00 2001 From: Julian Date: Thu, 25 Feb 2021 10:24:09 +0100 Subject: [PATCH] Show a warning if require key changes for dependency (#9542) Fixes #9516 Co-authored-by: Jordi Boggiano --- src/Composer/Command/RequireCommand.php | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 524f5aa4c..95da6ee9f 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -211,6 +211,29 @@ EOT $versionParser->parseConstraints($constraint); } + $inconsistentRequireKeys = $this->getInconsistentRequireKeys($requirements, $requireKey); + if (count($inconsistentRequireKeys) > 0) { + foreach ($inconsistentRequireKeys as $package) { + $io->warning(sprintf( + '%s is currently present in the %s key and you ran the command %s the --dev flag, which would move it to the %s key.', + $package, + $removeKey, + $input->getOption('dev') ? 'with' : 'without', + $requireKey + )); + } + + if ($io->isInteractive()) { + if (!$io->askConfirmation(sprintf('Do you want to move %s? [no]? ', count($inconsistentRequireKeys) > 1 ? 'these requirements' : 'this requirement'), false)) { + if (!$io->askConfirmation(sprintf('Do you want to re-run the command %s --dev? [yes]? ', $input->getOption('dev') ? 'without' : 'with'), true)) { + return 0; + } + + list($requireKey, $removeKey) = array($removeKey, $requireKey); + } + } + } + $sortPackages = $input->getOption('sort-packages') || $composer->getConfig()->get('sort-packages'); $this->firstRequire = $this->newlyCreated; @@ -247,6 +270,42 @@ EOT } } + private function getInconsistentRequireKeys(array $newRequirements, $requireKey) + { + $requireKeys = $this->getPackagesByRequireKey(); + $inconsistentRequirements = array(); + foreach ($requireKeys as $package => $packageRequireKey) { + if (!isset($newRequirements[$package])) { + continue; + } + if ($requireKey !== $packageRequireKey) { + $inconsistentRequirements[] = $package; + } + } + + return $inconsistentRequirements; + } + + private function getPackagesByRequireKey() + { + $composerDefinition = $this->json->read(); + $require = array(); + $requireDev = array(); + + if (isset($composerDefinition['require'])) { + $require = $composerDefinition['require']; + } + + if (isset($composerDefinition['require-dev'])) { + $requireDev = $composerDefinition['require-dev']; + } + + return array_merge( + array_fill_keys(array_keys($require), 'require'), + array_fill_keys(array_keys($requireDev), 'require-dev') + ); + } + private function doUpdate(InputInterface $input, OutputInterface $output, IOInterface $io, array $requirements, $requireKey, $removeKey) { // Update packages