Skip to content

Commit

Permalink
do not throw error is method is marked as #[Before]
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech authored and localheinz committed Jan 7, 2025
1 parent a95714d commit efbc9f8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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
{
}
}

0 comments on commit efbc9f8

Please sign in to comment.