Skip to content

Commit 732d033

Browse files
authored
define: case_insensitive is ignored, case-insensitive constants are no longer supported
1 parent ba16285 commit 732d033

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
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

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,9 @@ public function supportsEnums(): bool
147147
return $this->versionId >= 80100;
148148
}
149149

150+
public function supportsCaseInsensitiveConstantNames(): bool
151+
{
152+
return $this->versionId < 80000;
153+
}
154+
150155
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
/**
12+
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
13+
*/
14+
class DefineParametersRule implements \PHPStan\Rules\Rule
15+
{
16+
17+
private PhpVersion $phpVersion;
18+
19+
public function __construct(PhpVersion $phpVersion)
20+
{
21+
$this->phpVersion = $phpVersion;
22+
}
23+
24+
public function getNodeType(): string
25+
{
26+
return FuncCall::class;
27+
}
28+
29+
public function processNode(Node $node, Scope $scope): array
30+
{
31+
if (!($node->name instanceof \PhpParser\Node\Name)) {
32+
return [];
33+
}
34+
if ($this->phpVersion->supportsCaseInsensitiveConstantNames()) {
35+
return [];
36+
}
37+
$name = strtolower((string) $node->name);
38+
if ($name !== 'define') {
39+
return [];
40+
}
41+
$args = $node->getArgs();
42+
$argsCount = count($args);
43+
// Expects 2 or 3, 1 arg is caught by CallToFunctionParametersRule
44+
if ($argsCount < 3) {
45+
return [];
46+
}
47+
return [
48+
RuleErrorBuilder::message(
49+
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.'
50+
)->line($node->getLine())->build(),
51+
];
52+
}
53+
54+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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->analyse([__DIR__ . '/data/call-to-define.php'], []);
22+
} else {
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+
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
define('CaSeInSeNsItIvE', 0, true);
4+
define('CaSeInSeNsItIvE_IsOK', 0);
5+
define('all_lowercase', 1);
6+
define('ALL_UPPERCASE', 1);

0 commit comments

Comments
 (0)