Skip to content

Commit 9ca1122

Browse files
paulbalandanondrejmirtes
authored andcommitted
Introduce ClassAsClassConstantRule
1 parent 2016780 commit 9ca1122

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ lint:
5454
--exclude tests/PHPStan/Rules/Classes/data/first-class-instantiation-callable.php \
5555
--exclude tests/PHPStan/Rules/Classes/data/instantiation-callable.php \
5656
--exclude tests/PHPStan/Rules/Classes/data/bug-9402.php \
57+
--exclude tests/PHPStan/Rules/Constants/data/class-as-class-constant.php \
5758
--exclude tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-native-type.php \
5859
--exclude tests/PHPStan/Rules/Constants/data/overriding-constant-native-types.php \
5960
--exclude tests/PHPStan/Rules/Methods/data/bug-10043.php \

conf/config.level0.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ rules:
6868
- PHPStan\Rules\Classes\NonClassAttributeClassRule
6969
- PHPStan\Rules\Classes\ReadOnlyClassRule
7070
- PHPStan\Rules\Classes\TraitAttributeClassRule
71+
- PHPStan\Rules\Constants\ClassAsClassConstantRule
7172
- PHPStan\Rules\Constants\DynamicClassConstantFetchRule
7273
- PHPStan\Rules\Constants\FinalConstantRule
7374
- PHPStan\Rules\Constants\NativeTypedClassConstantRule
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Constants;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Rules\Rule;
8+
use PHPStan\Rules\RuleErrorBuilder;
9+
10+
/**
11+
* @implements Rule<Node\Stmt\ClassConst>
12+
*/
13+
final class ClassAsClassConstantRule implements Rule
14+
{
15+
16+
public function getNodeType(): string
17+
{
18+
return Node\Stmt\ClassConst::class;
19+
}
20+
21+
public function processNode(Node $node, Scope $scope): array
22+
{
23+
$errors = [];
24+
25+
foreach ($node->consts as $const) {
26+
if ($const->name->toLowerString() !== 'class') {
27+
continue;
28+
}
29+
30+
$errors[] = RuleErrorBuilder::message('A class constant must not be called \'class\'; it is reserved for class name fetching.')
31+
->line($const->getStartLine())
32+
->identifier('classConstant.class')
33+
->nonIgnorable()
34+
->build();
35+
}
36+
37+
return $errors;
38+
}
39+
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Constants;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
8+
/**
9+
* @extends RuleTestCase<ClassAsClassConstantRule>
10+
*/
11+
class ClassAsClassConstantRuleTest extends RuleTestCase
12+
{
13+
14+
protected function getRule(): Rule
15+
{
16+
return new ClassAsClassConstantRule();
17+
}
18+
19+
public function testRule(): void
20+
{
21+
$this->analyse([__DIR__ . '/data/class-as-class-constant.php'], [
22+
[
23+
'A class constant must not be called \'class\'; it is reserved for class name fetching.',
24+
9,
25+
],
26+
[
27+
'A class constant must not be called \'class\'; it is reserved for class name fetching.',
28+
16,
29+
],
30+
]);
31+
}
32+
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ClassAsClassConstant;
4+
5+
final class A
6+
{
7+
public const FOO = 'foo';
8+
public const BAR = 'bar';
9+
public const CLASS = 'qux';
10+
}
11+
12+
final class B
13+
{
14+
public const YES = 1,
15+
NO = 0,
16+
CLASS = -1;
17+
}

0 commit comments

Comments
 (0)