Skip to content

Commit

Permalink
Fix: Ignore protected methods declared in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 6, 2025
1 parent e5bd8b5 commit 30998b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ For a full diff see [`2.5.0...main`][2.5.0...main].
- 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 `protected` method in an anonymous class ([#890]), by [@localheinz]
- Adjusted `Methods\PrivateInFinalClass` to ignore `protected` methods from traits([#891]), by [@localheinz]

## [`2.5.0`][2.5.0]

Expand Down Expand Up @@ -554,6 +555,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
[#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
[#891]: https://github.com/ergebnis/phpstan-rules/pull/891

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

if (self::methodIsDeclaredByTrait($containingClass, $methodName)) {
return [];
}

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

Expand Down Expand Up @@ -99,4 +103,17 @@ private static function methodIsDeclaredByParentClass(

return true;
}

private static function methodIsDeclaredByTrait(
Reflection\ClassReflection $containingClass,
string $methodName
): bool {
foreach ($containingClass->getTraits() as $trait) {
if ($trait->hasMethod($methodName)) {
return true;
}
}

return false;
}
}
33 changes: 0 additions & 33 deletions test/Integration/Methods/PrivateInFinalClassRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,17 @@ public function testPrivateInFinalClassRule(): void
$this->analyse(
self::phpFilesIn(__DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule'),
[
[
\sprintf(
'Method %s::method() is protected, but since the containing class is final, it can be private.',
Test\Fixture\Methods\PrivateInFinalClassRule\FinalClassImplementingAbstractProtectedMethodFromTrait::class,
),
11,
],
[
\sprintf(
'Method %s::method() is protected, but since the containing class is final, it can be private.',
Test\Fixture\Methods\PrivateInFinalClassRule\FinalClassOverridingProtectedMethodFromTrait::class,
),
11,
],
[
\sprintf(
'Method %s::method() is protected, but since the containing class is final, it can be private.',
Test\Fixture\Methods\PrivateInFinalClassRule\FinalClassWithProtectedMethod::class,
),
9,
],
[
\sprintf(
'Method %s::method() is protected, but since the containing class is final, it can be private.',
Test\Fixture\Methods\PrivateInFinalClassRule\FinalClassWithProtectedMethodFromTrait::class,
),
9,
],
[
'Method method() in anonymous class is protected, but since the containing class is final, it can be private.',
8,
],
[
'Method method() in anonymous class is protected, but since the containing class is final, it can be private.',
9,
],
[
'Method method() in anonymous class is protected, but since the containing class is final, it can be private.',
20,
],
[
'Method method() in anonymous class is protected, but since the containing class is final, it can be private.',
28,
],
],
);
}
Expand Down

0 comments on commit 30998b6

Please sign in to comment.