Skip to content

Commit

Permalink
Skip only response inside controller class (#6204)
Browse files Browse the repository at this point in the history
* Skip only response inside controller class

* fixup! Skip only response inside controller class

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 30, 2024
1 parent de1d273 commit 97653a9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;

use Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

final class IncludeResponseOutsideController
{
public function create()
{
return new JsonResponse();
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture;

use Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

final class IncludeResponseOutsideController
{
public function create(): \Symfony\Component\HttpFoundation\JsonResponse
{
return new JsonResponse();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ReturnAnalyzer $returnAnalyzer,
private readonly ControllerAnalyzer $controllerAnalyzer
) {
}

Expand Down Expand Up @@ -184,9 +186,8 @@ private function refactorDirectReturnNew(

$returnType = $this->typeFactory->createMixedPassedOrUnionType($newTypes);

// skip in case of response, as handled by another rule earlier
/** @see ResponseReturnTypeControllerActionRector */
if ($returnType instanceof ObjectType && $returnType->isInstanceOf(ResponseClass::BASIC)->yes()) {
/** handled by @see \Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector earlier */
if ($this->isResponseInsideController($returnType, $functionLike)) {
return null;
}

Expand Down Expand Up @@ -222,4 +223,21 @@ private function resolveReturnNewType(array $returns): ?array

return $newTypes;
}

private function isResponseInsideController(Type $returnType, ClassMethod|Function_ $functionLike): bool
{
if (! $functionLike instanceof ClassMethod) {
return false;
}

if (! $returnType instanceof ObjectType) {
return false;
}

if (! $returnType->isInstanceOf(ResponseClass::BASIC)->yes()) {
return false;
}

return $this->controllerAnalyzer->isInsideController($functionLike);
}
}

0 comments on commit 97653a9

Please sign in to comment.