Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Skip attributes on setter rule #120

Merged
merged 4 commits into from
May 21, 2024
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
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
Loading