Skip to content

Commit 922f11b

Browse files
committed
fix: protected method in final class -> private is not a BC break
A method should only be considered "accessible" outside the class if: * It is `public` * It is `protected` and the class is NOT `final` Previously, `protected` methods in `final` classes were incorrectly considered to be accessible. This meant that changing one to `protected` would incorrectly be reported as a BC break. This commit fixes that logic.
1 parent 5a7bbb1 commit 922f11b

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/DetectChanges/BCBreak/ClassBased/MethodRemoved.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public function __invoke(ReflectionClass $fromClass, ReflectionClass $toClass):
4444
/** @return array<string, ReflectionMethod> */
4545
private function accessibleMethods(ReflectionClass $class): array
4646
{
47-
$methods = Vec\filter($class->getMethods(), function (ReflectionMethod $method): bool {
48-
return ($method->isPublic()
49-
|| $method->isProtected())
47+
$methods = Vec\filter($class->getMethods(), function (ReflectionMethod $method) use ($class): bool {
48+
return $this->isAccessibleMethod($method, $class)
5049
&& ! $this->isInternalDocComment($method->getDocComment());
5150
});
5251

@@ -58,6 +57,15 @@ private function accessibleMethods(ReflectionClass $class): array
5857
);
5958
}
6059

60+
private function isAccessibleMethod(ReflectionMethod $method, ReflectionClass $class): bool
61+
{
62+
if ($method->isPublic()) {
63+
return true;
64+
}
65+
66+
return $method->isProtected() && ! $class->isFinal();
67+
}
68+
6169
private function isInternalDocComment(string|null $comment): bool
6270
{
6371
return $comment !== null

0 commit comments

Comments
 (0)