|
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
|
30 |
|
{ |
|
27
|
|
|
foreach ($codebase->definitions() as $definition) { |
|
28
|
30 |
|
if ($definition instanceof ClassDefinition) { |
|
29
|
30 |
|
$this->resolveForClass($definition, $codebase); |
|
30
|
30 |
|
} |
|
31
|
30 |
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function resolveForClass(ClassDefinition $definition, Codebase $codebase): void |
|
35
|
30 |
|
{ |
|
36
|
15 |
|
$this->resolveExternalAttributes($definition, $codebase); |
|
37
|
3 |
|
$this->resolveExternalConstructorParameters($definition, $codebase); |
|
38
|
|
|
} |
|
39
|
30 |
|
|
|
40
|
30 |
|
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
|
30 |
|
$codebase->add(new ClassDefinition($attribute->referenceName())); |
|
45
|
18 |
|
} |
|
46
|
6 |
|
}, $definition->attributes()); |
|
47
|
|
|
} |
|
48
|
30 |
|
|
|
49
|
10 |
|
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
|
|
|
|