1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.1 |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhUml\Parser\Code; |
9
|
|
|
|
10
|
|
|
use PhUml\Code\ClassDefinition; |
11
|
|
|
use PhUml\Code\Codebase; |
12
|
|
|
use PhUml\Code\InterfaceDefinition; |
13
|
|
|
use PhUml\Code\Name; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* It checks the parent of a definition and the interfaces it implements looking for external |
17
|
|
|
* definitions |
18
|
|
|
* |
19
|
|
|
* An external definition is either a class or interface from a third party library, or a built-in |
20
|
|
|
* class |
21
|
|
|
*/ |
22
|
|
|
class ExternalDefinitionsResolver |
23
|
|
|
{ |
24
|
117 |
|
public function resolve(Codebase $codebase): void |
25
|
|
|
{ |
26
|
117 |
|
foreach ($codebase->definitions() as $definition) { |
27
|
114 |
|
if ($definition instanceof ClassDefinition) { |
28
|
108 |
|
$this->resolveForClass($definition, $codebase); |
29
|
60 |
|
} elseif ($definition instanceof InterfaceDefinition) { |
30
|
114 |
|
$this->resolveForInterface($definition, $codebase); |
31
|
|
|
} |
32
|
|
|
} |
33
|
117 |
|
} |
34
|
|
|
|
35
|
108 |
|
protected function resolveForClass(ClassDefinition $definition, Codebase $codebase): void |
36
|
|
|
{ |
37
|
108 |
|
$this->resolveExternalInterfaces($definition->interfaces(), $codebase); |
38
|
108 |
|
$this->resolveExternalParentClass($definition, $codebase); |
39
|
108 |
|
} |
40
|
|
|
|
41
|
60 |
|
protected function resolveForInterface(InterfaceDefinition $definition, Codebase $codebase): void |
42
|
|
|
{ |
43
|
60 |
|
$this->resolveExternalInterfaces($definition->parents(), $codebase); |
44
|
60 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \PhUml\Code\Name[] $interfaces |
48
|
|
|
* @param Codebase $codebase |
49
|
|
|
*/ |
50
|
114 |
|
private function resolveExternalInterfaces(array $interfaces, Codebase $codebase): void |
51
|
|
|
{ |
52
|
114 |
|
foreach ($interfaces as $interface) { |
53
|
51 |
|
if (!$codebase->has($interface)) { |
54
|
51 |
|
$codebase->add($this->externalInterface($interface)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
114 |
|
} |
58
|
|
|
|
59
|
108 |
|
private function resolveExternalParentClass(ClassDefinition $definition, Codebase $codebase): void |
60
|
|
|
{ |
61
|
108 |
|
if (!$definition->hasParent()) { |
62
|
102 |
|
return; |
63
|
|
|
} |
64
|
54 |
|
$parent = $definition->parent(); |
65
|
54 |
|
if (!$codebase->has($parent)) { |
66
|
48 |
|
$codebase->add($this->externalClass($parent)); |
67
|
|
|
} |
68
|
54 |
|
} |
69
|
|
|
|
70
|
6 |
|
protected function externalInterface(Name $name): InterfaceDefinition |
71
|
|
|
{ |
72
|
6 |
|
return new InterfaceDefinition($name); |
73
|
|
|
} |
74
|
|
|
|
75
|
51 |
|
protected function externalClass(Name $name): ClassDefinition |
76
|
|
|
{ |
77
|
51 |
|
return new ClassDefinition($name); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|