-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
OctaneCompatibilityRule.php
157 lines (123 loc) · 4.65 KB
/
OctaneCompatibilityRule.php
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Rules;
use Illuminate\Contracts\Foundation\Application;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
/**
* @implements Rule<MethodCall>
*/
class OctaneCompatibilityRule implements Rule
{
public function getNodeType(): string
{
return MethodCall::class;
}
/** @param MethodCall $node */
public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof Node\Identifier) {
return [];
}
if (! in_array($node->name->name, ['singleton', 'bind'], true)) {
return [];
}
$args = $node->getArgs();
if (count($args) < 2) {
return [];
}
$calledOnType = $scope->getType($node->var);
if (! $calledOnType instanceof TypeWithClassName) {
return [];
}
if ($calledOnType->getClassName() !== Application::class &&
! (new ObjectType(Application::class))->isSuperTypeOf($calledOnType)->yes()
) {
return [];
}
if (! $args[1]->value instanceof Node\Expr\Closure) {
return [];
}
/** @var Node\Expr\Closure $closure */
$closure = $args[1]->value;
/** @var Node\Param[] $closureParams */
$closureParams = $closure->getParams();
// Closure should have at least one parameter. First param
// is container, second is parameters. If no parameter
// is given we will check for the usage of `$this->app`
if (count($closureParams) < 1) {
return $this->checkForThisAppUsage($scope, $closure);
}
// Using `$app` with `bind` is ok, so we return early
if ($node->name->name === 'bind') {
return [];
}
if (! $closureParams[0]->var instanceof Node\Expr\Variable) {
return [];
}
$containerParameterName = $closureParams[0]->var->name;
$nodes = (new NodeFinder)->find($closure->getStmts(), function (Node $node) use ($containerParameterName): bool {
if (! $node instanceof Node\Expr\New_) {
return false;
}
if (count($node->getArgs()) < 1) {
return false;
}
if (! $node->getArgs()[0]->value instanceof Node\Expr\Variable && ! $node->getArgs()[0]->value instanceof Node\Expr\ArrayDimFetch) {
return false;
}
if ($node->getArgs()[0]->value instanceof Node\Expr\ArrayDimFetch) {
/** @var Node\Expr\Variable $var */
$var = $node->getArgs()[0]->value->var;
if ($var->name !== $containerParameterName) {
return false;
}
if ($node->getArgs()[0]->value->dim === null) {
return false;
}
if (! $node->getArgs()[0]->value->dim instanceof Node\Scalar\String_) {
return false;
}
return in_array($node->getArgs()[0]->value->dim->value, ['request', 'config'], true);
}
if ($node->getArgs()[0]->value->name !== $containerParameterName) {
return false;
}
return true;
});
if (count($nodes) > 0) {
return array_map([$this, 'dependencyInjectionError'], $nodes);
}
return [];
}
/** @return \PHPStan\Rules\RuleError[] */
private function checkForThisAppUsage(Scope $scope, Node\Expr\Closure $closure): array
{
$nodes = (new NodeFinder)->find($closure->getStmts(), function (Node $node): bool {
return $node instanceof Node\Expr\PropertyFetch &&
$node->var instanceof Node\Expr\Variable &&
$node->var->name === 'this' &&
$node->name instanceof Node\Identifier &&
$node->name->name === 'app';
});
if (count($nodes) > 0) {
return array_map([$this, 'dependencyInjectionError'], $nodes);
}
return [];
}
private function dependencyInjectionError(Node $node): RuleError
{
return RuleErrorBuilder::message('Consider using bind method instead or pass a closure.')
->identifier('rules.octane')
->tip('See: https://laravel.com/docs/octane#dependency-injection-and-octane')
->line($node->getAttribute('startLine'))
->build();
}
}