Skip to content

Commit

Permalink
Skip attributes on setter rule (#120)
Browse files Browse the repository at this point in the history
* add route test fixture

* add route test fixture

* skip attributed class method
  • Loading branch information
TomasVotruba authored May 21, 2024
1 parent a3dff14 commit cb7c2cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Rules/NoReturnSetterMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
// possibly some important logic
if ($node->attrGroups !== []) {
return [];
}

if (! $classReflection->isClass()) {
if (! $this->isInsideClassReflection($scope)) {
return [];
}

Expand Down Expand Up @@ -124,4 +124,14 @@ private function hasReturnReturnFunctionLike(ClassMethod $classMethod): bool
$yield = $this->typeAwareNodeFinder->findFirstInstanceOf($classMethod, Yield_::class);
return $yield instanceof Yield_;
}

private function isInsideClassReflection(Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isClass();
}
}
17 changes: 17 additions & 0 deletions tests/Rules/NoReturnSetterMethodRule/Fixture/SkipRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\NoReturnSetterMethodRule\Fixture;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

final class SkipRoute
{
#[Route('some_route')]
public function setIncome(string $name): JsonResponse
{
return new JsonResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/SomeSetterClass.php', [[NoReturnSetterMethodRule::ERROR_MESSAGE, 9]]];

yield [__DIR__ . '/Fixture/SkipRoute.php', []];
yield [__DIR__ . '/Fixture/SkipEmptyReturn.php', []];
yield [__DIR__ . '/Fixture/SkipVoidSetter.php', []];
yield [__DIR__ . '/Fixture/SkipSetUp.php', []];
Expand Down

0 comments on commit cb7c2cd

Please sign in to comment.