Skip to content

Commit 54366b7

Browse files
committed
DefineParametersRule for checking deprecated arg 3
1 parent 41a3e66 commit 54366b7

File tree

4 files changed

+83
-13
lines changed

4 files changed

+83
-13
lines changed

conf/config.level0.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ rules:
3636
- PHPStan\Rules\Functions\ArrowFunctionReturnNullsafeByRefRule
3737
- PHPStan\Rules\Functions\CallToFunctionParametersRule
3838
- PHPStan\Rules\Functions\ClosureAttributesRule
39+
- PHPStan\Rules\Functions\DefineParametersRule
3940
- PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule
4041
- PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule
4142
- PHPStan\Rules\Functions\ExistingClassesInTypehintsRule
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Functions;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Php\PhpVersion;
9+
10+
/**
11+
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
12+
*/
13+
class DefineParametersRule implements \PHPStan\Rules\Rule
14+
{
15+
16+
private PhpVersion $phpVersion;
17+
18+
public function __construct(PhpVersion $phpVersion)
19+
{
20+
$this->phpVersion = $phpVersion;
21+
}
22+
23+
public function getNodeType(): string
24+
{
25+
return FuncCall::class;
26+
}
27+
28+
public function processNode(Node $node, Scope $scope): array
29+
{
30+
if (!($node->name instanceof \PhpParser\Node\Name)) {
31+
return [];
32+
}
33+
if ($this->phpVersion->getVersionId() < 80000) {
34+
return [];
35+
}
36+
$name = strtolower((string) $node->name);
37+
if ($name !== 'define') {
38+
return [];
39+
}
40+
$args = $node->getArgs();
41+
$argsCount = count($args);
42+
// Expects 2, 1 arg is caught by CallToFunctionParametersRule
43+
if ($argsCount < 3) {
44+
return [];
45+
}
46+
return [
47+
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.',
48+
];
49+
}
50+
51+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,6 @@ public function testCallToArrayUnique(): void
131131
]);
132132
}
133133

134-
public function testCallToDefineCaseInsenstive(): void
135-
{
136-
if (PHP_VERSION_ID < 80000) {
137-
$this->markTestSkipped('Test requires PHP 8.0.');
138-
}
139-
$this->analyse([__DIR__ . '/data/call-to-define.php'], [
140-
[
141-
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported',
142-
3,
143-
],
144-
]);
145-
}
146-
147134
public function testCallToArrayMapVariadic(): void
148135
{
149136
$this->analyse([__DIR__ . '/data/call-to-array-map-unique.php'], []);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Functions;
4+
5+
use PHPStan\Php\PhpVersion;
6+
7+
/**
8+
* @extends \PHPStan\Testing\RuleTestCase<DefineParametersRule>
9+
*/
10+
class DefineParametersRuleTest extends \PHPStan\Testing\RuleTestCase
11+
{
12+
13+
protected function getRule(): \PHPStan\Rules\Rule
14+
{
15+
return new DefineParametersRule(new PhpVersion(PHP_VERSION_ID));
16+
}
17+
18+
public function testFile(): void
19+
{
20+
if (PHP_VERSION_ID < 80000) {
21+
$this->markTestSkipped('Test requires PHP 8.0.');
22+
}
23+
$this->analyse([__DIR__ . '/data/call-to-define.php'], [
24+
[
25+
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.',
26+
3,
27+
],
28+
]);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)