Skip to content

Commit af181bb

Browse files
committed
Don't crash on named arguments
1 parent 8dfba16 commit af181bb

4 files changed

Lines changed: 71 additions & 11 deletions

File tree

‎src/Rules/PHPUnit/AttributeVersionRequirementHelper.php‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc
8888
continue;
8989
}
9090

91+
// phpunit does not support named arguments.
92+
// we handle them here just to make sure we don't crash on them.
93+
$versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null;
94+
if (!is_string($versionRequirement)) {
95+
continue;
96+
}
97+
9198
$versionNode = $this->getFixableVersionNode(
9299
$attributeNodes[$attr->getName()][$attributeIndex] ?? null,
93-
$args,
100+
$versionRequirement,
94101
);
95102

96103
// the following block is mimicing PHPUnit version parsing
97104
// see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
98105

99-
// named arguments are handled just to make sure we don't crash on them.
100-
$versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null;
101-
if (!is_string($versionRequirement)) {
102-
continue;
103-
}
104-
105106
if ($this->warnAboutIncompleteVersion($versionRequirement)) {
106107
$errorBuilder = RuleErrorBuilder::message(
107108
sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement),
@@ -220,10 +221,8 @@ private function groupAttributeNodesByName(array $attrGroups): array
220221

221222
/**
222223
* Returns the string literal holding the version requirement, when it is a plain literal in the analysed file.
223-
*
224-
* @param array<int|string, mixed> $args
225224
*/
226-
private function getFixableVersionNode(?Node\Attribute $attributeNode, array $args): ?String_
225+
private function getFixableVersionNode(?Node\Attribute $attributeNode, string $versionRequirement): ?String_
227226
{
228227
if ($attributeNode === null || count($attributeNode->args) !== 1) {
229228
return null;
@@ -235,7 +234,7 @@ private function getFixableVersionNode(?Node\Attribute $attributeNode, array $ar
235234
}
236235

237236
// make sure the AST node is the one the reflection attribute was read from
238-
if ($versionNode->value !== ($args[0] ?? null)) {
237+
if ($versionNode->value !== $versionRequirement) {
239238
return null;
240239
}
241240

‎tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ public function testFixVersionWithoutOperator(): void
226226
$this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php.fixed');
227227
}
228228

229+
public function testNamedArgument(): void
230+
{
231+
$this->phpunitMajorVersion = 13;
232+
$this->phpunitMinorVersion = 0;
233+
$this->deprecationRulesInstalled = false;
234+
$this->warnAboutIncompleteVersion = true;
235+
236+
$this->analyse([__DIR__ . '/data/requires-php-version-named-argument.php'], [
237+
[
238+
'Version requirement 8.0 is incomplete. Expect a version composed of major, minor and patch.',
239+
12,
240+
],
241+
[
242+
'Version requirement 8.0 is missing operator.',
243+
12,
244+
],
245+
]);
246+
}
247+
248+
public function testFixNamedArgument(): void
249+
{
250+
$this->phpunitMajorVersion = 13;
251+
$this->phpunitMinorVersion = 0;
252+
$this->deprecationRulesInstalled = false;
253+
$this->warnAboutIncompleteVersion = true;
254+
255+
$this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php.fixed');
256+
}
257+
229258
protected function getRule(): Rule
230259
{
231260
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace RequiresPhpVersionNamedArgument;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\RequiresPhp;
7+
8+
class NamedArgument extends TestCase
9+
{
10+
// phpunit does not support named arguments.
11+
// we handle them here just to make sure we don't crash on them.
12+
#[RequiresPhp(versionRequirement: '8.0')]
13+
public function testFoo(): void {
14+
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace RequiresPhpVersionNamedArgument;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\RequiresPhp;
7+
8+
class NamedArgument extends TestCase
9+
{
10+
// phpunit does not support named arguments.
11+
// we handle them here just to make sure we don't crash on them.
12+
#[RequiresPhp(versionRequirement: '>=8.0.0')]
13+
public function testFoo(): void {
14+
15+
}
16+
}

0 commit comments

Comments
 (0)