From 7e4a8b785ccc617997212c8671e6c1edd97af1e3 Mon Sep 17 00:00:00 2001 From: Tom Klingenberg Date: Tue, 12 Apr 2022 00:27:49 +0200 Subject: [PATCH] implicit "--no-check-lock" (10715, --no-check-lock; 0.1/?) prepare: refactor: extract getting setting mapping booleans. get(): mixed -> getBool(): bool (make the implicit type-information explicit, phpstan complained after ->get() calls in expressions) issue: 10715 [1]: https://symfony.com/blog/new-in-symfony-5-3-negatable-command-options --- src/Composer/Config.php | 67 +++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 0d927f1903d7..0ea62f9c82be 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -308,32 +308,14 @@ public function get($key, $flags = 0) return (($flags & self::RELATIVE_PATHS) == self::RELATIVE_PATHS) ? $val : $this->realpath($val); - // booleans with env var support + // booleans with and without env var support case 'cache-read-only': case 'htaccess-protect': - // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config - $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); - - $val = $this->getComposerEnv($env); - if (false === $val) { - $val = $this->config[$key]; - } else { - $this->setSourceOfConfigValue($val, $key, $env); - } - - return $val !== 'false' && (bool) $val; - - // booleans without env var support case 'disable-tls': case 'secure-http': case 'use-github-api': case 'lock': - // special case for secure-http - if ($key === 'secure-http' && $this->get('disable-tls') === true) { - return false; - } - - return $this->config[$key] !== 'false' && (bool) $this->config[$key]; + return $this->getBool($key, $flags); // ints without env var support case 'cache-ttl': @@ -436,6 +418,51 @@ public function get($key, $flags = 0) } } + /** + * Returns a boolean setting + * + * @param string $key + * @param int $flags Options (see class constants) + * @throws \RuntimeException + * + * @return bool + */ + public function getBool($key, $flags = 0) + { + switch ($key) + { + // booleans with env var support + case 'cache-read-only': + case 'htaccess-protect': + // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config + $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); + + $val = $this->getComposerEnv($env); + if (false === $val) { + $val = $this->config[$key]; + } else { + $this->setSourceOfConfigValue($val, $key, $env); + } + + return $val !== 'false' && (bool) $val; + + // booleans without env var support + case 'disable-tls': + case 'secure-http': + case 'use-github-api': + case 'lock': + // special case for secure-http + if ($key === 'secure-http' && $this->get('disable-tls') === true) { + return false; + } + + return $this->config[$key] !== 'false' && (bool) $this->config[$key]; + + default: + throw new \RuntimeException('Not a boolean setting: "'.$key.'"'); + } + } + /** * @param int $flags *