Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ lint:
--exclude tests/PHPStan/Rules/Classes/data/first-class-instantiation-callable.php \
--exclude tests/PHPStan/Rules/Classes/data/instantiation-callable.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-9402.php \
--exclude tests/PHPStan/Rules/Constants/data/class-as-class-constant.php \
--exclude tests/PHPStan/Rules/Constants/data/value-assigned-to-class-constant-native-type.php \
--exclude tests/PHPStan/Rules/Constants/data/overriding-constant-native-types.php \
--exclude tests/PHPStan/Rules/Methods/data/bug-10043.php \
Expand Down
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ rules:
- PHPStan\Rules\Classes\NonClassAttributeClassRule
- PHPStan\Rules\Classes\ReadOnlyClassRule
- PHPStan\Rules\Classes\TraitAttributeClassRule
- PHPStan\Rules\Constants\ClassAsClassConstantRule
- PHPStan\Rules\Constants\DynamicClassConstantFetchRule
- PHPStan\Rules\Constants\FinalConstantRule
- PHPStan\Rules\Constants\NativeTypedClassConstantRule
Expand Down
40 changes: 40 additions & 0 deletions src/Rules/Constants/ClassAsClassConstantRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Constants;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node\Stmt\ClassConst>
*/
final class ClassAsClassConstantRule implements Rule
{

public function getNodeType(): string
{
return Node\Stmt\ClassConst::class;
}

public function processNode(Node $node, Scope $scope): array
{
$errors = [];

foreach ($node->consts as $const) {
if ($const->name->toLowerString() !== 'class') {
continue;
}

$errors[] = RuleErrorBuilder::message('A class constant must not be called \'class\'; it is reserved for class name fetching.')
->line($const->getStartLine())
->identifier('classConstant.class')
->nonIgnorable()
->build();
}

return $errors;
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Constants/ClassAsClassConstantRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Constants;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<ClassAsClassConstantRule>
*/
class ClassAsClassConstantRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new ClassAsClassConstantRule();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/class-as-class-constant.php'], [
[
'A class constant must not be called \'class\'; it is reserved for class name fetching.',
9,
],
[
'A class constant must not be called \'class\'; it is reserved for class name fetching.',
16,
],
]);
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Constants/data/class-as-class-constant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace ClassAsClassConstant;

final class A
{
public const FOO = 'foo';
public const BAR = 'bar';
public const CLASS = 'qux';
}

final class B
{
public const YES = 1,
NO = 0,
CLASS = -1;
}
Loading