Skip to content

Commit

Permalink
Fix: Rename methods and extract fields
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 7, 2025
1 parent 44c7c4d commit 6de6bb3
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/Methods/PrivateInFinalClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@
*/
final class PrivateInFinalClassRule implements Rules\Rule
{
/**
* @var list<string>
*/
private static array $whitelistedAnnotations = [
'@after',
'@before',
'@postCondition',
'@preCondition',
];

/**
* @var list<class-string>
*/
private static array $whitelistedAttributes = [
Framework\Attributes\After::class,
Framework\Attributes\Before::class,
Framework\Attributes\PostCondition::class,
Framework\Attributes\PreCondition::class,
];
private Type\FileTypeMapper $fileTypeMapper;

public function __construct(Type\FileTypeMapper $fileTypeMapper)
Expand Down Expand Up @@ -58,11 +77,11 @@ public function processNode(
return [];
}

if ($this->methodHasPhpUnitAnnotationWhichRequiresProtectedVisibility($node, $containingClass)) {
if ($this->methodHasWhitelistedAnnotation($node, $containingClass)) {
return [];
}

if (self::methodHasPhpUnitAttributeWhichRequiresProtectedVisibility($node)) {
if (self::methodHasWhitelistedAttribute($node)) {
return [];
}

Expand Down Expand Up @@ -105,7 +124,7 @@ public function processNode(
];
}

private function methodHasPhpUnitAnnotationWhichRequiresProtectedVisibility(
private function methodHasWhitelistedAnnotation(
Node\Stmt\ClassMethod $node,
Reflection\ClassReflection $containingClass
): bool {
Expand All @@ -123,16 +142,9 @@ private function methodHasPhpUnitAnnotationWhichRequiresProtectedVisibility(
$docComment->getText(),
);

$annotations = [
'@after',
'@before',
'@postCondition',
'@preCondition',
];

foreach ($resolvedPhpDoc->getPhpDocNodes() as $phpDocNode) {
foreach ($phpDocNode->getTags() as $tag) {
if (\in_array($tag->name, $annotations, true)) {
if (\in_array($tag->name, self::$whitelistedAnnotations, true)) {
return true;
}
}
Expand All @@ -141,18 +153,11 @@ private function methodHasPhpUnitAnnotationWhichRequiresProtectedVisibility(
return false;
}

private static function methodHasPhpUnitAttributeWhichRequiresProtectedVisibility(Node\Stmt\ClassMethod $node): bool
private static function methodHasWhitelistedAttribute(Node\Stmt\ClassMethod $node): bool
{
$attributes = [
Framework\Attributes\After::class,
Framework\Attributes\Before::class,
Framework\Attributes\PostCondition::class,
Framework\Attributes\PreCondition::class,
];

foreach ($node->attrGroups as $attributeGroup) {
foreach ($attributeGroup->attrs as $attribute) {
if (\in_array($attribute->name->toString(), $attributes, true)) {
if (\in_array($attribute->name->toString(), self::$whitelistedAttributes, true)) {
return true;
}
}
Expand Down

0 comments on commit 6de6bb3

Please sign in to comment.