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