Repository navigation
Expand file tree
/
Copy pathComposerTriggeredSet.php
More file actions
90 lines (76 loc) · 2.61 KB
/
Copy pathComposerTriggeredSet.php
File metadata and controls
90 lines (76 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace Rector\Set\ValueObject;
use Composer\Semver\Semver;
use Nette\Utils\Strings;
use Rector\Composer\ValueObject\InstalledPackage;
use Rector\Set\Contract\SetInterface;
use Webmozart\Assert\Assert;
/**
* @api used by extensions
*
* @deprecated Bond the rules themselves instead, by implementing the ComposerPackageConstraintInterface. A set
* triggered on a single major version has to be repeated for every version an upgrade passes through, while a bonded
* rule states the exact package version its target API is available from and applies from there upwards.
*
* @see \Rector\VersionBonding\Contract\ComposerPackageConstraintInterface
* @see https://github.com/rectorphp/rector-src/pull/8296
*
* @see \Rector\Tests\Set\ValueObject\ComposerTriggeredSetTest
*/
final readonly class ComposerTriggeredSet implements SetInterface
{
/**
* @see https://regex101.com/r/ioYomu/1
*/
private const string PACKAGE_REGEX = '#^[a-z0-9-]+\/([a-z0-9-_]+|\*)$#';
/**
* A bare "10.0" version, that is turned into a "^10.0" constraint
*
* @see https://regex101.com/r/vTJXPU/1
*/
private const string BARE_VERSION_REGEX = '#^\d+(\.\d+)*$#';
public function __construct(
private string $groupName,
private string $packageName,
private string $version,
private string $setFilePath
) {
Assert::regex($this->packageName, self::PACKAGE_REGEX);
Assert::fileExists($setFilePath);
}
public function getGroupName(): string
{
return $this->groupName;
}
public function getSetFilePath(): string
{
return $this->setFilePath;
}
/**
* @param array<string, InstalledPackage> $installedPackages
*/
public function matchInstalledPackages(array $installedPackages): bool
{
$package = $installedPackages[$this->packageName] ?? null;
if (! $package instanceof InstalledPackage) {
return false;
}
return Semver::satisfies($package->getVersion(), $this->resolveVersionConstraint());
}
public function getName(): string
{
return $this->packageName . ' ' . $this->version;
}
/**
* A bare version means "this major version", e.g. "10.0" is "^10.0". Anything else is used as is,
* to allow a set that spans multiple major versions, e.g. ">=10.0" or ">=10.0 <13.0".
*/
private function resolveVersionConstraint(): string
{
if (Strings::match($this->version, self::BARE_VERSION_REGEX) !== null) {
return '^' . $this->version;
}
return $this->version;
}
}