forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeVersionRequirementHelper.php
More file actions
199 lines (168 loc) · 5.61 KB
/
Copy pathAttributeVersionRequirementHelper.php
File metadata and controls
199 lines (168 loc) · 5.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PharIo\Version\UnsupportedVersionConstraintException;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraintParser;
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 function count;
use function is_numeric;
use function preg_match;
use function sprintf;
use function substr_count;
use function version_compare;
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 = [];
$parser = new VersionConstraintParser();
foreach ($attributes as $attr) {
$args = $attr->getArguments();
if (count($args) !== 1) {
continue;
}
// the following block is mimicing PHPUnit version parsing
// see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
$versionRequirement = $args[0];
if ($this->warnAboutIncompleteVersion($versionRequirement)) {
$errors[] = RuleErrorBuilder::message(
sprintf('Version requirement is incomplete.'),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
}
if (
!is_numeric($versionRequirement)
) {
if (!$this->bleedingEdge) {
continue;
}
$pharIoVersions = strpos($attr->getName(), 'RequiresPhpunit') !== false
? $this->PHPUnitVersion->getPharIoVersions()
: $this->getAnalyzedPhpVersions();
if ($pharIoVersions === []) {
continue;
}
try {
// check composer like version constraints, e.g. ^1 or ~2
$testPhpVersionConstraint = $parser->parse($versionRequirement);
foreach ($pharIoVersions as $pharIoVersion) {
if ($testPhpVersionConstraint->complies($pharIoVersion)) {
// one of the versions within range matched, check next attribute
continue 2;
}
}
} catch (UnsupportedVersionConstraintException $e) {
// test php-src builtin operators as in version_compare()
if (preg_match(self::VERSION_COMPARISON, $versionRequirement, $matches) <= 0) {
$errors[] = RuleErrorBuilder::message(
sprintf($e->getMessage()),
)
->identifier('phpunit.attributeRequiresPhpVersion')
->build();
continue;
}
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
foreach ($pharIoVersions as $pharIoVersion) {
if (version_compare($pharIoVersion->getVersionString(), $matches['version'], $operator)) {
// 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();
continue;
}
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();
}
}
return $errors;
}
/**
* @return Version[]
*/
private function getAnalyzedPhpVersions(): array
{
[$minVersion, $maxVersion] = $this->phpVersionRangeHelper->getVersionRange();
if ($minVersion !== null && $maxVersion !== null) {
$versions = [];
$minorVersionIterator = new PhpMinorVersionIterator(
$minVersion,
$maxVersion,
);
foreach ($minorVersionIterator as $phpstanVersion) {
$versions[] = new Version($phpstanVersion->getVersionString());
}
return $versions;
}
return [];
}
// see https://github.com/sebastianbergmann/phpunit/issues/6451
private function warnAboutIncompleteVersion(string $versionRequirement): bool
{
if (!$this->bleedingEdge) {
return false;
}
if (!$this->warnAboutIncompleteVersion) {
return false;
}
if (!$this->PHPUnitVersion->warnsAboutIncompleteVersion()->yes()) {
return false;
}
return substr_count($versionRequirement, '.') !== 2;
}
}