Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/DependencyResolver/DetermineMinimumStability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Php\Pie\DependencyResolver;

use Composer\Semver\VersionParser;
use Webmozart\Assert\Assert;

/**
* Utility to extract a valid Stability (see {@see \Composer\Package\BasePackage::$stabilities}) from a requested
* package version in a predictable way.
*
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
*/
final class DetermineMinimumStability
{
private const STABILITY_STABLE = 'stable';
private const STABILITY_RC = 'RC';
private const STABILITY_BETA = 'beta';
private const STABILITY_ALPHA = 'alpha';
private const STABILITY_DEV = 'dev';

private const DEFAULT_MINIMUM_STABILITY = self::STABILITY_STABLE;

/** @psalm-assert self::STABILITY_* $stability */
private static function assertValidStabilityString(string $stability): void
{
Assert::oneOf(
$stability,
[
self::STABILITY_STABLE,
self::STABILITY_RC,
self::STABILITY_BETA,
self::STABILITY_ALPHA,
self::STABILITY_DEV,
],
);
}

/** @return self::STABILITY_* */
public static function fromRequestedVersion(string|null $requestedVersion): string
{
if ($requestedVersion === null) {
return self::DEFAULT_MINIMUM_STABILITY;
}

$stability = VersionParser::parseStability($requestedVersion);
self::assertValidStabilityString($stability);

return $stability;
}
}
17 changes: 1 addition & 16 deletions src/DependencyResolver/ResolveDependencyWithComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Php\Pie\Platform\TargetPlatform;

use function preg_match;
use function str_starts_with;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class ResolveDependencyWithComposer implements DependencyResolver
Expand All @@ -27,21 +26,7 @@ public function __construct(

private function factoryRepositorySet(string|null $requestedVersion): RepositorySet
{
$minimumStability = 'stable';

/** Stability options from {@see https://getcomposer.org/doc/04-schema.md#minimum-stability} */
if ($requestedVersion !== null) {
if (preg_match('#@(dev|alpha|beta|RC|stable)$#', $requestedVersion, $matches)) {
$minimumStability = $matches[1];
}

// If a specific stability was not requested, but the version requested was `dev-` something, change to dev min stability
if (! $matches && str_starts_with($requestedVersion, 'dev-')) {
$minimumStability = 'dev';
}
}

$repositorySet = new RepositorySet($minimumStability);
$repositorySet = new RepositorySet(DetermineMinimumStability::fromRequestedVersion($requestedVersion));
$repositorySet->addRepository(new CompositeRepository($this->composer->getRepositoryManager()->getRepositories()));

return $repositorySet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static function validVersionsList(): array
['~1.0@beta', '1.0.1', self::DOWNLOAD_URL_1_0_1],
['dev-main', 'dev-main', self::DOWNLOAD_URL_ANY],
['dev-main#769f906413d6d1e12152f6d34134cbcd347ca253', 'dev-main', self::DOWNLOAD_URL_1_0_1],
['v1.x-dev', 'v1.x-dev', self::DOWNLOAD_URL_1_1_0_BETA_1],
];

return array_combine(
Expand Down
61 changes: 61 additions & 0 deletions test/unit/DependencyResolver/DetermineMinimumStabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Php\PieUnitTest\DependencyResolver;

use Php\Pie\DependencyResolver\DetermineMinimumStability;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function array_combine;
use function array_map;

#[CoversClass(DetermineMinimumStability::class)]
final class DetermineMinimumStabilityTest extends TestCase
{
/**
* @return array<string, array{0: non-empty-string|null, 1: non-empty-string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function requestedVersionToStabilityProvider(): array
{
$providerCases = [
[null, 'stable'],
['1.2.3', 'stable'],
['1.2.3@stable', 'stable'],
['1.2.3@RC', 'RC'],
['1.2.3@rc', 'RC'],
['1.2.3@beta', 'beta'],
['1.2.3@alpha', 'alpha'],
['1.2.3@dev', 'dev'],
['*@stable', 'stable'],
['*@RC', 'RC'],
['*@rc', 'RC'],
['*@beta', 'beta'],
['*@alpha', 'alpha'],
['*@dev', 'dev'],
['dev-main', 'dev'],
['v1.x-dev', 'dev'],
];

return array_combine(
array_map(
static fn (array $case) => $case[0] ?? 'null',
$providerCases,
),
$providerCases,
);
}

#[DataProvider('requestedVersionToStabilityProvider')]
public function testFromRequestedVersion(string|null $requestedVersion, string $expectedStability): void
{
self::assertSame(
$expectedStability,
DetermineMinimumStability::fromRequestedVersion($requestedVersion),
);
}
}