Skip to content

Commit

Permalink
Fix response bug
Browse files Browse the repository at this point in the history
Resolves: #2764
  • Loading branch information
sabbelasichon committed Jan 10, 2022
1 parent 737d865 commit 6e4f2f3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -160,6 +161,10 @@ private function shouldSkip(ClassMethod $node): bool
return true;
}

if ($this->hasExceptionCall($node)) {
return true;
}

return $this->alreadyResponseReturnType($node);
}

Expand Down Expand Up @@ -202,17 +207,45 @@ private function alreadyResponseReturnType(ClassMethod $node): bool
$returns = $this->findReturns($node);

$responseObjectType = new ObjectType('Psr\Http\Message\ResponseInterface');

foreach ($returns as $return) {
if (null === $return->expr) {
continue;
}

$returnType = $this->getType($return->expr);

if ($returnType->isSuperTypeOf($responseObjectType)->yes()) {
return true;
}

if ($returnType instanceof ObjectType && $returnType->isInstanceOf(
'Psr\Http\Message\ResponseInterface'
)->yes()) {
return true;
}
}

return false;
}

private function hasExceptionCall(ClassMethod $node): bool
{
if (null === $node->stmts) {
return false;
}

$statements = $node->stmts;
$lastStatement = array_pop($statements);

if (! ($lastStatement instanceof Throw_)) {
return false;
}

$propagateResponseException = new ObjectType('TYPO3\CMS\Core\Http\PropagateResponseException');

return $this->getType($lastStatement->expr)
->isSuperTypeOf($propagateResponseException)
->yes();
}
}
17 changes: 17 additions & 0 deletions stubs/TYPO3/CMS/Core/Http/PropagateResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace TYPO3\CMS\Core\Http;

use Psr\Http\Message\ResponseInterface;

if(class_exists('TYPO3\CMS\Core\Http\PropagateResponseException')) {
return;
}

final class PropagateResponseException extends ImmediateResponseException
{
public function __construct(ResponseInterface $response, int $code)
{
parent::__construct();
}
}
15 changes: 5 additions & 10 deletions stubs/TYPO3/CMS/Core/Http/ResponseFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@

interface ResponseFactoryInterface
{
/**
* @param string $html
* @return \Psr\Http\Message\ResponseInterface
*/
public function createHtmlResponse($html);
/**
* @param string $json
* @return \Psr\Http\Message\ResponseInterface
*/
public function createJsonResponse($json);
public function createHtmlResponse(string $html): ResponseInterface;

public function createJsonResponse(string $json): ResponseInterface;

public function createResponse(int $statusCode): ResponseInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v0\ExtbaseControllerActionsMustReturnResponseInterfaceRector\Fixture;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class MyRefactoredController extends ActionController
{
public function someAction(): ResponseInterface
{
$response = $this->responseFactory->createResponse(200);
throw new PropagateResponseException($response, 200);
}

public function someOtherAction(): ResponseInterface
{
return new ForwardResponse('another');
}
}

?>

0 comments on commit 6e4f2f3

Please sign in to comment.