Skip to content

Commit

Permalink
Fix: Use more appropriate message when detecting protected method in …
Browse files Browse the repository at this point in the history
…final anonymous class
  • Loading branch information
localheinz committed Jan 6, 2025
1 parent bcdde25 commit aed422b
Show file tree
Hide file tree
Showing 2 changed files with 18 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 apprproate message when detecting a `proected` method in an `final` 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

0 comments on commit aed422b

Please sign in to comment.