-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathRequestIsMainRector.php
More file actions
115 lines (97 loc) · 3.06 KB
/
RequestIsMainRector.php
File metadata and controls
115 lines (97 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Rector\Symfony\CodeQuality\Rector\BinaryOp;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\CodeQuality\Rector\BinaryOp\RequestIsMainRector\RequestIsMainRectorTest
*/
final class RequestIsMainRector extends AbstractRector
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns status code numbers to constants', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
class SomeController
{
public function index(Request $request): bool
{
return $request->getRequestType() === HttpKernel::MASTER_REQUEST;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
class SomeController
{
public function index(Request $request): bool
{
return $request->isMasterRequestType();
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BinaryOp::class];
}
/**
* @param BinaryOp $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->left instanceof MethodCall) {
return null;
}
$methodCall = $node->left;
if (! $this->isRequestGetRequestType($methodCall)) {
return null;
}
if (! $this->isHttpKernelMainRequestClassConstFetch($node->right)) {
return null;
}
$requestClassReflection = $this->reflectionProvider->getClass(SymfonyClass::REQUEST);
$methodName = $requestClassReflection->hasMethod('isMainRequest') ? 'isMainRequest' : 'isMasterRequest';
return new MethodCall($methodCall->var, $methodName);
}
private function isRequestGetRequestType(MethodCall $methodCall): bool
{
if (! $this->isName($methodCall->name, 'getRequestType')) {
return false;
}
return $this->isObjectType($methodCall->var, new ObjectType(SymfonyClass::REQUEST));
}
private function isHttpKernelMainRequestClassConstFetch(Expr $expr): bool
{
if (! $expr instanceof ClassConstFetch) {
return false;
}
if (! $this->isNames($expr->class, [SymfonyClass::HTTP_KERNEL_INTERFACE, SymfonyClass::HTTP_KERNEL])) {
return false;
}
return $this->isNames($expr->name, ['MASTER_REQUEST', 'MAIN_REQUEST']);
}
}