forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposerPackageConstraintFilter.php
More file actions
53 lines (43 loc) · 1.48 KB
/
ComposerPackageConstraintFilter.php
File metadata and controls
53 lines (43 loc) · 1.48 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
<?php
declare(strict_types=1);
namespace Rector\VersionBonding;
use Composer\Semver\Semver;
use Rector\Composer\InstalledPackageResolver;
use Rector\Contract\Rector\RectorInterface;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
final readonly class ComposerPackageConstraintFilter
{
public function __construct(
private InstalledPackageResolver $installedPackageResolver,
) {
}
/**
* @param list<RectorInterface> $rectors
* @return list<RectorInterface>
*/
public function filter(array $rectors): array
{
$activeRectors = [];
foreach ($rectors as $rector) {
if (! $rector instanceof ComposerPackageConstraintInterface) {
$activeRectors[] = $rector;
continue;
}
if ($this->satisfiesComposerPackageConstraint($rector)) {
$activeRectors[] = $rector;
}
}
return $activeRectors;
}
private function satisfiesComposerPackageConstraint(ComposerPackageConstraintInterface $rector): bool
{
$composerPackageConstraint = $rector->provideComposerPackageConstraint();
$packageVersion = $this->installedPackageResolver->resolvePackageVersion(
$composerPackageConstraint->getPackageName(),
);
if ($packageVersion === null) {
return false;
}
return Semver::satisfies($packageVersion, $composerPackageConstraint->getConstraint());
}
}