Skip to content

Commit f4a0b7b

Browse files
authored
Don't crash analysis on wrongly typed argument values (#365)
* Don't crash analysis on wrongly typed argument values * remove useless comments * fix build
1 parent df82e59 commit f4a0b7b

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎src/Rules/PHPUnit/AttributeVersionRequirementHelper.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\ShouldNotHappenException;
1515
use function count;
1616
use function is_numeric;
17+
use function is_string;
1718
use function preg_match;
1819
use function sprintf;
1920
use function strpos;
@@ -74,7 +75,11 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
7475
// the following block is mimicing PHPUnit version parsing
7576
// see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
7677

77-
$versionRequirement = $args[0];
78+
// named arguments are handled just to make sure we don't crash on them.
79+
$versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null;
80+
if (!is_string($versionRequirement)) {
81+
continue;
82+
}
7883

7984
if ($this->warnAboutIncompleteVersion($versionRequirement)) {
8085
$errors[] = RuleErrorBuilder::message(

‎tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ public function testWarnAboutIncompletePhpunitVersion(): void
192192
]);
193193
}
194194

195+
public function testNonStringArguments(): void
196+
{
197+
$this->phpunitMajorVersion = 12;
198+
$this->phpunitMinorVersion = 5;
199+
$this->deprecationRulesInstalled = false;
200+
$this->warnAboutIncompleteVersion = true;
201+
202+
$this->analyse([__DIR__ . '/data/requires-php-version-non-string.php'], [
203+
[
204+
'Version requirement >=8.0 is incomplete. Expect a version composed of major, minor and patch.',
205+
35,
206+
],
207+
]);
208+
}
209+
195210
protected function getRule(): Rule
196211
{
197212
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);

‎tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ public function testWarnAboutIncompletePhpunitVersion(): void
5353
]);
5454
}
5555

56+
public function testNonStringArguments(): void
57+
{
58+
$this->phpunitMajorVersion = 12;
59+
$this->phpunitMinorVersion = 5;
60+
$this->warnAboutIncompleteVersion = true;
61+
62+
$this->analyse([__DIR__ . '/data/requires-php-version-non-string.php'], [
63+
[
64+
'Version requirement >=11.0 is incomplete. Expect a version composed of major, minor and patch.',
65+
57,
66+
],
67+
]);
68+
}
69+
5670
protected function getRule(): Rule
5771
{
5872
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace RequiresPhpVersionNonString;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\RequiresPhp;
7+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
8+
9+
class IntArgument extends TestCase
10+
{
11+
#[RequiresPhp(8)]
12+
public function testFoo(): void {
13+
14+
}
15+
}
16+
17+
class FloatArgument extends TestCase
18+
{
19+
#[RequiresPhp(8.0)]
20+
public function testFoo(): void {
21+
22+
}
23+
}
24+
25+
class ArrayArgument extends TestCase
26+
{
27+
#[RequiresPhp(['8.0'])]
28+
public function testFoo(): void {
29+
30+
}
31+
}
32+
33+
class NamedArgument extends TestCase
34+
{
35+
#[RequiresPhp(versionRequirement: '>=8.0')]
36+
public function testFoo(): void {
37+
38+
}
39+
}
40+
41+
class PhpunitIntArgument extends TestCase
42+
{
43+
#[RequiresPhpunit(11)]
44+
public function testFoo(): void {
45+
46+
}
47+
}
48+
49+
#[RequiresPhp(8)]
50+
class ClassIntArgument extends TestCase
51+
{
52+
public function testFoo(): void {
53+
54+
}
55+
}
56+
57+
#[RequiresPhpunit(versionRequirement: '>=11.0')]
58+
class ClassNamedArgument extends TestCase
59+
{
60+
public function testFoo(): void {
61+
62+
}
63+
}

0 commit comments

Comments
 (0)