1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @link http://phpdoc.org |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpDocumentor\Reflection\Php; |
16
|
|
|
|
17
|
|
|
use phpDocumentor\Reflection\DocBlock; |
18
|
|
|
use phpDocumentor\Reflection\Element; |
19
|
|
|
use phpDocumentor\Reflection\Fqsen; |
20
|
|
|
use phpDocumentor\Reflection\Location; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Descriptor representing a Class. |
24
|
|
|
*/ |
25
|
|
|
// @codingStandardsIgnoreStart |
26
|
|
|
final class Class_ implements Element |
27
|
|
|
// @codingStandardsIgnoreEnd |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Fqsen Full Qualified Structural Element Name |
31
|
|
|
*/ |
32
|
|
|
private $fqsen; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DocBlock|null |
36
|
|
|
*/ |
37
|
|
|
private $docBlock = null; |
38
|
|
|
|
39
|
|
|
/** @var boolean $abstract Whether this is an abstract class. */ |
40
|
|
|
private $abstract = false; |
41
|
|
|
|
42
|
|
|
/** @var boolean $final Whether this class is marked as final and can't be subclassed. */ |
43
|
|
|
private $final = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Class_ The class this class is extending. |
47
|
|
|
*/ |
48
|
|
|
private $parent = null; |
49
|
|
|
|
50
|
|
|
/** @var Fqsen[] $implements References to interfaces that are implemented by this class. */ |
51
|
|
|
private $implements = []; |
52
|
|
|
|
53
|
|
|
/** @var Constant[] $constants References to constants defined in this class. */ |
54
|
|
|
private $constants = []; |
55
|
|
|
|
56
|
|
|
/** @var Property[] $properties References to properties defined in this class. */ |
57
|
|
|
private $properties = []; |
58
|
|
|
|
59
|
|
|
/** @var Method[] $methods References to methods defined in this class. */ |
60
|
|
|
private $methods = []; |
61
|
|
|
|
62
|
|
|
/** @var Fqsen[] $usedTraits References to traits consumed by this class */ |
63
|
|
|
private $usedTraits = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var null|Location |
67
|
|
|
*/ |
68
|
|
|
private $location; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initializes a number of properties with the given values. Others are initialized by definition. |
72
|
|
|
* |
73
|
|
|
* |
74
|
|
|
* @param Location|null $location |
75
|
|
|
*/ |
76
|
3 |
|
public function __construct( |
77
|
|
|
Fqsen $fqsen, |
78
|
|
|
DocBlock $docBlock = null, |
79
|
|
|
Fqsen $parent = null, |
80
|
|
|
bool $abstract = false, |
81
|
|
|
bool $final = false, |
82
|
|
|
Location $location = null |
83
|
|
|
) { |
84
|
3 |
|
if ($location === null) { |
85
|
3 |
|
$location = new Location(-1); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
$this->fqsen = $fqsen; |
89
|
3 |
|
$this->parent = $parent; |
|
|
|
|
90
|
3 |
|
$this->docBlock = $docBlock; |
91
|
3 |
|
$this->abstract = $abstract; |
92
|
3 |
|
$this->final = $final; |
93
|
3 |
|
$this->location = $location; |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns true when this class is final. Otherwise returns false. |
98
|
|
|
*/ |
99
|
1 |
|
public function isFinal(): bool |
100
|
|
|
{ |
101
|
1 |
|
return $this->final; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns true when this class is abstract. Otherwise returns false. |
106
|
|
|
*/ |
107
|
1 |
|
public function isAbstract(): bool |
108
|
|
|
{ |
109
|
1 |
|
return $this->abstract; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the superclass this class is extending if available. |
114
|
|
|
*/ |
115
|
1 |
|
public function getParent(): ?Fqsen |
116
|
|
|
{ |
117
|
1 |
|
return $this->parent; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the interfaces this class is implementing. |
122
|
|
|
* |
123
|
|
|
* @return Fqsen[] |
124
|
|
|
*/ |
125
|
1 |
|
public function getInterfaces(): array |
126
|
|
|
{ |
127
|
1 |
|
return $this->implements; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Add a interface Fqsen this class is implementing. |
132
|
|
|
*/ |
133
|
1 |
|
public function addInterface(Fqsen $interface): void |
134
|
|
|
{ |
135
|
1 |
|
$this->implements[(string) $interface] = $interface; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the constants of this class. |
140
|
|
|
* |
141
|
|
|
* @return Constant[] |
142
|
|
|
*/ |
143
|
1 |
|
public function getConstants(): array |
144
|
|
|
{ |
145
|
1 |
|
return $this->constants; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add Constant to this class. |
150
|
|
|
*/ |
151
|
1 |
|
public function addConstant(Constant $constant): void |
152
|
|
|
{ |
153
|
1 |
|
$this->constants[(string) $constant->getFqsen()] = $constant; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the methods of this class. |
158
|
|
|
* |
159
|
|
|
* @return Method[] |
160
|
|
|
*/ |
161
|
1 |
|
public function getMethods(): array |
162
|
|
|
{ |
163
|
1 |
|
return $this->methods; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add a method to this class. |
168
|
|
|
*/ |
169
|
1 |
|
public function addMethod(Method $method): void |
170
|
|
|
{ |
171
|
1 |
|
$this->methods[(string) $method->getFqsen()] = $method; |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the properties of this class. |
176
|
|
|
* |
177
|
|
|
* @return Property[] |
178
|
|
|
*/ |
179
|
1 |
|
public function getProperties(): array |
180
|
|
|
{ |
181
|
1 |
|
return $this->properties; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Add a property to this class. |
186
|
|
|
*/ |
187
|
1 |
|
public function addProperty(Property $property): void |
188
|
|
|
{ |
189
|
1 |
|
$this->properties[(string) $property->getFqsen()] = $property; |
190
|
1 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the traits used by this class. |
194
|
|
|
* |
195
|
|
|
* @return Fqsen[] |
196
|
|
|
*/ |
197
|
1 |
|
public function getUsedTraits(): array |
198
|
|
|
{ |
199
|
1 |
|
return $this->usedTraits; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Add trait fqsen used by this class. |
204
|
|
|
*/ |
205
|
1 |
|
public function addUsedTrait(Fqsen $fqsen): void |
206
|
|
|
{ |
207
|
1 |
|
$this->usedTraits[(string) $fqsen] = $fqsen; |
208
|
1 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns the Fqsen of the element. |
212
|
|
|
*/ |
213
|
|
|
public function getFqsen(): Fqsen |
214
|
|
|
{ |
215
|
|
|
return $this->fqsen; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns the name of the element. |
220
|
|
|
*/ |
221
|
|
|
public function getName(): string |
222
|
|
|
{ |
223
|
|
|
return $this->fqsen->getName(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @returns null|DocBlock |
228
|
|
|
*/ |
229
|
|
|
public function getDocBlock(): ?DocBlock |
230
|
|
|
{ |
231
|
|
|
return $this->docBlock; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function getLocation(): ?Location |
235
|
|
|
{ |
236
|
|
|
return $this->location; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.