1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.4 |
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\Attributes\Attribute; |
11
|
|
|
use PhUml\Code\ClassDefinition; |
12
|
|
|
use PhUml\Code\Codebase; |
13
|
|
|
use PhUml\Code\Parameters\Parameter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* It checks the attributes and the constructor parameters of a class looking for external definitions |
17
|
|
|
* |
18
|
|
|
* An external definition is either a class or interface from a third party library, or a built-in class or interface |
19
|
|
|
* |
20
|
|
|
* In the case of a third-party library or built-in type a `ClassDefinition` is added by default. |
21
|
|
|
* Although we don't really know if it's an interface since we don't have access to the source code |
22
|
|
|
*/ |
23
|
|
|
final class ExternalAssociationsResolver implements RelationshipsResolver |
24
|
|
|
{ |
25
|
|
|
public function resolve(Codebase $codebase): void |
26
|
|
|
{ |
27
|
|
|
foreach ($codebase->definitions() as $definition) { |
28
|
|
|
if ($definition instanceof ClassDefinition) { |
29
|
|
|
$this->resolveForClass($definition, $codebase); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function resolveForClass(ClassDefinition $definition, Codebase $codebase): void |
35
|
|
|
{ |
36
|
|
|
$this->resolveExternalAttributes($definition, $codebase); |
37
|
|
|
$this->resolveExternalConstructorParameters($definition, $codebase); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function resolveExternalAttributes(ClassDefinition $definition, Codebase $codebase): void |
41
|
|
|
{ |
42
|
|
|
array_map(function (Attribute $attribute) use ($codebase): void { |
43
|
|
|
if ($attribute->isAReference() && ! $codebase->has($attribute->referenceName())) { |
44
|
|
|
$codebase->add(new ClassDefinition($attribute->referenceName())); |
45
|
|
|
} |
46
|
|
|
}, $definition->attributes()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function resolveExternalConstructorParameters(ClassDefinition $definition, Codebase $codebase): void |
50
|
|
|
{ |
51
|
|
|
array_map(function (Parameter $parameter) use ($codebase): void { |
52
|
|
|
if ($parameter->isAReference() && ! $codebase->has($parameter->referenceName())) { |
53
|
|
|
$codebase->add(new ClassDefinition($parameter->referenceName())); |
54
|
|
|
} |
55
|
|
|
}, $definition->constructorParameters()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|