Completed
Push — master ( 81db43...f030be )
by Aleh
12s
created

InMemoryIndex::addFQCN()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Padawan\Framework\Domain\Project;
4
5
use Padawan\Domain\Project\File;
6
use Padawan\Domain\Project\FQCN;
7
use Padawan\Domain\Project\Index;
8
use Padawan\Domain\Project\Node\ClassData;
9
use Padawan\Domain\Project\Node\FunctionData;
10
use Padawan\Domain\Project\Node\InterfaceData;
11
12
13
class InMemoryIndex implements Index
14
{
15
    private $files              = [];
16
    private $fqcns              = [];
17
    private $classes            = [];
18
    private $interfaces         = [];
19
    private $extends            = [];
20
    private $implements         = [];
21
    private $functions          = [];
22
23
    /** @var Index $coreIndex */
24
    private static $coreIndex;
25
26
    public function getFQCNs()
27
    {
28
        return $this->fqcns;
29
    }
30
31
    public function getImplements() {
32
        return $this->implements;
33
    }
34
    public function getExtends() {
35
        return $this->extends;
36
    }
37
38
    public function addFile(File $file)
39
    {
40
        $scope = $file->scope();
41
        if (empty($scope)) {
42
            throw new \Exception("Scope in file is empty");
43
        }
44
        foreach ($scope->getClasses() as $classData) {
45
            $this->addFQCN($classData->fqcn);
46
            $this->addClass($classData);
47
        }
48
        foreach ($scope->getInterfaces() as $interfaceData) {
49
            $this->addFQCN($interfaceData->fqcn);
50
            $this->addInterface($interfaceData);
51
        }
52
        foreach ($scope->getFunctions() as $functionData) {
53
            $this->addFunction($functionData);
54
        }
55
        $this->files[$file->path()] = $file;
56
    }
57
58
    public function findFileByPath($path)
59
    {
60
        if (!array_key_exists($path, $this->files)) {
61
            return null;
62
        }
63
        return $this->files[$path];
64
    }
65
66
    /**
67
     * @return FQCN
68
     */
69
    public function findFQCNByFile($file)
70
    {
71
        if (!array_key_exists($file, $this->flippedClassMap)) {
0 ignored issues
show
Bug introduced by
The property flippedClassMap does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
            return null;
73
        }
74
        $fqcnStr = $this->flippedClassMap[$file];
75
        if (empty($fqcnStr)) {
76
            return null;
77
        }
78
        if (!array_key_exists($fqcnStr, $this->fqcns)) {
79
            return null;
80
        }
81
        return $this->fqcns[$fqcnStr];
82
    }
83
84
    /**
85
     * @return ClassData
86
     */
87 View Code Duplication
    public function findClassByFQCN(FQCN $fqcn) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
        $str = $fqcn->toString();
89
        if (array_key_exists($str, $this->classes)) {
90
            return $this->classes[$str];
91
        }
92
        if ($this->hasCoreIndex()) {
93
            return self::$coreIndex->findClassByFQCN($fqcn);
94
        }
95
    }
96
97
    /**
98
     * @return InterfaceData
99
     */
100 View Code Duplication
    public function findInterfaceByFQCN(FQCN $fqcn) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
        $str = $fqcn->toString();
102
        if (array_key_exists($str, $this->interfaces)) {
103
            return $this->interfaces[$str];
104
        }
105
        if ($this->hasCoreIndex()) {
106
            return self::$coreIndex->findInterfaceByFQCN($fqcn);
107
        }
108
    }
109
110
    /**
111
     * @return FunctionData
112
     */
113
    public function findFunctionByName($functionName)
114
    {
115
        if (array_key_exists($functionName, $this->functions)) {
116
            return $this->functions[$functionName];
117
        }
118
        if ($this->hasCoreIndex()) {
119
            return self::$coreIndex->findFunctionByName($functionName);
120
        }
121
    }
122
123
    /**
124
     * @return ClassData[]
125
     */
126 View Code Duplication
    public function findClassChildren(FQCN $class) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
        if (!array_key_exists($class->toString(), $this->extends)
128
            || !is_array($this->extends[$class->toString()])
129
        ) {
130
            $this->extends[$class->toString()] = [];
131
        }
132
        return $this->extends[$class->toString()];
133
    }
134
135
    /**
136
     * @return ClassData[]
137
     */
138 View Code Duplication
    public function findInterfaceChildrenClasses(FQCN $interface) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
        if (!array_key_exists($interface->toString(), $this->implements)
140
            || !is_array($this->implements[$interface->toString()])
141
        ) {
142
            $this->implements[$interface->toString()] = [];
143
        }
144
        return $this->implements[$interface->toString()];
145
    }
146
147
    /**
148
     * @return ClassData[]
149
     */
150
    public function getClasses()
151
    {
152
        $classes = $this->classes;
153
        if ($this->hasCoreIndex()) {
154
            $classes = array_merge($classes, self::$coreIndex->getClasses());
155
        }
156
        return $classes;
157
    }
158
159
    /**
160
     * @return InterfaceData[]
161
     */
162
    public function getInterfaces()
163
    {
164
        $interfaces = $this->interfaces;
165
        if ($this->hasCoreIndex()) {
166
            $interfaces = array_merge($interfaces, self::$coreIndex->getInterfaces());
167
        }
168
        return $interfaces;
169
    }
170
171
    /**
172
     * @return FunctionData[]
173
     */
174
    public function getFunctions()
175
    {
176
        $functions = $this->functions;
177
        if ($this->hasCoreIndex()) {
178
            $functions = array_merge($functions, self::$coreIndex->getFunctions());
179
        }
180
        return $functions;
181
    }
182
183
    public function addClass(ClassData $class) {
184
        $this->classes[$class->fqcn->toString()] = $class;
185
        if ($class->getParent() instanceof FQCN) {
186
            $this->addExtend($class, $class->getParent());
187
        }
188
        foreach ($class->getInterfaces() as $interface) {
189
            if ($interface instanceof FQCN) {
190
                $this->addImplement($class, $interface);
191
            }
192
        }
193
        foreach ($this->findClassChildren($class->fqcn) AS $child) {
194
            $child->setParent($class);
195
        }
196
    }
197
198
    public function addInterface(InterfaceData $interface) {
199
        $this->interfaces[$interface->fqcn->toString()] = $interface;
200
        foreach ($this->findInterfaceChildrenClasses($interface->fqcn) as $child) {
201
            $this->addImplement($child, $interface->fqcn);
202
        }
203
        foreach ($interface->getInterfaces() as $parent) {
204
            if ($parent instanceof FQCN) {
205
                $this->addImplement($interface, $parent);
206
            }
207
        }
208
    }
209
210
    public function addFunction(FunctionData $function)
211
    {
212
        $this->functions[$function->name] = $function;
213
    }
214
215
    public function addFQCN(FQCN $fqcn) {
216
        $this->fqcns[$fqcn->toString()] = $fqcn;
217
    }
218
219
    protected function addExtend(ClassData $class, FQCN $parent) {
220
        $this->findClassChildren($parent);
221
        $this->extends[$parent->toString()][$class->fqcn->toString()] = $class;
222
        $parentClass = $this->findClassByFQCN($parent);
223
        if ($parentClass instanceof ClassData) {
224
            $class->setParent($parentClass);
225
        }
226
    }
227
228
    protected function addImplement($class, FQCN $fqcn) {
229
        $this->findInterfaceChildrenClasses($fqcn);
230
        $this->implements[$fqcn->toString()][$class->fqcn->toString()] = $class;
231
        $interface = $this->findInterfaceByFQCN($fqcn);
232
        if ($interface instanceof InterfaceData) {
233
            $class->addInterface($interface);
234
        }
235
    }
236
237
    private function hasCoreIndex()
238
    {
239
        return $this !== self::$coreIndex && !empty(self::$coreIndex);
240
    }
241
}
242