mirror of
https://github.com/rectorphp/rector.git
synced 2024-09-06 15:41:59 +00:00
[Laravel] Add request() to injection
This commit is contained in:
parent
ceca9df4cc
commit
399a8ad549
@ -3,9 +3,12 @@
|
||||
namespace Rector\Laravel\Rector\StaticCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\PhpParser\Node\Manipulator\ClassMethodManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
@ -67,19 +70,15 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [StaticCall::class];
|
||||
return [StaticCall::class, FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StaticCall $node
|
||||
* @param StaticCall|FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isType($node, $this->requestClass)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node, 'validate')) {
|
||||
if ($this->shouldSkip($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -91,6 +90,31 @@ CODE_SAMPLE
|
||||
|
||||
$variable = new Variable($requestName);
|
||||
|
||||
if ($node instanceof FuncCall) {
|
||||
return $variable;
|
||||
}
|
||||
|
||||
return new MethodCall($variable, $node->name, $node->args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StaticCall|FuncCall $node
|
||||
*/
|
||||
private function shouldSkip(Node $node): bool
|
||||
{
|
||||
if ($node instanceof StaticCall) {
|
||||
return ! $this->isType($node, $this->requestClass);
|
||||
}
|
||||
|
||||
if ($node instanceof FuncCall) {
|
||||
$class = $node->getAttribute(Attribute::CLASS_NODE);
|
||||
if (! $class instanceof Class_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ! $this->isName($node, 'request');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Laravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
|
||||
|
||||
class SomeFunction
|
||||
{
|
||||
public function store()
|
||||
{
|
||||
$validatedData = request()->get('foo');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Laravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
|
||||
|
||||
class SomeFunction
|
||||
{
|
||||
public function store(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$validatedData = $request->get('foo');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -9,7 +9,7 @@ final class RequestStaticValidateToInjectRectorTest extends AbstractRectorTestCa
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture-alias.php.inc']);
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture-alias.php.inc', __DIR__ . '/Fixture/function.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
|
Loading…
Reference in New Issue
Block a user