-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMemberAccessExpressionParser.php
101 lines (77 loc) · 3.1 KB
/
MemberAccessExpressionParser.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
<?php
namespace App\Parsers;
use App\Contexts\AbstractContext;
use App\Contexts\AssignmentValue;
use App\Contexts\MethodCall;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\MemberAccessExpression;
use Microsoft\PhpParser\Node\Expression\Variable;
use Microsoft\PhpParser\Node\QualifiedName;
class MemberAccessExpressionParser extends AbstractParser
{
/**
* @var MethodCall
*/
protected AbstractContext $context;
/**
* Check if the node has a object operator and
* is a last element in the string
*/
private function hasObjectOperator(MemberAccessExpression $node): bool
{
$name = $node->memberName->getFullText($node->getRoot()->getFullText());
return preg_match('/->' . $name . '->;$/s', $node->getFileContents());
}
public function parse(MemberAccessExpression $node)
{
if ($this->hasObjectOperator($node)) {
$this->context->autocompleting = true;
}
$this->context->methodName = $node->memberName->getFullText($node->getRoot()->getFullText());
foreach ($node->getDescendantNodes() as $child) {
if ($child instanceof QualifiedName) {
$this->context->className ??= (string) ($child->getResolvedName() ?? $child->getText());
return $this->context;
}
if ($child instanceof Variable) {
if ($child->getName() === 'this') {
$parent = $child->getParent();
if ($parent?->getParent() instanceof CallExpression) {
// They are calling a method on the current class
$result = $this->context->nearestClassDefinition();
if ($result) {
$this->context->className = $result->className;
}
continue;
}
if ($parent instanceof MemberAccessExpression) {
$propName = $parent->memberName->getFullText($node->getRoot()->getFullText());
$result = $this->context->searchForProperty($propName);
if ($result) {
$this->context->className = $result['types'][0] ?? null;
}
}
continue;
}
$varName = $child->getName();
$result = $this->context->searchForVar($varName);
if (!$result) {
return $this->context;
}
if ($result instanceof AssignmentValue) {
$this->context->className = $result->getValue()['name'] ?? null;
} else {
$this->context->className = $result;
}
}
}
return $this->context;
}
public function initNewContext(): ?AbstractContext
{
if (!($this->context instanceof MethodCall) || $this->context->methodName !== null) {
return new MethodCall;
}
return null;
}
}