mirror of
https://github.com/rectorphp/rector.git
synced 2024-09-06 15:41:59 +00:00
[Laravel] Add Redirect301ToPermanentRedirect
This commit is contained in:
parent
2a844ab44d
commit
4a27d61ccd
@ -18,3 +18,4 @@ services:
|
||||
sendResetLinkResponse:
|
||||
name: 'request'
|
||||
type: 'Illuminate\Http\Illuminate\Http'
|
||||
Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector: ~
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user