Skip to content

Commit

Permalink
Merge pull request #890 from ergebnis/fix/anonymous
Browse files Browse the repository at this point in the history
Fix: Use more appropriate message when detecting `protected` method in anonymous class
  • Loading branch information
localheinz authored Jan 6, 2025
2 parents 8b2a546 + 3102451 commit 317259a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For a full diff see [`2.5.0...main`][2.5.0...main].
- Returned rule with error identifier ([#882]), by [@localheinz]
- Adjusted `Methods\FinalInAbstractClassRule` to ignore Doctrine embeddables and entities ([#396]), by [@localheinz]
- Adjusted `Expressions\NoCompactRule` to detect usages of `compact()` with incorrect case ([#889]), by [@localheinz]
- Adjusted `Methods\PrivateInFinalClass` to use more appropriate message when detecting a `proected` method in an anonymous class ([#890]), by [@localheinz]

## [`2.5.0`][2.5.0]

Expand Down Expand Up @@ -552,6 +553,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[#880]: https://github.com/ergebnis/phpstan-rules/pull/880
[#882]: https://github.com/ergebnis/phpstan-rules/pull/882
[#889]: https://github.com/ergebnis/phpstan-rules/pull/889
[#890]: https://github.com/ergebnis/phpstan-rules/pull/890

[@enumag]: https://github.com/enumag
[@ergebnis]: https://github.com/ergebnis
Expand Down
16 changes: 16 additions & 0 deletions src/Methods/PrivateInFinalClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public function processNode(
}
}

/** @var Reflection\ClassReflection $classReflection */
$classReflection = $scope->getClassReflection();

if ($classReflection->isAnonymous()) {
$message = \sprintf(
'Method %s() in anonymous class is protected, but since the containing class is final, it can be private.',
$node->name->name,
);

return [
Rules\RuleErrorBuilder::message($message)
->identifier(ErrorIdentifier::privateInFinalClass()->toString())
->build(),
];
}

$message = \sprintf(
'Method %s::%s() is protected, but since the containing class is final, it can be private.',
$containingClass->getName(),
Expand Down
7 changes: 7 additions & 0 deletions test/Integration/Methods/PrivateInFinalClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public static function provideCasesWhereAnalysisShouldSucceed(): iterable
public static function provideCasesWhereAnalysisShouldFail(): iterable
{
$paths = [
'anonymous-class-with-protected-method' => [
__DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Failure/AnonymousClassWithProtectedMethod.php',
[
'Method method() in anonymous class is protected, but since the containing class is final, it can be private.',
8,
],
],
'final-class-with-protected-method' => [
__DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Failure/FinalClassWithProtectedMethod.php',
[
Expand Down

0 comments on commit 317259a

Please sign in to comment.