Skip to content

Commit 6be077e

Browse files
committed
Final constant rule
1 parent 270326a commit 6be077e

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

conf/config.level0.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ rules:
5050
- PHPStan\Rules\Classes\NewStaticRule
5151
- PHPStan\Rules\Classes\NonClassAttributeClassRule
5252
- PHPStan\Rules\Classes\TraitAttributeClassRule
53+
- PHPStan\Rules\Constants\FinalConstantRule
5354
- PHPStan\Rules\Exceptions\ThrowExpressionRule
5455
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule
5556
- PHPStan\Rules\Functions\ArrowFunctionReturnNullsafeByRefRule

src/Php/PhpVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ public function isInterfaceConstantImplicitlyFinal(): bool
132132
return $this->versionId < 80100;
133133
}
134134

135+
public function supportsFinalConstants(): bool
136+
{
137+
return $this->versionId >= 80100;
138+
}
139+
135140
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Constants;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Stmt\ClassConst;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Php\PhpVersion;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
12+
/** @implements Rule<ClassConst> */
13+
class FinalConstantRule implements 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 ClassConst::class;
26+
}
27+
28+
public function processNode(Node $node, Scope $scope): array
29+
{
30+
if (!$node->isFinal()) {
31+
return [];
32+
}
33+
34+
if ($this->phpVersion->supportsFinalConstants()) {
35+
return [];
36+
}
37+
38+
return [
39+
RuleErrorBuilder::message('Final class constants are supported only on PHP 8.1 and later.')->nonIgnorable()->build(),
40+
];
41+
}
42+
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Constants;
4+
5+
use PHPStan\Php\PhpVersion;
6+
use PHPStan\Rules\Rule;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends RuleTestCase<FinalConstantRule>
11+
*/
12+
class FinalConstantRuleTest extends RuleTestCase
13+
{
14+
15+
/** @var int */
16+
private $phpVersionId;
17+
18+
protected function getRule(): Rule
19+
{
20+
return new FinalConstantRule(new PhpVersion($this->phpVersionId));
21+
}
22+
23+
public function dataRule(): array
24+
{
25+
return [
26+
[
27+
80000,
28+
[
29+
[
30+
'Final class constants are supported only on PHP 8.1 and later.',
31+
9,
32+
],
33+
],
34+
],
35+
[
36+
80100,
37+
[],
38+
],
39+
];
40+
}
41+
42+
/**
43+
* @dataProvider dataRule
44+
* @param int $phpVersionId
45+
* @param mixed[] $errors
46+
*/
47+
public function testRule(int $phpVersionId, array $errors): void
48+
{
49+
if (!self::$useStaticReflectionProvider) {
50+
$this->markTestSkipped('Test requires static reflection.');
51+
}
52+
53+
$this->phpVersionId = $phpVersionId;
54+
$this->analyse([__DIR__ . '/data/final-constant.php'], $errors);
55+
}
56+
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace FinalConstant;
4+
5+
class Foo
6+
{
7+
8+
const TEST = 1;
9+
final const BAR = 2;
10+
11+
}

0 commit comments

Comments
 (0)