diff --git a/src/Methods/PrivateInFinalClassRule.php b/src/Methods/PrivateInFinalClassRule.php index b552ecbb..c04025ff 100644 --- a/src/Methods/PrivateInFinalClassRule.php +++ b/src/Methods/PrivateInFinalClassRule.php @@ -21,6 +21,13 @@ final class PrivateInFinalClassRule implements Rules\Rule { + /** + * @var array + */ + private static array $attributesThatIndicateProtectedIsOk = [ + \PHPUnit\Framework\Attributes\Before::class, + ]; + public function getNodeType(): string { return Node\Stmt\ClassMethod::class; @@ -53,6 +60,15 @@ public function processNode( return []; } + // Check attributes + foreach ($node->attrGroups as $attrGroup) { + foreach ($attrGroup->attrs as $attr) { + if (\in_array($attr->name->toString(), self::$attributesThatIndicateProtectedIsOk, true)) { + return []; + } + } + } + $methodName = $node->name->toString(); $parentClass = $containingClass->getNativeReflection()->getParentClass(); diff --git a/test/Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodUsingPhpUnitAttribute.php b/test/Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodUsingPhpUnitAttribute.php new file mode 100644 index 00000000..627c1ec2 --- /dev/null +++ b/test/Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodUsingPhpUnitAttribute.php @@ -0,0 +1,13 @@ + __DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodExtendingClassWithSameProtectedMethod.php', 'final-class-with-protected-method-from-trait' => __DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodFromTrait.php', 'final-class-with-public-method' => __DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithPublicMethod.php', + 'final-class-with-protected-method-using-php-unit-attribute' => __DIR__ . '/../../Fixture/Methods/PrivateInFinalClassRule/Success/FinalClassWithProtectedMethodUsingPhpUnitAttribute.php', ]; foreach ($paths as $description => $path) {