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