Completed
Push — develop ( 49ed8e...8c2a0b )
by Jaap
09:55
created

src/phpDocumentor/Descriptor/ClassDescriptor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor;
13
14
/**
15
 * Descriptor representing a Class.
16
 */
17
class ClassDescriptor extends DescriptorAbstract implements Interfaces\ClassInterface
18
{
19
    /** @var ClassDescriptor|null $extends Reference to an instance of the superclass for this class, if any. */
20
    protected $parent;
21
22
    /** @var Collection $implements References to interfaces that are implemented by this class. */
23
    protected $implements;
24
25
    /** @var boolean $abstract Whether this is an abstract class. */
26
    protected $abstract = false;
27
28
    /** @var boolean $final Whether this class is marked as final and can't be subclassed. */
29
    protected $final = false;
30
31
    /** @var Collection $constants References to constants defined in this class. */
32
    protected $constants;
33
34
    /** @var Collection $properties References to properties defined in this class. */
35
    protected $properties;
36
37
    /** @var Collection $methods References to methods defined in this class. */
38
    protected $methods;
39
40
    /** @var Collection $usedTraits References to traits consumed by this class */
41
    protected $usedTraits = array();
42
43
    /**
44
     * Initializes the all properties representing a collection with a new Collection object.
45
     */
46
    public function __construct()
47
    {
48
        parent::__construct();
49
50
        $this->setInterfaces(new Collection());
51
        $this->setUsedTraits(new Collection());
52
        $this->setConstants(new Collection());
53
        $this->setProperties(new Collection());
54
        $this->setMethods(new Collection());
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function setParent($parents)
61
    {
62
        $this->parent = $parents;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parents of type object<phpDocumentor\Des...tor\DescriptorAbstract> is incompatible with the declared type object<phpDocumentor\Des...r\ClassDescriptor>|null of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function getParent()
69
    {
70
        return $this->parent;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function setInterfaces(Collection $implements)
77
    {
78
        $this->implements = $implements;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getInterfaces()
85
    {
86
        return $this->implements;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function setFinal($final)
93
    {
94
        $this->final = $final;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function isFinal()
101
    {
102
        return $this->final;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function setAbstract($abstract)
109
    {
110
        $this->abstract = $abstract;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function isAbstract()
117
    {
118
        return $this->abstract;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function setConstants(Collection $constants)
125
    {
126
        $this->constants = $constants;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function getConstants()
133
    {
134
        return $this->constants;
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function getInheritedConstants()
141
    {
142
        if (!$this->getParent() || (!$this->getParent() instanceof ClassDescriptor)) {
143
            return new Collection();
144
        }
145
146
        $inheritedConstants = clone $this->getParent()->getConstants();
147
148
        return $inheritedConstants->merge($this->getParent()->getInheritedConstants());
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154
    public function setMethods(Collection $methods)
155
    {
156
        $this->methods = $methods;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162
    public function getMethods()
163
    {
164
        return $this->methods;
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170
    public function getInheritedMethods()
171
    {
172
        $inheritedMethods = new Collection();
173
174
        foreach ($this->getUsedTraits() as $trait) {
175
            if (!$trait instanceof TraitDescriptor) {
176
                continue;
177
            }
178
179
            $inheritedMethods = $inheritedMethods->merge(clone $trait->getMethods());
180
        }
181
182
        if (!$this->getParent() || (!$this->getParent() instanceof ClassDescriptor)) {
183
            return $inheritedMethods;
184
        }
185
186
        $inheritedMethods = $inheritedMethods->merge(clone $this->getParent()->getMethods());
187
188
        return $inheritedMethods->merge($this->getParent()->getInheritedMethods());
189
    }
190
191
    /**
192
     * @return Collection
193
     */
194
    public function getMagicMethods()
195
    {
196
        /** @var Collection $methodTags */
197
        $methodTags = clone $this->getTags()->get('method', new Collection());
198
199
        $methods = new Collection();
200
201
        /** @var Tag\MethodDescriptor $methodTag */
202
        foreach ($methodTags as $methodTag) {
203
            $method = new MethodDescriptor();
204
            $method->setName($methodTag->getMethodName());
205
            $method->setDescription($methodTag->getDescription());
206
            $method->setStatic($methodTag->isStatic());
207
            $method->setParent($this);
208
209
            $returnTags = $method->getTags()->get('return', new Collection());
210
            $returnTags->add($methodTag->getResponse());
211
212
            foreach ($methodTag->getArguments() as $name => $argument) {
213
                $method->addArgument($name, $argument);
214
            }
215
216
            $methods->add($method);
217
        }
218
219
        if ($this->getParent() instanceof static) {
220
            $methods = $methods->merge($this->getParent()->getMagicMethods());
221
        }
222
223
        return $methods;
224
    }
225
226
    /**
227
     * {@inheritDoc}
228
     */
229
    public function setProperties(Collection $properties)
230
    {
231
        $this->properties = $properties;
232
    }
233
234
    /**
235
     * {@inheritDoc}
236
     */
237
    public function getProperties()
238
    {
239
        return $this->properties;
240
    }
241
242
    /**
243
     * {@inheritDoc}
244
     */
245
    public function getInheritedProperties()
246
    {
247
        $inheritedProperties = new Collection();
248
249
        foreach ($this->getUsedTraits() as $trait) {
250
            if (!$trait instanceof TraitDescriptor) {
251
                continue;
252
            }
253
254
            $inheritedProperties = $inheritedProperties->merge(clone $trait->getProperties());
255
        }
256
257
        if (!$this->getParent() || (!$this->getParent() instanceof ClassDescriptor)) {
258
            return $inheritedProperties;
259
        }
260
261
        $inheritedProperties = $inheritedProperties->merge(clone $this->getParent()->getProperties());
262
263
        return $inheritedProperties->merge($this->getParent()->getInheritedProperties());
264
    }
265
266
    /**
267
     * @return Collection
268
     */
269
    public function getMagicProperties()
270
    {
271
        /** @var Collection $propertyTags */
272
        $propertyTags = clone $this->getTags()->get('property', new Collection());
273
        $propertyTags = $propertyTags->merge($this->getTags()->get('property-read', new Collection()));
274
        $propertyTags = $propertyTags->merge($this->getTags()->get('property-write', new Collection()));
275
276
        $properties = new Collection();
277
278
        /** @var Tag\PropertyDescriptor $propertyTag */
279
        foreach ($propertyTags as $propertyTag) {
280
            $property = new PropertyDescriptor();
281
            $property->setName(ltrim($propertyTag->getVariableName(), '$'));
282
            $property->setDescription($propertyTag->getDescription());
283
            $property->setTypes($propertyTag->getTypes());
284
            $property->setParent($this);
285
286
            $properties->add($property);
287
        }
288
289
        if ($this->getParent() instanceof ClassDescriptor) {
290
            $properties = $properties->merge($this->getParent()->getMagicProperties());
291
        }
292
293
        return $properties;
294
    }
295
296
    /**
297
     * @param string $package
298
     */
299
    public function setPackage($package)
300
    {
301
        parent::setPackage($package);
302
303
        foreach ($this->getConstants() as $constant) {
304
            // TODO #840: Workaround; for some reason there are NULLs in the constants array.
305
            if ($constant) {
306
                $constant->setPackage($package);
307
            }
308
        }
309
310
        foreach ($this->getProperties() as $property) {
311
            // TODO #840: Workaround; for some reason there are NULLs in the properties array.
312
            if ($property) {
313
                $property->setPackage($package);
314
            }
315
        }
316
317
        foreach ($this->getMethods() as $method) {
318
            // TODO #840: Workaround; for some reason there are NULLs in the methods array.
319
            if ($method) {
320
                $method->setPackage($package);
321
            }
322
        }
323
    }
324
325
    /**
326
     * Sets a collection of all traits used by this class.
327
     *
328
     * @param Collection $usedTraits
329
     *
330
     * @return void
331
     */
332
    public function setUsedTraits($usedTraits)
333
    {
334
        $this->usedTraits = $usedTraits;
335
    }
336
337
    /**
338
     * Returns the traits used by this class.
339
     *
340
     * Returned values may either be a string (when the Trait is not in this project) or a TraitDescriptor.
341
     *
342
     * @return Collection
343
     */
344
    public function getUsedTraits()
345
    {
346
        return $this->usedTraits;
347
    }
348
349
    public function getInheritedElement()
350
    {
351
        return $this->getParent();
352
    }
353
}
354