Skip to content

Commit

Permalink
implicit "--no-check-lock" (10715, --no-check-lock; 0.1/?)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ktomk committed Apr 11, 2022
1 parent f4a5a72 commit 7e4a8b7
Showing 1 changed file with 47 additions and 20 deletions.
67 changes: 47 additions & 20 deletions src/Composer/Config.php
Expand Up @@ -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':
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 7e4a8b7

Please sign in to comment.