Skip to content
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
@@ -0,0 +1,39 @@
<?php

namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

final class ClassWithUntypedRequestParam extends Controller
{
/**
* @Route("/some-action", name="some_action")
*/
public function someAction($request)
{
return $this->getRequest()->getSomething();
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony30\Rector\ClassMethod\GetRequestRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;

final class ClassWithUntypedRequestParam extends Controller
{
/**
* @Route("/some-action", name="some_action")
*/
public function someAction(\Symfony\Component\HttpFoundation\Request $request)
{
return $request->getSomething();
}
}

?>
38 changes: 32 additions & 6 deletions rules/Symfony30/Rector/ClassMethod/GetRequestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ public function refactor(Node $node): ?Node
return null;
}

private function resolveExistingUntypedRequestParam(ClassMethod $classMethod): ?Param
{
foreach ($classMethod->params as $param) {
if ($param->type !== null) {
continue;
}

if (! $param->var instanceof Variable) {
continue;
}

if ($this->isName($param->var, 'request')) {
return $param;
}
}

return null;
}

private function resolveUniqueName(ClassMethod $classMethod, string $name): string
{
$candidateNames = [];
Expand Down Expand Up @@ -239,16 +258,23 @@ private function getRequestVariableAndParamName(): string

private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod
{
$this->requestVariableAndParamName = $this->resolveUniqueName($classMethod, 'request');

if (! $this->isActionWithGetRequestInBody($classMethod)) {
return null;
}

$fullyQualified = new FullyQualified(SymfonyClass::REQUEST);
$classMethod->params[] = new Param(new Variable(
$this->getRequestVariableAndParamName()
), null, $fullyQualified);
$existingUntypedRequestParam = $this->resolveExistingUntypedRequestParam($classMethod);
if ($existingUntypedRequestParam instanceof Param) {
// reuse an already present, untyped $request param instead of adding a duplicate
$existingUntypedRequestParam->type = new FullyQualified(SymfonyClass::REQUEST);
$this->requestVariableAndParamName = $this->getName($existingUntypedRequestParam);
} else {
$this->requestVariableAndParamName = $this->resolveUniqueName($classMethod, 'request');

$fullyQualified = new FullyQualified(SymfonyClass::REQUEST);
$classMethod->params[] = new Param(new Variable(
$this->getRequestVariableAndParamName()
), null, $fullyQualified);
}

$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use (
$classMethod
Expand Down
Loading