Skip to content

Commit 8c77529

Browse files
committed
Don't crash on named arguments
1 parent 76fd68f commit 8c77529

4 files changed

Lines changed: 72 additions & 7 deletions

File tree

‎src/Rules/PHPUnit/AttributeVersionRequirementHelper.php‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function explode;
1919
use function implode;
2020
use function is_numeric;
21+
use function is_string;
2122
use function preg_match;
2223
use function sprintf;
2324
use function strpos;
@@ -87,16 +88,21 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc
8788
continue;
8889
}
8990

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+
9098
$versionNode = $this->getFixableVersionNode(
9199
$attributeNodes[$attr->getName()][$attributeIndex] ?? null,
92-
$args,
100+
$versionRequirement,
93101
);
94102

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

98-
$versionRequirement = $args[0];
99-
100106
if ($this->warnAboutIncompleteVersion($versionRequirement)) {
101107
$errorBuilder = RuleErrorBuilder::message(
102108
sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement),
@@ -215,10 +221,8 @@ private function groupAttributeNodesByName(array $attrGroups): array
215221

216222
/**
217223
* Returns the string literal holding the version requirement, when it is a plain literal in the analysed file.
218-
*
219-
* @param array<int|string, mixed> $args
220224
*/
221-
private function getFixableVersionNode(?Node\Attribute $attributeNode, array $args): ?String_
225+
private function getFixableVersionNode(?Node\Attribute $attributeNode, string $versionRequirement): ?String_
222226
{
223227
if ($attributeNode === null || count($attributeNode->args) !== 1) {
224228
return null;
@@ -230,7 +234,7 @@ private function getFixableVersionNode(?Node\Attribute $attributeNode, array $ar
230234
}
231235

232236
// make sure the AST node is the one the reflection attribute was read from
233-
if ($versionNode->value !== ($args[0] ?? null)) {
237+
if ($versionNode->value !== $versionRequirement) {
234238
return null;
235239
}
236240

‎tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php‎

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

214+
public function testNamedArgument(): void
215+
{
216+
$this->phpunitMajorVersion = 13;
217+
$this->phpunitMinorVersion = 0;
218+
$this->deprecationRulesInstalled = false;
219+
$this->warnAboutIncompleteVersion = true;
220+
221+
$this->analyse([__DIR__ . '/data/requires-php-version-named-argument.php'], [
222+
[
223+
'Version requirement 8.0 is incomplete. Expect a version composed of major, minor and patch.',
224+
12,
225+
],
226+
[
227+
'Version requirement 8.0 is missing operator.',
228+
12,
229+
],
230+
]);
231+
}
232+
233+
public function testFixNamedArgument(): void
234+
{
235+
$this->phpunitMajorVersion = 13;
236+
$this->phpunitMinorVersion = 0;
237+
$this->deprecationRulesInstalled = false;
238+
$this->warnAboutIncompleteVersion = true;
239+
240+
$this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php.fixed');
241+
}
242+
214243
protected function getRule(): Rule
215244
{
216245
$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)