Skip to content

Commit cb7c2cd

Browse files
authored
Skip attributes on setter rule (#120)
* add route test fixture * add route test fixture * skip attributed class method
1 parent a3dff14 commit cb7c2cd

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Rules/NoReturnSetterMethodRule.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function getNodeType(): string
5353
*/
5454
public function processNode(Node $node, Scope $scope): array
5555
{
56-
$classReflection = $scope->getClassReflection();
57-
if (! $classReflection instanceof ClassReflection) {
56+
// possibly some important logic
57+
if ($node->attrGroups !== []) {
5858
return [];
5959
}
6060

61-
if (! $classReflection->isClass()) {
61+
if (! $this->isInsideClassReflection($scope)) {
6262
return [];
6363
}
6464

@@ -124,4 +124,14 @@ private function hasReturnReturnFunctionLike(ClassMethod $classMethod): bool
124124
$yield = $this->typeAwareNodeFinder->findFirstInstanceOf($classMethod, Yield_::class);
125125
return $yield instanceof Yield_;
126126
}
127+
128+
private function isInsideClassReflection(Scope $scope): bool
129+
{
130+
$classReflection = $scope->getClassReflection();
131+
if (! $classReflection instanceof ClassReflection) {
132+
return false;
133+
}
134+
135+
return $classReflection->isClass();
136+
}
127137
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\NoReturnSetterMethodRule\Fixture;
6+
7+
use Symfony\Component\HttpFoundation\JsonResponse;
8+
use Symfony\Component\Routing\Attribute\Route;
9+
10+
final class SkipRoute
11+
{
12+
#[Route('some_route')]
13+
public function setIncome(string $name): JsonResponse
14+
{
15+
return new JsonResponse();
16+
}
17+
}

tests/Rules/NoReturnSetterMethodRule/NoReturnSetterMethodRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static function provideData(): Iterator
2525
{
2626
yield [__DIR__ . '/Fixture/SomeSetterClass.php', [[NoReturnSetterMethodRule::ERROR_MESSAGE, 9]]];
2727

28+
yield [__DIR__ . '/Fixture/SkipRoute.php', []];
2829
yield [__DIR__ . '/Fixture/SkipEmptyReturn.php', []];
2930
yield [__DIR__ . '/Fixture/SkipVoidSetter.php', []];
3031
yield [__DIR__ . '/Fixture/SkipSetUp.php', []];

0 commit comments

Comments
 (0)