Skip to content

Commit

Permalink
Fix Issue #78 - Error in Redirect::route() Method (#106)
Browse files Browse the repository at this point in the history
* Bug fix on redirect route to to route transformation
* Redirect back to back helper transformation fix

1) Argument missing fix
2) Last child can not be deleted.

* rename classes
* Code fixes
  • Loading branch information
hirenkeraliya authored May 17, 2023
1 parent 6043322 commit 02c1eb7
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/Rector/ClassMethod/AddArgumentDefaultValueRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
Expand Down Expand Up @@ -93,7 +94,7 @@ public function refactor(Node $node): ClassMethod
$position = $addedArgument->getPosition();
$param = $node->params[$position];

if ($param->default !== null) {
if ($param->default instanceof Expr) {
continue;
}

Expand Down
23 changes: 14 additions & 9 deletions src/Rector/Empty_/EmptyToBlankAndFilledFuncRector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\Empty_;

use PhpParser\Node;
Expand All @@ -14,22 +16,25 @@
*/
class EmptyToBlankAndFilledFuncRector extends AbstractRector
{

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace use of the unsafe empty() function with Laravel\'s safer blank() & filled() functions.', [
new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'Replace use of the unsafe empty() function with Laravel\'s safer blank() & filled() functions.',
[
new CodeSample(
<<<'CODE_SAMPLE'
empty([]);
!empty([]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
blank([]);
filled([]);
CODE_SAMPLE
),
]);
),

]
);
}

public function getNodeTypes(): array
Expand All @@ -45,7 +50,7 @@ public function refactor(Node $node): ?Node
}
$method = 'filled';
$args = [$node->expr->expr];
} else if ($node instanceof Empty_) {
} elseif ($node instanceof Empty_) {
$method = 'blank';
$args = [$node->expr];
} else {
Expand Down
15 changes: 9 additions & 6 deletions src/Rector/MethodCall/AssertStatusToAssertMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -189,18 +192,18 @@ private function updateAssertStatusCall(MethodCall $methodCall): ?MethodCall
return null;
}

if (count($methodCall->getArgs()) <> 1) {
if (count($methodCall->getArgs()) !== 1) {
return null;
}

$arg = $methodCall->getArgs()[0];
$argValue = $arg->value;

if (! $argValue instanceof Node\Scalar\LNumber && ! $argValue instanceof Node\Expr\ClassConstFetch) {
if (! $argValue instanceof LNumber && ! $argValue instanceof ClassConstFetch) {
return null;
}

if ($argValue instanceof Node\Scalar\LNumber) {
if ($argValue instanceof LNumber) {
$replacementMethod = match ($argValue->value) {
200 => 'assertOk',
204 => 'assertNoContent',
Expand All @@ -216,9 +219,9 @@ private function updateAssertStatusCall(MethodCall $methodCall): ?MethodCall
} else {
if (! in_array($this->getName($argValue->class), [
'Illuminate\Http\Response',
'Symfony\Component\HttpFoundation\Response'
'Symfony\Component\HttpFoundation\Response',
], true)) {
return null;
return null;
}

$replacementMethod = match ($this->getName($argValue->name)) {
Expand All @@ -239,7 +242,7 @@ private function updateAssertStatusCall(MethodCall $methodCall): ?MethodCall
return null;
}

$methodCall->name = new Node\Identifier($replacementMethod);
$methodCall->name = new Identifier($replacementMethod);
$methodCall->args = [];

return $methodCall;
Expand Down
12 changes: 9 additions & 3 deletions src/Rector/MethodCall/RedirectBackToBackHelperRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node
return $this->updateRedirectStaticCall($node);
}

private function updateRedirectHelperCall(MethodCall $methodCall): ?MethodCall
private function updateRedirectHelperCall(MethodCall $methodCall): MethodCall|FuncCall|null
{
if (! $this->isName($methodCall->name, 'back')) {
return null;
Expand All @@ -115,9 +115,15 @@ private function updateRedirectHelperCall(MethodCall $methodCall): ?MethodCall
return null;
}

$this->removeNode($methodCall);
$childElement = $methodCall->getAttribute('parent');

$parentNode->var->name = new Name('back');
if ($childElement instanceof MethodCall) {
$this->removeNode($methodCall);
$parentNode->var->name = new Name('back');
$parentNode->var->args = $methodCall->getArgs();
} else {
return new FuncCall(new Name('back'), $methodCall->getArgs());
}

return $parentNode;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Rector/MethodCall/RedirectRouteToToRouteHelperRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node
return $this->updateRedirectStaticCall($node);
}

private function updateRedirectHelperCall(MethodCall $methodCall): ?MethodCall
private function updateRedirectHelperCall(MethodCall $methodCall): MethodCall|FuncCall|null
{
if (! $this->isName($methodCall->name, 'route')) {
return null;
Expand All @@ -115,10 +115,15 @@ private function updateRedirectHelperCall(MethodCall $methodCall): ?MethodCall
return null;
}

$this->removeNode($methodCall);
$childElement = $methodCall->getAttribute('parent');

$parentNode->var->name = new Name('to_route');
$parentNode->var->args = $methodCall->getArgs();
if ($childElement instanceof MethodCall) {
$this->removeNode($methodCall);
$parentNode->var->name = new Name('to_route');
$parentNode->var->args = $methodCall->getArgs();
} else {
return new FuncCall(new Name('to_route'), $methodCall->getArgs());
}

return $parentNode;
}
Expand Down
14 changes: 5 additions & 9 deletions src/Rector/StaticCall/RouteActionCallableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -140,16 +142,12 @@ public function refactor(Node $node): ?Node
if (is_string($argValue['middleware'])) {
$argument = new String_($argValue['middleware']);
} else {
$argument = new Node\Expr\Array_(array_map(
static fn ($value) => new Node\Expr\ArrayItem(new String_($value)),
$argument = new Array_(array_map(
static fn ($value) => new ArrayItem(new String_($value)),
$argValue['middleware']
));
}
$node = new MethodCall(
$node,
'middleware',
[new Arg($argument)]
);
$node = new MethodCall($node, 'middleware', [new Arg($argument)]);
}

return $node;
Expand All @@ -172,8 +170,6 @@ public function configure(array $configuration): void
}

/**
* @param mixed $action
*
* @return array{string, string}|null
*/
private function resolveControllerFromAction(mixed $action): ?array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Rector\Config\RectorConfig;

use RectorLaravel\Rector\MethodCall\RedirectBackToBackHelperRector;
use RectorLaravel\Rector\MethodCall\AssertStatusToAssertMethodRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(\RectorLaravel\Rector\MethodCall\AssertStatusToAssertMethodRector::class);
$rectorConfig->rule(AssertStatusToAssertMethodRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectBackToBackHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithArgument
{
public function store()
{
return redirect()->back(302);
}

public function update()
{
return Redirect::back(302);
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectBackToBackHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithArgument
{
public function store()
{
return back(302);
}

public function update()
{
return back(302);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectBackToBackHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithoutWith
{
public function store()
{
return redirect()->back();
}

public function update()
{
return Redirect::back();
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectBackToBackHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithoutWith
{
public function store()
{
return back();
}

public function update()
{
return back();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectRouteToToRouteHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithoutWith
{
public function store()
{
return redirect()->route('home');
}

public function update()
{
return Redirect::route('home');
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\RedirectRouteToToRouteHelperRector\Fixture;

use Illuminate\Support\Facades\Redirect;

class FixtureWithoutWith
{
public function store()
{
return to_route('home');
}

public function update()
{
return to_route('home');
}
}

?>

0 comments on commit 02c1eb7

Please sign in to comment.