Repository navigation
Expand file tree
/
Copy pathAttributeVersionRequirementHelper.php
More file actions
164 lines (139 loc) · 4.49 KB
/
Copy pathAttributeVersionRequirementHelper.php
File metadata and controls
164 lines (139 loc) · 4.49 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflection\ReflectionAttribute;
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
use PHPStan\Php\PhpMinorVersionIterator;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use SebastianBergmann\VersionRequirement\InvalidVersionOperatorException;
use SebastianBergmann\VersionRequirement\InvalidVersionRequirementException;
use SebastianBergmann\VersionRequirement\Requirement;
use function count;
use function sprintf;
use function strpos;
final class AttributeVersionRequirementHelper
{
private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m";
private PHPUnitVersion $PHPUnitVersion;
/**
* When phpstan-deprecation-rules is installed, rule reports deprecated usages.
*/
private bool $deprecationRulesInstalled;
/**
* Whether warnings about incomplete versions are allowed to be emitted
*/
private bool $warnAboutIncompleteVersion;
private bool $bleedingEdge;
private ConfiguredPhpVersionRangeHelper $phpVersionRangeHelper;
public function __construct(
PHPUnitVersion $PHPUnitVersion,
ConfiguredPhpVersionRangeHelper $phpVersionRangeHelper,
bool $deprecationRulesInstalled = false,
bool $bleedingEdge = false,
bool $warnAboutIncompleteVersion = true
)
{
$this->PHPUnitVersion = $PHPUnitVersion;
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
$this->warnAboutIncompleteVersion = $warnAboutIncompleteVersion;
$this->bleedingEdge = $bleedingEdge;
$this->phpVersionRangeHelper = $phpVersionRangeHelper;
}
/**
* @param array<ReflectionAttribute> $attributes
*
* @return list<IdentifierRuleError>
*/
public function checkVersionRequirement(array $attributes, Scope $scope): array
{
$errors = [];
foreach ($attributes as $attr) {
$args = $attr->getArguments();
if (count($args) !== 1) {
continue;
}
$versionRequirement = $args[0];
try {
$requirement = Requirement::from($versionRequirement);
} catch (InvalidVersionOperatorException $e) {
if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement is missing operator.'),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
} elseif (
$this->deprecationRulesInstalled
&& $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes()
) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement without operator is deprecated.'),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
}
continue;
} catch (InvalidVersionRequirementException $e) {
$errors[] = RuleErrorBuilder::message(
sprintf($e->getMessage()),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
continue;
}
if (!$requirement->isComplete()) {
if (!$this->warnAboutIncompleteVersion) {
continue;
}
if (!$this->PHPUnitVersion->warnsAboutIncompleteVersion()->yes()) {
continue;
}
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement is incomplete.'),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
continue;
}
$versionStrings = strpos($attr->getName(), 'RequiresPhpunit') !== false
? $this->PHPUnitVersion->getMinMaxVersion()
: $this->getAnalyzedPhpVersions();
if ($versionStrings === []) {
continue;
}
foreach ($versionStrings as $versionString) {
if ($requirement->isSatisfiedBy($versionString)) {
// one of the versions within range matched, check next attribute
continue 2;
}
}
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement will always evaluate to false.'),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
}
return $errors;
}
/**
* @return list<string>
*/
private function getAnalyzedPhpVersions(): array
{
// @phpstan-ignore phpstanApi.method
[$minVersion, $maxVersion] = $this->phpVersionRangeHelper->getVersionRange();
if ($minVersion !== null && $maxVersion !== null) {
$versions = [];
$minorVersionIterator = new PhpMinorVersionIterator(
$minVersion,
$maxVersion,
);
foreach ($minorVersionIterator as $phpstanVersion) {
$versions[] = $phpstanVersion->getVersionString();
}
return $versions;
}
return [];
}
}