-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAbstractContext.php
206 lines (162 loc) · 5.16 KB
/
AbstractContext.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace App\Contexts;
use Illuminate\Support\Arr;
use Microsoft\PhpParser\Range;
abstract class AbstractContext
{
public array $children = [];
/**
* Whether this context can be found as last result
* in findAutocompleting method
*/
public bool $findable = false;
public bool $autocompleting = false;
protected array $freshObject;
protected bool $hasChildren = true;
public ?AbstractContext $parent = null;
public array $start = [];
public array $end = [];
abstract public function type(): string;
public function castToArray(): array
{
return [];
}
public function __construct(protected $label = '')
{
if (method_exists($this, 'init')) {
call_user_func([$this, 'init']);
}
$this->freshObject = $this->freshArray();
}
public function flip()
{
return array_merge(
Arr::except($this->toArray(), ['children']),
['parent' => $this->parent?->flip()],
);
}
public function findAutocompleting(?AbstractContext $context = null)
{
$context = $context ?? $this;
$result = $this->searchForAutocompleting($context, true);
$lastResult = null;
while ($result !== null) {
$lastResult = $result;
$result = $this->searchForAutocompleting($result);
}
return $lastResult;
}
protected function searchForAutocompleting(AbstractContext $context, $checkCurrent = false)
{
if ($checkCurrent && $context->autocompleting && $context->findable) {
return $context;
}
$publicProps = Arr::except(get_object_vars($context), ['freshObject', 'parent']);
foreach ($publicProps as $child) {
$child = is_array($child) ? $child : [$child];
foreach ($child as $subChild) {
if ($subChild instanceof AbstractContext) {
$result = $this->findAutocompleting($subChild);
if ($result) {
return $result;
}
}
}
}
return null;
}
protected function freshArray()
{
return $this->toArray();
}
public function initNew(AbstractContext $newContext)
{
$newContext->parent = $this;
$this->children[] = $newContext;
return $newContext;
}
public function searchForVar(?string $name): AssignmentValue|string|null
{
if ($name === null) {
return null;
}
if (property_exists($this, 'parameters') && $this->parameters instanceof Parameters) {
foreach ($this->parameters->children as $param) {
if ($param->name === $name) {
return $param->types[0] ?? null;
}
}
}
foreach ($this->children as $child) {
if ($child instanceof Assignment && $child->name === $name) {
return $child->value;
}
}
return $this->parent?->searchForVar($name) ?? null;
}
public function addPropertyToNearestClassDefinition(?string $name, $types = [])
{
if ($name === null) {
return;
}
if ($this instanceof ClassDefinition) {
$this->properties[] = [
'name' => $name,
'types' => $types,
];
} else {
$this->parent?->addPropertyToNearestClassDefinition($name, $types);
}
}
public function nearestClassDefinition()
{
if ($this instanceof ClassDefinition) {
return $this;
}
return $this->parent?->nearestClassDefinition() ?? null;
}
public function searchForProperty(string $name)
{
if ($this instanceof ClassDefinition) {
return collect($this->properties)->first(fn ($prop) => $prop['name'] === $name);
}
return $this->parent?->searchForProperty($name) ?? null;
}
public function pristine(): bool
{
return $this->freshObject === $this->freshArray();
}
public function touched(): bool
{
return !$this->pristine();
}
public function toArray(): array
{
return array_merge(
['type' => $this->type()],
$this->autocompleting ? ['autocompleting' => true] : [],
$this->castToArray(),
($this->label !== '') ? ['label' => $this->label] : [],
(count($this->start) > 0) ? ['start' => $this->start] : [],
(count($this->end) > 0) ? ['end' => $this->end] : [],
($this->hasChildren)
? ['children' => array_map(fn ($child) => $child->toArray(), $this->children)]
: [],
);
}
public function toJson($flags = 0)
{
return json_encode($this->toArray(), $flags);
}
public function setPosition(Range $range)
{
$this->start = [
'line' => $range->start->line,
'column' => $range->start->character,
];
$this->end = [
'line' => $range->end->line,
'column' => $range->end->character,
];
}
}