-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInMemoryIndex.php
More file actions
255 lines (229 loc) · 7.15 KB
/
Copy pathInMemoryIndex.php
File metadata and controls
255 lines (229 loc) · 7.15 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace Padawan\Framework\Domain\Project;
use Padawan\Domain\Project\File;
use Padawan\Domain\Project\FQCN;
use Padawan\Domain\Project\Index;
use Padawan\Domain\Project\Node\ClassData;
use Padawan\Domain\Project\Node\FunctionData;
use Padawan\Domain\Project\Node\InterfaceData;
class InMemoryIndex implements Index
{
private $files = [];
private $fqcns = [];
private $classes = [];
private $interfaces = [];
private $extends = [];
private $implements = [];
private $functions = [];
/** @var Index $coreIndex */
private static $coreIndex;
public function getFQCNs()
{
return $this->fqcns;
}
public function getImplements() {
return $this->implements;
}
public function getExtends() {
return $this->extends;
}
public function addFile(File $file)
{
$scope = $file->scope();
if (empty($scope)) {
throw new \Exception("Scope in file is empty");
}
foreach ($scope->getClasses() as $classData) {
$this->addFQCN($classData->fqcn);
$this->addClass($classData);
}
foreach ($scope->getInterfaces() as $interfaceData) {
$this->addFQCN($interfaceData->fqcn);
$this->addInterface($interfaceData);
}
foreach ($scope->getFunctions() as $functionData) {
$this->addFunction($functionData);
}
$this->files[$file->path()] = $file;
}
public function findFileByPath($path)
{
if (!array_key_exists($path, $this->files)) {
return null;
}
return $this->files[$path];
}
/**
* @return FQCN
*/
public function findFQCNByFile($file)
{
if (!array_key_exists($file, $this->flippedClassMap)) {
return null;
}
$fqcnStr = $this->flippedClassMap[$file];
if (empty($fqcnStr)) {
return null;
}
if (!array_key_exists($fqcnStr, $this->fqcns)) {
return null;
}
return $this->fqcns[$fqcnStr];
}
/**
* @return ClassData
*/
public function findClassByFQCN(FQCN $fqcn) {
$str = $fqcn->toString();
if (array_key_exists($str, $this->classes)) {
return $this->classes[$str];
}
if ($this->hasCoreIndex()) {
return self::$coreIndex->findClassByFQCN($fqcn);
}
}
/**
* @return InterfaceData
*/
public function findInterfaceByFQCN(FQCN $fqcn) {
$str = $fqcn->toString();
if (array_key_exists($str, $this->interfaces)) {
return $this->interfaces[$str];
}
if ($this->hasCoreIndex()) {
return self::$coreIndex->findInterfaceByFQCN($fqcn);
}
}
/**
* @return FunctionData
*/
public function findFunctionByName($functionName)
{
if (array_key_exists($functionName, $this->functions)) {
return $this->functions[$functionName];
}
if ($this->hasCoreIndex()) {
return self::$coreIndex->findFunctionByName($functionName);
}
}
/**
* @return ClassData[]
*/
public function findClassChildren(FQCN $class) {
if (!array_key_exists($class->toString(), $this->extends)
|| !is_array($this->extends[$class->toString()])
) {
$this->extends[$class->toString()] = [];
}
return $this->extends[$class->toString()];
}
/**
* @return ClassData[]
*/
public function findInterfaceChildrenClasses(FQCN $interface) {
if (!array_key_exists($interface->toString(), $this->implements)
|| !is_array($this->implements[$interface->toString()])
) {
$this->implements[$interface->toString()] = [];
}
return $this->implements[$interface->toString()];
}
/**
* @return ClassData[]
*/
public function getClasses()
{
$classes = $this->classes;
if ($this->hasCoreIndex()) {
$classes = array_merge($classes, self::$coreIndex->getClasses());
}
return $classes;
}
/**
* @return InterfaceData[]
*/
public function getInterfaces()
{
$interfaces = $this->interfaces;
if ($this->hasCoreIndex()) {
$interfaces = array_merge($interfaces, self::$coreIndex->getInterfaces());
}
return $interfaces;
}
/**
* @return FunctionData[]
*/
public function getFunctions()
{
$functions = $this->functions;
if ($this->hasCoreIndex()) {
$functions = array_merge($functions, self::$coreIndex->getFunctions());
}
return $functions;
}
public function addClass(ClassData $class) {
if (!$this->isSemanticsCorrect($class)) {
return;
}
$this->classes[$class->fqcn->toString()] = $class;
if ($class->getParent() instanceof FQCN) {
$this->addExtend($class, $class->getParent());
}
foreach ($class->getInterfaces() as $interface) {
if ($interface instanceof FQCN) {
$this->addImplement($class, $interface);
}
}
foreach ($this->findClassChildren($class->fqcn) AS $child) {
$child->setParent($class);
}
}
public function addInterface(InterfaceData $interface) {
$this->interfaces[$interface->fqcn->toString()] = $interface;
foreach ($this->findInterfaceChildrenClasses($interface->fqcn) as $child) {
$this->addImplement($child, $interface->fqcn);
}
foreach ($interface->getInterfaces() as $parent) {
if ($parent instanceof FQCN) {
$this->addImplement($interface, $parent);
}
}
}
public function addFunction(FunctionData $function)
{
$this->functions[$function->name] = $function;
}
public function addFQCN(FQCN $fqcn) {
$this->fqcns[$fqcn->toString()] = $fqcn;
}
protected function addExtend(ClassData $class, FQCN $parent) {
$this->findClassChildren($parent);
$this->extends[$parent->toString()][$class->fqcn->toString()] = $class;
$parentClass = $this->findClassByFQCN($parent);
if ($parentClass instanceof ClassData) {
$class->setParent($parentClass);
}
}
protected function addImplement($class, FQCN $fqcn) {
$this->findInterfaceChildrenClasses($fqcn);
$this->implements[$fqcn->toString()][$class->fqcn->toString()] = $class;
$interface = $this->findInterfaceByFQCN($fqcn);
if ($interface instanceof InterfaceData) {
$class->addInterface($interface);
}
}
private function hasCoreIndex()
{
return $this !== self::$coreIndex && !empty(self::$coreIndex);
}
private function isSemanticsCorrect(ClassData $class)
{
if ($class->getParent() === $class) {
return false;
}
if ($class->getParent() instanceof FQCN
&& $class->getParent()->toString() === $class->fqcn->toString()) {
return false;
}
}
}