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

Fix response bug #2776

Merged
merged 1 commit into from
Jan 10, 2022
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
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');
}
}

?>