[Laravel] Add Redirect301ToPermanentRedirect

This commit is contained in:
Tomas Votruba 2019-03-08 21:04:38 +00:00
parent 2a844ab44d
commit 4a27d61ccd
4 changed files with 127 additions and 0 deletions

View File

@ -18,3 +18,4 @@ services:
sendResetLinkResponse:
name: 'request'
type: 'Illuminate\Http\Illuminate\Http'
Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector: ~

View File

@ -0,0 +1,80 @@
<?php declare(strict_types=1);
namespace Rector\Laravel\Rector\StaticCall;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://laravel.com/docs/5.7/upgrade
*/
final class Redirect301ToPermanentRedirectRector extends AbstractRector
{
/**
* @var string[]
*/
private $routeTypes = ['Illuminate\Support\Facades\Route', 'Illuminate\Routing\Route'];
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change "redirect" call with 301 to "permanentRedirect"', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isTypes($node, $this->routeTypes)) {
return null;
}
if (! isset($node->args[2])) {
return null;
}
if ($this->getValue($node->args[2]->value) !== 301) {
return null;
}
unset($node->args[2]);
$node->name = new Identifier('permanentRedirect');
return $node;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Laravel\Tests\Rector\StaticCall\Redirect301ToPermanentRedirectRector\Fixture;
class SomeClass
{
public function run()
{
\Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
}
}
?>
-----
<?php
namespace Rector\Laravel\Tests\Rector\StaticCall\Redirect301ToPermanentRedirectRector\Fixture;
class SomeClass
{
public function run()
{
\Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
}
}
?>

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace Rector\Laravel\Tests\Rector\StaticCall\Redirect301ToPermanentRedirectRector;
use Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class Redirect301ToPermanentRedirectRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return Redirect301ToPermanentRedirectRector::class;
}
}