|
5 | 5 | use PharIo\Version\UnsupportedVersionConstraintException; |
6 | 6 | use PharIo\Version\Version; |
7 | 7 | use PharIo\Version\VersionConstraintParser; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Scalar\String_; |
8 | 10 | use PHPStan\Analyser\Scope; |
9 | 11 | use PHPStan\BetterReflection\Reflection\ReflectionAttribute; |
10 | 12 | use PHPStan\Php\ConfiguredPhpVersionRangeHelper; |
|
13 | 15 | use PHPStan\Rules\RuleErrorBuilder; |
14 | 16 | use PHPStan\ShouldNotHappenException; |
15 | 17 | use function count; |
| 18 | +use function explode; |
| 19 | +use function implode; |
16 | 20 | use function is_numeric; |
17 | 21 | use function is_string; |
18 | 22 | use function preg_match; |
|
24 | 28 | final class AttributeVersionRequirementHelper |
25 | 29 | { |
26 | 30 |
|
| 31 | + /** |
| 32 | + * Matches requirements whose version can be completed to major.minor.patch |
| 33 | + * without changing their meaning, e.g. `>=8`, `>=8.1` or `^8.1`. |
| 34 | + * Versions without an operator are deliberately not matched. |
| 35 | + */ |
| 36 | + private const FIXABLE_VERSION_REQUIREMENT = '/^(?P<operator>>=|\^)(?P<space>\s*)(?P<version>\d+(?:\.\d+){0,2})$/'; |
| 37 | + |
27 | 38 | private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m"; |
28 | 39 |
|
29 | 40 | private PHPUnitVersion $PHPUnitVersion; |
@@ -59,34 +70,45 @@ public function __construct( |
59 | 70 |
|
60 | 71 | /** |
61 | 72 | * @param array<ReflectionAttribute> $attributes |
| 73 | + * @param array<Node\AttributeGroup> $attrGroups AST attribute groups of the node $attributes were read from |
62 | 74 | * |
63 | 75 | * @return list<IdentifierRuleError> |
64 | 76 | */ |
65 | | - public function checkVersionRequirement(array $attributes, Scope $scope): array |
| 77 | + public function checkVersionRequirement(array $attributes, array $attrGroups, Scope $scope): array |
66 | 78 | { |
67 | 79 | $errors = []; |
68 | 80 | $parser = new VersionConstraintParser(); |
| 81 | + $attributeNodes = $this->groupAttributeNodesByName($attrGroups); |
| 82 | + $attributeIndexes = []; |
69 | 83 | foreach ($attributes as $attr) { |
| 84 | + $attributeIndex = $attributeIndexes[$attr->getName()] ?? 0; |
| 85 | + $attributeIndexes[$attr->getName()] = $attributeIndex + 1; |
| 86 | + |
70 | 87 | $args = $attr->getArguments(); |
71 | 88 | if (count($args) !== 1) { |
72 | 89 | continue; |
73 | 90 | } |
74 | 91 |
|
75 | | - // the following block is mimicing PHPUnit version parsing |
76 | | - // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php |
77 | | - |
78 | | - // named arguments are handled just to make sure we don't crash on them. |
| 92 | + // phpunit does not support named arguments. |
| 93 | + // we handle them here just to make sure we don't crash on them. |
79 | 94 | $versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null; |
80 | 95 | if (!is_string($versionRequirement)) { |
81 | 96 | continue; |
82 | 97 | } |
83 | 98 |
|
| 99 | + // the following block is mimicing PHPUnit version parsing |
| 100 | + // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php |
| 101 | + |
84 | 102 | if ($this->warnAboutIncompleteVersion($versionRequirement)) { |
85 | | - $errors[] = RuleErrorBuilder::message( |
| 103 | + $errorBuilder = RuleErrorBuilder::message( |
86 | 104 | sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement), |
87 | 105 | ) |
88 | | - ->identifier('phpunit.attributeRequiresPhpVersion') |
89 | | - ->build(); |
| 106 | + ->identifier('phpunit.attributeRequiresPhpVersion'); |
| 107 | + $versionNode = $this->getFixableVersionNode( |
| 108 | + $attributeNodes[$attr->getName()][$attributeIndex] ?? null, |
| 109 | + $versionRequirement, |
| 110 | + ); |
| 111 | + $errors[] = $this->buildError($errorBuilder, $versionNode); |
90 | 112 | } |
91 | 113 |
|
92 | 114 | if ( |
@@ -180,6 +202,84 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array |
180 | 202 | return $errors; |
181 | 203 | } |
182 | 204 |
|
| 205 | + /** |
| 206 | + * @param array<Node\AttributeGroup> $attrGroups |
| 207 | + * |
| 208 | + * @return array<string, list<Node\Attribute>> |
| 209 | + */ |
| 210 | + private function groupAttributeNodesByName(array $attrGroups): array |
| 211 | + { |
| 212 | + $attributeNodes = []; |
| 213 | + foreach ($attrGroups as $attrGroup) { |
| 214 | + foreach ($attrGroup->attrs as $attributeNode) { |
| 215 | + $attributeNodes[$attributeNode->name->toString()][] = $attributeNode; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return $attributeNodes; |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Returns the string literal holding the version requirement, when it is a plain literal in the analysed file. |
| 224 | + */ |
| 225 | + private function getFixableVersionNode(?Node\Attribute $attributeNode, string $versionRequirement): ?String_ |
| 226 | + { |
| 227 | + if ($attributeNode === null || count($attributeNode->args) !== 1) { |
| 228 | + return null; |
| 229 | + } |
| 230 | + |
| 231 | + $versionNode = $attributeNode->args[0]->value; |
| 232 | + if (!$versionNode instanceof String_) { |
| 233 | + return null; |
| 234 | + } |
| 235 | + |
| 236 | + // make sure the AST node is the one the reflection attribute was read from |
| 237 | + if ($versionNode->value !== $versionRequirement) { |
| 238 | + return null; |
| 239 | + } |
| 240 | + |
| 241 | + return $versionNode; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @param RuleErrorBuilder<IdentifierRuleError> $errorBuilder |
| 246 | + */ |
| 247 | + private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNode): IdentifierRuleError |
| 248 | + { |
| 249 | + if ($versionNode === null) { |
| 250 | + return $errorBuilder->build(); |
| 251 | + } |
| 252 | + |
| 253 | + $fixedVersionRequirement = $this->fixVersionRequirement($versionNode->value); |
| 254 | + if ($fixedVersionRequirement === null) { |
| 255 | + return $errorBuilder->build(); |
| 256 | + } |
| 257 | + |
| 258 | + return $errorBuilder->fixNode( |
| 259 | + $versionNode, |
| 260 | + static fn (String_ $versionNode): String_ => new String_($fixedVersionRequirement, ['kind' => $versionNode->getAttribute('kind')]), |
| 261 | + )->build(); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Completes the version to major.minor.patch. |
| 266 | + * Only applied where the meaning of the requirement stays the same. |
| 267 | + * Versions without an operator are not fixed. |
| 268 | + */ |
| 269 | + private function fixVersionRequirement(string $versionRequirement): ?string |
| 270 | + { |
| 271 | + if (preg_match(self::FIXABLE_VERSION_REQUIREMENT, $versionRequirement, $matches) !== 1) { |
| 272 | + return null; |
| 273 | + } |
| 274 | + |
| 275 | + $versionParts = explode('.', $matches['version']); |
| 276 | + while (count($versionParts) < 3) { |
| 277 | + $versionParts[] = '0'; |
| 278 | + } |
| 279 | + |
| 280 | + return $matches['operator'] . $matches['space'] . implode('.', $versionParts); |
| 281 | + } |
| 282 | + |
183 | 283 | /** |
184 | 284 | * @return list<Version> |
185 | 285 | */ |
|
0 commit comments