Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule that disallows final private methods #1490

Merged
merged 1 commit into from
Jul 10, 2022
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 conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ rules:
- PHPStan\Rules\Methods\CallMethodsRule
- PHPStan\Rules\Methods\CallStaticMethodsRule
- PHPStan\Rules\Methods\ExistingClassesInTypehintsRule
- PHPStan\Rules\Methods\FinalPrivateMethodRule
- PHPStan\Rules\Methods\MethodCallableRule
- PHPStan\Rules\Methods\MissingMethodImplementationRule
- PHPStan\Rules\Methods\MethodAttributesRule
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,9 @@ public function supportsPassNoneEncodings(): bool
return $this->versionId < 70300;
}

public function producesWarningForFinalPrivateMethods(): bool
{
return $this->versionId >= 80000;
}

}
53 changes: 53 additions & 0 deletions src/Rules/Methods/FinalPrivateMethodRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Methods;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/** @implements Rule<InClassMethodNode> */
class FinalPrivateMethodRule implements Rule
{

public function __construct(
private PhpVersion $phpVersion,
)
{
}

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

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof PhpMethodFromParserNodeReflection) {
return [];
}

if (!$this->phpVersion->producesWarningForFinalPrivateMethods()) {
return [];
}

if (!$method->isFinal()->yes() || !$method->isPrivate()) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Private method %s::%s() cannot be final as it is never overridden by other classes.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->build(),
];
}

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

namespace PHPStan\Rules\Methods;

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

/** @extends RuleTestCase<FinalPrivateMethodRule> */
class FinalPrivateMethodRuleTest extends RuleTestCase
{

private int $phpVersionId;

protected function getRule(): Rule
{
return new FinalPrivateMethodRule(
new PhpVersion($this->phpVersionId),
);
}

public function dataRule(): array
{
return [
[
70400,
[],
],
[
80000,
[
[
'Private method FinalPrivateMethod\Foo::foo() cannot be final as it is never overridden by other classes.',
8,
],
],
],
];
}

/**
* @dataProvider dataRule
* @param mixed[] $errors
*/
public function testRule(int $phpVersion, array $errors): void
{
$this->phpVersionId = $phpVersion;
$this->analyse([__DIR__ . '/data/final-private-method.php'], $errors);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Methods/data/final-private-method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 7.4

namespace FinalPrivateMethod;

class Foo
{

final private function foo(): void
{
}

final protected function bar(): void
{
}

final public function baz(): void
{
}

private function foobar(): void
{
}

}