Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

#[Before] attribute should not be private in final classes #863

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"PHPStan\\Rules\\Rule",
"PHPStan\\Rules\\RuleError",
"PHPStan\\Rules\\RuleErrorBuilder",
"PHPUnit\\Framework\\Attributes\\After",
"PHPUnit\\Framework\\Attributes\\Before",
"PHPUnit\\Framework\\Attributes\\PostCondition",
"PHPUnit\\Framework\\Attributes\\PreCondition",
"self",
"string",
"true"
Expand Down
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ parameters:
count: 1
path: src/Functions/NoNullableReturnTypeDeclarationRule.php

-
message: "#^Class PHPUnit\\\\Framework\\\\Attributes\\\\After not found\\.$#"
count: 1
path: src/Methods/PrivateInFinalClassRule.php

-
message: "#^Class PHPUnit\\\\Framework\\\\Attributes\\\\Before not found\\.$#"
count: 1
path: src/Methods/PrivateInFinalClassRule.php

-
message: "#^Class PHPUnit\\\\Framework\\\\Attributes\\\\PostCondition not found\\.$#"
count: 1
path: src/Methods/PrivateInFinalClassRule.php

-
message: "#^Class PHPUnit\\\\Framework\\\\Attributes\\\\PreCondition not found\\.$#"
count: 1
path: src/Methods/PrivateInFinalClassRule.php

25 changes: 25 additions & 0 deletions src/Methods/PrivateInFinalClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Analyser;
use PHPStan\Reflection;
use PHPStan\Rules;
use PHPUnit\Framework;

/**
* @implements Rules\Rule<Node\Stmt\ClassMethod>
Expand Down Expand Up @@ -48,6 +49,10 @@ public function processNode(
return [];
}

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

$methodName = $node->name->toString();

if (self::methodIsDeclaredByParentClass($containingClass, $methodName)) {
Expand Down Expand Up @@ -87,6 +92,26 @@ public function processNode(
];
}

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

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if (\in_array($attribute->name->toString(), $attributes, true)) {
return true;
}
}
}

return false;
}

private static function methodIsDeclaredByParentClass(
Reflection\ClassReflection $containingClass,
string $methodName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Methods\PrivateInFinalClassRule;

use PHPUnit\Framework;

final class FinalClassWithProtectedMethodsWithPhpUnitAttributes extends Framework\TestCase
{
#[Framework\Attributes\After()]
protected function methodWithAfterAttribute(): void
{
}

#[Framework\Attributes\Before()]
protected function methodWithBeforeAttribute(): void
{
}

#[Framework\Attributes\PreCondition()]
protected function methodWithPreConditionAttribute(): void
{
}

#[Framework\Attributes\PostCondition()]
protected function methodWithPostConditionAttribute(): void
{
}
}
Loading