|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 8.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\Code; |
|
9
|
|
|
|
|
10
|
|
|
use BadMethodCallException; |
|
11
|
|
|
use PhUml\Code\Methods\Method; |
|
12
|
|
|
use PhUml\Code\Modifiers\CanBeAbstract; |
|
13
|
|
|
use PhUml\Code\Modifiers\Visibility; |
|
14
|
|
|
use PhUml\Code\Parameters\Parameter; |
|
15
|
|
|
use PhUml\Code\Properties\Constant; |
|
16
|
|
|
use PhUml\Code\Properties\HasConstants; |
|
17
|
|
|
use PhUml\Code\Properties\HasProperties; |
|
18
|
|
|
use PhUml\Code\Properties\Property; |
|
19
|
|
|
use PhUml\Code\Properties\WithConstants; |
|
20
|
|
|
use PhUml\Code\Properties\WithProperties; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* It represents a class definition |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ClassDefinition extends Definition implements HasProperties, HasConstants, CanBeAbstract, UseTraits |
|
26
|
|
|
{ |
|
27
|
|
|
use WithProperties; |
|
28
|
|
|
use WithConstants; |
|
29
|
|
|
use WithTraits; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Method[] $methods |
|
33
|
|
|
* @param Constant[] $constants |
|
34
|
|
|
* @param Property[] $properties |
|
35
|
|
|
* @param Name[] $interfaces |
|
36
|
|
|
* @param Name[] $traits |
|
37
|
|
|
*/ |
|
38
|
92 |
|
public function __construct( |
|
39
|
|
|
Name $name, |
|
40
|
|
|
array $methods = [], |
|
41
|
|
|
array $constants = [], |
|
42
|
|
|
private readonly ?Name $parent = null, |
|
|
|
|
|
|
43
|
|
|
array $properties = [], |
|
44
|
|
|
private readonly array $interfaces = [], |
|
45
|
|
|
array $traits = [], |
|
46
|
|
|
private readonly bool $isAttribute = false |
|
47
|
|
|
) { |
|
48
|
92 |
|
parent::__construct($name, $methods); |
|
49
|
92 |
|
$this->constants = $constants; |
|
50
|
92 |
|
$this->properties = $properties; |
|
51
|
92 |
|
$this->traits = $traits; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* This method is used by the `AssociationsBuilder` class to discover associations with other |
|
56
|
|
|
* classes via the constructor |
|
57
|
|
|
* |
|
58
|
|
|
* @return Parameter[] |
|
59
|
|
|
* @see \PhUml\Graphviz\Builders\AssociationsBuilder::fromProperties() for more details |
|
60
|
|
|
*/ |
|
61
|
22 |
|
public function constructorParameters(): array |
|
62
|
|
|
{ |
|
63
|
22 |
|
$constructors = array_filter($this->methods, static fn (Method $method): bool => $method->isConstructor()); |
|
64
|
22 |
|
$constructor = reset($constructors); |
|
65
|
|
|
|
|
66
|
22 |
|
return $constructor === false ? [] : $constructor->parameters(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* This method is used to build the `Summary` of a `Codebase` |
|
71
|
|
|
* |
|
72
|
|
|
* @see Summary::propertiesSummary() for more details |
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function countPropertiesByVisibility(Visibility $modifier): int |
|
75
|
|
|
{ |
|
76
|
7 |
|
return \count(array_filter( |
|
77
|
7 |
|
$this->properties, |
|
78
|
7 |
|
static fn (Property $property): bool => $property->hasVisibility($modifier) |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* This method is used to build the `Summary` of a `Codebase` |
|
84
|
|
|
* |
|
85
|
|
|
* @see Summary::propertiesSummary() for more details |
|
86
|
|
|
*/ |
|
87
|
7 |
|
public function countTypedPropertiesByVisibility(Visibility $modifier): int |
|
88
|
|
|
{ |
|
89
|
7 |
|
return \count(array_filter( |
|
90
|
7 |
|
$this->properties, |
|
91
|
|
|
static fn (Property $property): bool => |
|
92
|
6 |
|
$property->hasTypeDeclaration() && $property->hasVisibility($modifier) |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* It is used by the `ClassGraphBuilder` to create the edges to represent implementation |
|
98
|
|
|
* associations |
|
99
|
|
|
* |
|
100
|
|
|
* @return Name[] |
|
101
|
|
|
* @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details |
|
102
|
|
|
*/ |
|
103
|
43 |
|
public function interfaces(): array |
|
104
|
|
|
{ |
|
105
|
43 |
|
return $this->interfaces; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* It is used by the `ClassGraphBuilder` to create the edge to represent inheritance |
|
110
|
|
|
* |
|
111
|
|
|
* @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details |
|
112
|
|
|
*/ |
|
113
|
19 |
|
public function parent(): Name |
|
114
|
|
|
{ |
|
115
|
19 |
|
if ($this->parent === null) { |
|
116
|
1 |
|
throw new BadMethodCallException('This class does not have a parent class'); |
|
117
|
|
|
} |
|
118
|
18 |
|
return $this->parent; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* It is used by the `ClassGraphBuilder` to determine if an inheritance association should be |
|
123
|
|
|
* created |
|
124
|
|
|
* |
|
125
|
|
|
* @see \PhUml\Graphviz\Builders\ClassGraphBuilder::extractFrom() for more details |
|
126
|
|
|
*/ |
|
127
|
42 |
|
public function hasParent(): bool |
|
128
|
|
|
{ |
|
129
|
42 |
|
return $this->parent !== null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* This method is used when the commands are called with the option `hide-empty-blocks` |
|
134
|
|
|
* |
|
135
|
|
|
* It counts both the properties and the constants of a class |
|
136
|
|
|
* |
|
137
|
|
|
* @see Definition::hasProperties() for more details |
|
138
|
|
|
*/ |
|
139
|
20 |
|
public function hasProperties(): bool |
|
140
|
|
|
{ |
|
141
|
20 |
|
return \count($this->constants) + \count($this->properties) > 0; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* This method is used to determine if the class name should be shown in italics |
|
146
|
|
|
*/ |
|
147
|
20 |
|
public function isAbstract(): bool |
|
148
|
|
|
{ |
|
149
|
20 |
|
return array_filter($this->methods(), static fn (Method $method): bool => $method->isAbstract()) !== []; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
21 |
|
public function isAttribute(): bool |
|
153
|
|
|
{ |
|
154
|
21 |
|
return $this->isAttribute; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|