Skip to content

Commit 4dbefe4

Browse files
committed
Use RuleErrorBuilder and add method for checking case-insensitive constants support
1 parent 54366b7 commit 4dbefe4

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

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
}

src/Rules/Functions/DefineParametersRule.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\FuncCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Php\PhpVersion;
9+
use PHPStan\Rules\RuleErrorBuilder;
910

1011
/**
1112
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
@@ -30,7 +31,7 @@ public function processNode(Node $node, Scope $scope): array
3031
if (!($node->name instanceof \PhpParser\Node\Name)) {
3132
return [];
3233
}
33-
if ($this->phpVersion->getVersionId() < 80000) {
34+
if ($this->phpVersion->supportsCaseInsensitiveConstantNames()) {
3435
return [];
3536
}
3637
$name = strtolower((string) $node->name);
@@ -39,12 +40,14 @@ public function processNode(Node $node, Scope $scope): array
3940
}
4041
$args = $node->getArgs();
4142
$argsCount = count($args);
42-
// Expects 2, 1 arg is caught by CallToFunctionParametersRule
43+
// Expects 2 or 3, 1 arg is caught by CallToFunctionParametersRule
4344
if ($argsCount < 3) {
4445
return [];
4546
}
4647
return [
47-
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.',
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(),
4851
];
4952
}
5053

tests/PHPStan/Rules/Functions/DefineParametersRuleTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ protected function getRule(): \PHPStan\Rules\Rule
1818
public function testFile(): void
1919
{
2020
if (PHP_VERSION_ID < 80000) {
21-
$this->markTestSkipped('Test requires PHP 8.0.');
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+
]);
2229
}
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-
]);
2930
}
3031

3132
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<?php
22

33
define('CaSeInSeNsItIvE', 0, true);
4+
define('CaSeInSeNsItIvE_IsOK', 0);
5+
define('all_lowercase', 1);
6+
define('ALL_UPPERCASE', 1);

0 commit comments

Comments
 (0)