Skip to content

Commit 7aae907

Browse files
fix(RequestStaticValidateToInjectRector): check extends method (#68)
* fix(RequestStaticValidateToInjectRector): check extends method Co-authored-by: Anthony Clark <[email protected]>
1 parent 077a1d7 commit 7aae907

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

src/Rector/StaticCall/RequestStaticValidateToInjectRector.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use PhpParser\Node\Expr\Variable;
1212
use PhpParser\Node\Identifier;
1313
use PhpParser\Node\Stmt\Class_;
14+
use PhpParser\Node\Stmt\ClassMethod;
1415
use PHPStan\Type\ObjectType;
1516
use Rector\Core\NodeManipulator\ClassMethodManipulator;
1617
use Rector\Core\Rector\AbstractRector;
18+
use Rector\Core\Reflection\ReflectionResolver;
1719
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1820
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1921

@@ -29,7 +31,8 @@ final class RequestStaticValidateToInjectRector extends AbstractRector
2931
private array $requestObjectTypes = [];
3032

3133
public function __construct(
32-
private readonly ClassMethodManipulator $classMethodManipulator
34+
private readonly ClassMethodManipulator $classMethodManipulator,
35+
private readonly ReflectionResolver $reflectionResolver
3336
) {
3437
$this->requestObjectTypes = [new ObjectType('Illuminate\Http\Request'), new ObjectType('Request')];
3538
}
@@ -77,7 +80,7 @@ public function getNodeTypes(): array
7780
}
7881

7982
/**
80-
* @param StaticCall|FuncCall $node
83+
* @param StaticCall|FuncCall $node
8184
*/
8285
public function refactor(Node $node): ?Node
8386
{
@@ -120,6 +123,15 @@ private function shouldSkip(StaticCall|FuncCall $node): bool
120123
return true;
121124
}
122125

126+
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
127+
if ($classMethod instanceof ClassMethod) {
128+
$classMethodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod);
129+
if ($classMethodReflection?->getPrototype()?->getDeclaringClass()?->getName() !== $class->namespacedName?->toString()
130+
) {
131+
return true;
132+
}
133+
}
134+
123135
return ! $this->isName($node, 'request');
124136
}
125137
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
4+
5+
class FunctionExt1
6+
{
7+
public function store()
8+
{
9+
}
10+
}
11+
12+
class FunctionExt2 extends FunctionExt1
13+
{
14+
public function store()
15+
{
16+
$validatedData = request('foo');
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace RectorLaravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
25+
26+
class FunctionExt1
27+
{
28+
public function store()
29+
{
30+
}
31+
}
32+
33+
class FunctionExt2 extends FunctionExt1
34+
{
35+
public function store()
36+
{
37+
$validatedData = request('foo');
38+
}
39+
}
40+
41+
?>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
4+
5+
class FunctionExt1
6+
{
7+
public function store()
8+
{
9+
}
10+
}
11+
trait FunctionTrait{
12+
public function store()
13+
{
14+
$validatedData = request('foo');
15+
}
16+
}
17+
class FunctionExt2 extends FunctionExt1
18+
{
19+
use FunctionTrait;
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace RectorLaravel\Tests\Rector\StaticCall\RequestStaticValidateToInjectRector\Fixture;
27+
28+
class FunctionExt1
29+
{
30+
public function store()
31+
{
32+
}
33+
}
34+
trait FunctionTrait{
35+
public function store()
36+
{
37+
$validatedData = request('foo');
38+
}
39+
}
40+
class FunctionExt2 extends FunctionExt1
41+
{
42+
use FunctionTrait;
43+
}
44+
45+
?>

0 commit comments

Comments
 (0)