-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
ContextSensitiveCompletor.php
188 lines (167 loc) · 6.84 KB
/
ContextSensitiveCompletor.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Phpactor\Completion\Bridge\WorseReflection\Completor;
use Generator;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\DelimitedList\ArgumentExpressionList;
use Microsoft\PhpParser\Node\Expression\ArgumentExpression;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
use Microsoft\PhpParser\Node\QualifiedName;
use Phpactor\Completion\Bridge\TolerantParser\Qualifier\AlwaysQualfifier;
use Phpactor\Completion\Bridge\TolerantParser\TolerantCompletor;
use Phpactor\Completion\Bridge\TolerantParser\TolerantQualifiable;
use Phpactor\Completion\Bridge\TolerantParser\TolerantQualifier;
use Phpactor\TextDocument\ByteOffset;
use Phpactor\TextDocument\TextDocument;
use Phpactor\WorseReflection\Core\Exception\NotFound;
use Phpactor\WorseReflection\Core\Inference\Context\ClassLikeContext;
use Phpactor\WorseReflection\Core\Inference\Context\MemberAccessContext;
use Phpactor\WorseReflection\Core\Reflection\Collection\ReflectionParameterCollection;
use Phpactor\WorseReflection\Core\Reflection\ReflectionFunctionLike;
use Phpactor\WorseReflection\Core\Reflection\ReflectionMember;
use Phpactor\WorseReflection\Core\Type\ArrayType;
use Phpactor\WorseReflection\Core\Type\ClassLikeType;
use Phpactor\WorseReflection\Core\Util\NodeUtil;
use Phpactor\WorseReflection\Reflector;
class ContextSensitiveCompletor implements TolerantCompletor, TolerantQualifiable
{
public function __construct(private TolerantCompletor $inner, private Reflector $reflector)
{
}
public function complete(Node $node, TextDocument $source, ByteOffset $offset): Generator
{
$generator = $this->inner->complete($node, $source, $offset);
if (
!$node instanceof CallExpression &&
($node instanceof QualifiedName && !$node->parent instanceof ObjectCreationExpression)
) {
yield from $generator;
return $generator->getReturn();
}
$type = $this->resolveFilterableType($node, $source, $offset);
if (null === $type) {
yield from $generator;
return $generator->getReturn();
}
foreach ($generator as $suggestion) {
$fqn = $suggestion->fqn();
if (!$fqn) {
yield $suggestion;
continue;
}
try {
$refection = $this->reflector->reflectClassLike($fqn);
} catch (NotFound $e) {
continue;
}
if (!$refection->isInstanceOf($type->name())) {
continue;
}
yield $suggestion;
}
return $generator->getReturn();
}
public function qualifier(): TolerantQualifier
{
if ($this->inner instanceof TolerantQualifiable) {
return $this->inner->qualifier();
}
return new AlwaysQualfifier();
}
private function resolveFilterableType(Node $node, TextDocument $source, ByteOffset $offset): ?ClassLikeType
{
$argumentNb = 0;
$memberAccessOrObjectCreation = $node;
$node = NodeUtil::firstDescendantNodeBeforeOffset($node, $offset->toInt());
if ($node instanceof QualifiedName) {
$memberAccessOrObjectCreation = null;
$argumentExpression = $node->getFirstAncestor(ArgumentExpression::class);
if ($argumentExpression instanceof ArgumentExpression) {
$list = $argumentExpression->getFirstAncestor(ArgumentExpressionList::class);
if (!$list instanceof ArgumentExpressionList) {
return null;
}
$argumentNb = NodeUtil::argumentOffset($list, $argumentExpression) ?? 0;
$memberAccessOrObjectCreation = $list->parent;
}
}
$argumentList = $node->getFirstAncestor(ArgumentExpressionList::class);
if ($argumentList instanceof ArgumentExpressionList) {
$argumentNb = max(0, count(iterator_to_array($argumentList->getValues())) - 1);
$memberAccessOrObjectCreation = $argumentList->parent;
}
if (!$memberAccessOrObjectCreation instanceof CallExpression && !$memberAccessOrObjectCreation instanceof ObjectCreationExpression) {
return null;
}
$offset = $memberAccessOrObjectCreation->openParen?->getStartPosition();
if (null === $offset) {
return null;
}
try {
$memberAccessOrObjectCreation = $this->reflector->reflectOffset($source, $offset)->nodeContext();
} catch (NotFound $e) {
return null;
}
if ($memberAccessOrObjectCreation instanceof MemberAccessContext) {
return $this->typeFromMemberAccess($memberAccessOrObjectCreation, $argumentNb);
}
if ($memberAccessOrObjectCreation instanceof ClassLikeContext) {
return $this->typeFromClassInstantiation($memberAccessOrObjectCreation, $argumentNb);
}
return null;
}
/**
* @param MemberAccessContext<ReflectionMember> $memberAccessOrObjectCreation
*/
private function typeFromMemberAccess(MemberAccessContext $memberAccessOrObjectCreation, int $argumentNb): ?ClassLikeType
{
try {
$functionLike = $memberAccessOrObjectCreation->accessedMember();
} catch (NotFound) {
return null;
}
if (!$functionLike instanceof ReflectionFunctionLike) {
return null;
}
$parameters = $functionLike->parameters();
return $this->typeFromParameters($parameters, $argumentNb);
}
/**
* @param int<0, max> $argumentNb
*/
private function typeFromClassInstantiation(ClassLikeContext $classLikeContext, int $argumentNb): ?ClassLikeType
{
try {
$classLike = $classLikeContext->classLike();
$constructor = $classLike->methods()->get('__construct');
$parameters = $constructor->parameters();
} catch (NotFound) {
return null;
}
return $this->typeFromParameters($parameters, $argumentNb);
}
private function typeFromParameters(ReflectionParameterCollection $parameters, int $argumentNb): ?ClassLikeType
{
$parameter = $parameters->at($argumentNb);
if (null === $parameter) {
$lastParameter = $parameters->lastOrNull();
if (null === $lastParameter) {
return null;
}
if (!$lastParameter->isVariadic()) {
return null;
}
$parameter = $lastParameter;
}
$type = $parameter->type();
if ($parameter->isVariadic()) {
if ($type instanceof ArrayType) {
$type = $type->iterableValueType();
}
}
if (!$type instanceof ClassLikeType) {
return null;
}
return $type;
}
}