Skip to content

Commit

Permalink
Refactor the BasePackage::$stabilities into a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jul 12, 2024
1 parent 07aee7e commit 685add7
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 54 deletions.
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Composer/Command/ArchiveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function selectPackage(IOInterface $io, string $packageName, ?string $
}

if ($version !== null && Preg::isMatchStrictGroups('{@(stable|RC|beta|alpha|dev)$}i', $version, $match)) {
$minStability = $match[1];
$minStability = VersionParser::normalizeStability($match[1]);
$version = (string) substr($version, 0, -strlen($match[0]));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ static function ($vals) {
}],
'minimum-stability' => [
static function ($val): bool {
return isset(BasePackage::$stabilities[VersionParser::normalizeStability($val)]);
return isset(BasePackage::STABILITIES[VersionParser::normalizeStability($val)]);
},
static function ($val): string {
return VersionParser::normalizeStability($val);
Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Command/CreateProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ protected function installRootPackage(InputInterface $input, IOInterface $io, Co
if (null === $stability) {
if (null === $packageVersion) {
$stability = 'stable';
} elseif (Preg::isMatchStrictGroups('{^[^,\s]*?@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $packageVersion, $match)) {
} elseif (Preg::isMatchStrictGroups('{^[^,\s]*?@('.implode('|', array_keys(BasePackage::STABILITIES)).')$}i', $packageVersion, $match)) {
$stability = $match[1];
} else {
$stability = VersionParser::parseStability($packageVersion);
Expand All @@ -384,8 +384,8 @@ protected function installRootPackage(InputInterface $input, IOInterface $io, Co

$stability = VersionParser::normalizeStability($stability);

if (!isset(BasePackage::$stabilities[$stability])) {
throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::$stabilities)));
if (!isset(BasePackage::STABILITIES[$stability])) {
throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::STABILITIES)));
}

$composer = $this->createComposerInstance($input, $io, $config->all(), $disablePlugins, $disableScripts);
Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function configure()
new InputOption('homepage', null, InputOption::VALUE_REQUIRED, 'Homepage of package'),
new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"', null, $this->suggestAvailablePackageInclPlatform()),
new InputOption('require-dev', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"', null, $this->suggestAvailablePackageInclPlatform()),
new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum stability (empty or one of: '.implode(', ', array_keys(BasePackage::$stabilities)).')'),
new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum stability (empty or one of: '.implode(', ', array_keys(BasePackage::STABILITIES)).')'),
new InputOption('license', 'l', InputOption::VALUE_REQUIRED, 'License of package'),
new InputOption('repository', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Add custom repositories, either by URL or using JSON arrays'),
new InputOption('autoload', 'a', InputOption::VALUE_REQUIRED, 'Add PSR-4 autoload mapping. Maps your package\'s namespace to the provided directory. (Expects a relative path, e.g. src/)'),
Expand Down Expand Up @@ -364,10 +364,10 @@ static function ($value) use ($minimumStability) {
return $minimumStability;
}

if (!isset(BasePackage::$stabilities[$value])) {
if (!isset(BasePackage::STABILITIES[$value])) {
throw new \InvalidArgumentException(
'Invalid minimum stability "'.$value.'". Must be empty or one of: '.
implode(', ', array_keys(BasePackage::$stabilities))
implode(', ', array_keys(BasePackage::STABILITIES))
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/Composer/Command/PackageDiscoveryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Composer\Filter\PlatformRequirementFilter\IgnoreAllPlatformRequirementFilter;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\IOInterface;
use Composer\Package\BasePackage;
use Composer\Package\CompletePackageInterface;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser;
Expand Down Expand Up @@ -52,6 +53,9 @@ protected function getRepos(): CompositeRepository
return $this->repos;
}

/**
* @param key-of<BasePackage::STABILITIES>|null $minimumStability
*/
private function getRepositorySet(InputInterface $input, ?string $minimumStability = null): RepositorySet
{
$key = $minimumStability ?? 'default';
Expand All @@ -64,6 +68,9 @@ private function getRepositorySet(InputInterface $input, ?string $minimumStabili
return $this->repositorySets[$key];
}

/**
* @return key-of<BasePackage::STABILITIES>
*/
private function getMinimumStability(InputInterface $input): string
{
if ($input->hasOption('stability')) { // @phpstan-ignore-line as InitCommand does have this option but not all classes using this trait do
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ private function findLatestPackage(PackageInterface $package, Composer $composer
$stability = $composer->getPackage()->getMinimumStability();
$flags = $composer->getPackage()->getStabilityFlags();
if (isset($flags[$name])) {
$stability = array_search($flags[$name], BasePackage::$stabilities, true);
$stability = array_search($flags[$name], BasePackage::STABILITIES, true);
}

$bestStability = $stability;
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/DependencyResolver/DefaultPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(bool $preferStable = false, bool $preferLowest = fal
public function versionCompare(PackageInterface $a, PackageInterface $b, string $operator): bool
{
if ($this->preferStable && ($stabA = $a->getStability()) !== ($stabB = $b->getStability())) {
return BasePackage::$stabilities[$stabA] < BasePackage::$stabilities[$stabB];
return BasePackage::STABILITIES[$stabA] < BasePackage::STABILITIES[$stabB];
}

// dev versions need to be compared as branches via matchSpecific's special treatment, the rest can be optimized with compiling matcher
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/DependencyResolver/PoolBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PoolBuilder
{
/**
* @var int[]
* @phpstan-var array<string, BasePackage::STABILITY_*>
* @phpstan-var array<key-of<BasePackage::STABILITIES>, BasePackage::STABILITY_*>
*/
private $acceptableStabilities;
/**
Expand Down Expand Up @@ -153,7 +153,7 @@ class PoolBuilder

/**
* @param int[] $acceptableStabilities array of stability => BasePackage::STABILITY_* value
* @phpstan-param array<string, BasePackage::STABILITY_*> $acceptableStabilities
* @phpstan-param array<key-of<BasePackage::STABILITIES>, BasePackage::STABILITY_*> $acceptableStabilities
* @param int[] $stabilityFlags an array of package name => BasePackage::STABILITY_* value
* @phpstan-param array<string, BasePackage::STABILITY_*> $stabilityFlags
* @param array[] $rootAliases
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ private function createRepositorySet(bool $forUpdate, PlatformRepository $platfo
$this->fixedRootPackage->setRequires([]);
$this->fixedRootPackage->setDevRequires([]);

$stabilityFlags[$this->package->getName()] = BasePackage::$stabilities[VersionParser::parseStability($this->package->getVersion())];
$stabilityFlags[$this->package->getName()] = BasePackage::STABILITIES[VersionParser::parseStability($this->package->getVersion())];

$repositorySet = new RepositorySet($minimumStability, $stabilityFlags, $rootAliases, $this->package->getReferences(), $rootRequires, $this->temporaryConstraints);
$repositorySet->addRepository(new RootPackageRepository($this->fixedRootPackage));
Expand Down
13 changes: 10 additions & 3 deletions src/Composer/Package/BasePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ abstract class BasePackage implements PackageInterface
public const STABILITY_ALPHA = 15;
public const STABILITY_DEV = 20;

/** @var array<string, self::STABILITY_*> */
public static $stabilities = [
public const STABILITIES = [
'stable' => self::STABILITY_STABLE,
'RC' => self::STABILITY_RC,
'beta' => self::STABILITY_BETA,
'alpha' => self::STABILITY_ALPHA,
'dev' => self::STABILITY_DEV,
];

/**
* @deprecated
* @readonly
* @var array<key-of<BasePackage::STABILITIES>, self::STABILITY_*>
* @phpstan-ignore property.readOnlyByPhpDocDefaultValue
*/
public static $stabilities = self::STABILITIES;

/**
* READ-ONLY: The package id, public for fast access in dependency solver
* @var int
Expand Down Expand Up @@ -234,7 +241,7 @@ public function getFullPrettyVersion(bool $truncate = true, int $displayMode = P
*/
public function getStabilityPriority(): int
{
return self::$stabilities[$this->getStability()];
return self::STABILITIES[$this->getStability()];
}

public function __clone()
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Package/Loader/RootPackageLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ private function extractAliases(array $requires, array $aliases): array
*
* @param array<string, string> $requires
* @param array<string, int> $stabilityFlags
* @param key-of<BasePackage::STABILITIES> $minimumStability
*
* @return array<string, int>
*
Expand All @@ -235,8 +236,7 @@ private function extractAliases(array $requires, array $aliases): array
*/
public static function extractStabilityFlags(array $requires, string $minimumStability, array $stabilityFlags): array
{
$stabilities = BasePackage::$stabilities;
/** @var int $minimumStability */
$stabilities = BasePackage::STABILITIES;
$minimumStability = $stabilities[$minimumStability];
foreach ($requires as $reqName => $reqVersion) {
$constraints = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Package/Loader/ValidatingArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ public function load(array $config, string $class = 'Composer\Package\CompletePa
}

if ($this->validateString('minimum-stability') && isset($this->config['minimum-stability'])) {
if (!isset(BasePackage::$stabilities[strtolower($this->config['minimum-stability'])]) && $this->config['minimum-stability'] !== 'RC') {
$this->errors[] = 'minimum-stability : invalid value ('.$this->config['minimum-stability'].'), must be one of '.implode(', ', array_keys(BasePackage::$stabilities));
if (!isset(BasePackage::STABILITIES[strtolower($this->config['minimum-stability'])]) && $this->config['minimum-stability'] !== 'RC') {
$this->errors[] = 'minimum-stability : invalid value ('.$this->config['minimum-stability'].'), must be one of '.implode(', ', array_keys(BasePackage::STABILITIES));
unset($this->config['minimum-stability']);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Composer/Package/Locker.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ public function getPlatformRequirements(bool $withDevReqs = false): array
return $requirements;
}

/**
* @return key-of<BasePackage::STABILITIES>
*/
public function getMinimumStability(): string
{
$lockData = $this->getLockData();
Expand Down Expand Up @@ -431,7 +434,7 @@ private function lockPackages(array $packages): array

$spec = $this->dumper->dump($package);
unset($spec['version_normalized']);
// remove `transport-options.ssl` from lock file to prevent storing
// remove `transport-options.ssl` from lock file to prevent storing
// local-filesystem repo config paths in the lock file as that makes it less portable
if (isset($spec['transport-options']['ssl'])) {
unset($spec['transport-options']['ssl']);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Package/RootPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RootPackage extends CompletePackage implements RootPackageInterface
{
public const DEFAULT_PRETTY_VERSION = '1.0.0+no-version-set';

/** @var string */
/** @var key-of<BasePackage::STABILITIES> */
protected $minimumStability = 'stable';
/** @var bool */
protected $preferStable = false;
Expand Down
6 changes: 5 additions & 1 deletion src/Composer/Package/RootPackageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function getAliases(): array;

/**
* Returns the minimum stability of the package
*
* @return key-of<BasePackage::STABILITIES>
*/
public function getMinimumStability(): string;

Expand Down Expand Up @@ -120,12 +122,14 @@ public function setDevAutoload(array $devAutoload): void;
/**
* Set the stabilityFlags
*
* @param array<string, BasePackage::STABILITY_*> $stabilityFlags
* @phpstan-param array<string, BasePackage::STABILITY_*> $stabilityFlags
*/
public function setStabilityFlags(array $stabilityFlags): void;

/**
* Set the minimumStability
*
* @phpstan-param key-of<BasePackage::STABILITIES> $minimumStability
*/
public function setMinimumStability(string $minimumStability): void;

Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Package/Version/StabilityFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class StabilityFilter
* Checks if any of the provided package names in the given stability match the configured acceptable stability and flags
*
* @param int[] $acceptableStabilities array of stability => BasePackage::STABILITY_* value
* @phpstan-param array<string, BasePackage::STABILITY_*> $acceptableStabilities
* @phpstan-param array<key-of<BasePackage::STABILITIES>, BasePackage::STABILITY_*> $acceptableStabilities
* @param int[] $stabilityFlags an array of package name => BasePackage::STABILITY_* value
* @phpstan-param array<string, BasePackage::STABILITY_*> $stabilityFlags
* @param string[] $names The package name(s) to check for stability flags
* @param string $stability one of 'stable', 'RC', 'beta', 'alpha' or 'dev'
* @param key-of<BasePackage::STABILITIES> $stability one of 'stable', 'RC', 'beta', 'alpha' or 'dev'
* @return bool true if any package name is acceptable
*/
public static function isPackageAcceptable(array $acceptableStabilities, array $stabilityFlags, array $names, string $stability): bool
{
foreach ($names as $name) {
// allow if package matches the package-specific stability flag
if (isset($stabilityFlags[$name])) {
if (BasePackage::$stabilities[$stability] <= $stabilityFlags[$name]) {
if (BasePackage::STABILITIES[$stability] <= $stabilityFlags[$name]) {
return true;
}
} elseif (isset($acceptableStabilities[$stability])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Package/Version/VersionSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(RepositorySet $repositorySet, ?PlatformRepository $p
*/
public function findBestCandidate(string $packageName, ?string $targetPackageVersion = null, string $preferredStability = 'stable', $platformRequirementFilter = null, int $repoSetFlags = 0, ?IOInterface $io = null, $showWarnings = true)
{
if (!isset(BasePackage::$stabilities[$preferredStability])) {
if (!isset(BasePackage::STABILITIES[$preferredStability])) {
// If you get this, maybe you are still relying on the Composer 1.x signature where the 3rd arg was the php version
throw new \UnexpectedValueException('Expected a valid stability name as 3rd argument, got '.$preferredStability);
}
Expand All @@ -86,7 +86,7 @@ public function findBestCandidate(string $packageName, ?string $targetPackageVer
$constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null;
$candidates = $this->repositorySet->findPackages(strtolower($packageName), $constraint, $repoSetFlags);

$minPriority = BasePackage::$stabilities[$preferredStability];
$minPriority = BasePackage::STABILITIES[$preferredStability];
usort($candidates, static function (PackageInterface $a, PackageInterface $b) use ($minPriority) {
$aPriority = $a->getStabilityPriority();
$bPriority = $b->getStabilityPriority();
Expand Down
Loading

0 comments on commit 685add7

Please sign in to comment.