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 @@ -107,6 +107,7 @@ lint:
--exclude tests/PHPStan/Rules/Properties/data/final-property-hooks.php \
--exclude tests/PHPStan/Rules/Properties/data/final-properties.php \
--exclude tests/PHPStan/Rules/Properties/data/property-in-interface-explicit-abstract.php \
--exclude tests/PHPStan/Rules/Constants/data/final-private-const.php \
src tests

cs:
Expand Down
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rules:
- PHPStan\Rules\Constants\FinalConstantRule
- PHPStan\Rules\Constants\MagicConstantContextRule
- PHPStan\Rules\Constants\NativeTypedClassConstantRule
- PHPStan\Rules\Constants\FinalPrivateConstantRule
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule
- PHPStan\Rules\Exceptions\NoncapturingCatchRule
- PHPStan\Rules\Exceptions\ThrowExpressionRule
Expand Down
49 changes: 49 additions & 0 deletions src/Rules/Constants/FinalPrivateConstantRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Constants;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function sprintf;

/** @implements Rule<ClassConst> */
final class FinalPrivateConstantRule implements Rule
{

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

public function processNode(Node $node, Scope $scope): array
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
$classReflection = $scope->getClassReflection();

if (!$node->isFinal()) {
return [];
}

if (!$node->isPrivate()) {
return [];
}

$errors = [];
foreach ($node->consts as $classConstNode) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Private constant %s::%s() cannot be final as it is never overridden by other classes.',
$classReflection->getDisplayName(),
$classConstNode->name->name,
))->identifier('classConstant.finalPrivate')->nonIgnorable()->build();
}

return $errors;
}

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

namespace PHPStan\Rules\Constants;

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

/** @extends RuleTestCase<FinalPrivateConstantRule> */
class FinalPrivateConstantRuleTest extends RuleTestCase
{

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

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/final-private-const.php'], [
[
'Private constant FinalPrivateConstants\User::FINAL_PRIVATE() cannot be final as it is never overridden by other classes.',
8,
],
]);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Constants/data/final-private-const.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace FinalPrivateConstants;

class User
{
private const PRIVATE = 'mailto: example.org';
final private const FINAL_PRIVATE = 'mailto: example.org';
final protected const PROTECTED = 'mailto: example.org';
final public const PUBLIC = 'mailto: example.org';
}
Loading