Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

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

assigning incompatible types to properties.

Bug Documentation Major

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
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
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor;
17
18
/**
19
 * Descriptor representing a Class.
20
 */
21
class ClassDescriptor extends DescriptorAbstract implements Interfaces\ClassInterface
22
{
23
    /** @var ClassDescriptor|null $extends Reference to an instance of the superclass for this class, if any. */
24
    protected $parent;
25
26
    /** @var Collection $implements References to interfaces that are implemented by this class. */
27
    protected $implements;
28
29
    /** @var boolean $abstract Whether this is an abstract class. */
30
    protected $abstract = false;
31
32
    /** @var boolean $final Whether this class is marked as final and can't be subclassed. */
33
    protected $final = false;
34
35
    /** @var Collection $constants References to constants defined in this class. */
36
    protected $constants;
37
38
    /** @var Collection $properties References to properties defined in this class. */
39
    protected $properties;
40
41
    /** @var Collection $methods References to methods defined in this class. */
42
    protected $methods;
43
44
    /** @var Collection $usedTraits References to traits consumed by this class */
45
    protected $usedTraits;
46
47
    /**
48
     * Initializes the all properties representing a collection with a new Collection object.
49
     */
50 1
    public function __construct()
51
    {
52 1
        parent::__construct();
53
54 1
        $this->setInterfaces(new Collection());
55 1
        $this->setUsedTraits(new Collection());
56 1
        $this->setConstants(new Collection());
57 1
        $this->setProperties(new Collection());
58 1
        $this->setMethods(new Collection());
59 1
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 2
    public function setParent($parents)
65
    {
66 2
        $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...
67 2
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 1
    public function getParent()
73
    {
74 1
        return $this->parent;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function setInterfaces(Collection $implements)
81
    {
82 1
        $this->implements = $implements;
83 1
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 1
    public function getInterfaces()
89
    {
90 1
        return $this->implements;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 1
    public function setFinal($final)
97
    {
98 1
        $this->final = $final;
99 1
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 1
    public function isFinal()
105
    {
106 1
        return $this->final;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 1
    public function setAbstract($abstract)
113
    {
114 1
        $this->abstract = $abstract;
115 1
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 1
    public function isAbstract()
121
    {
122 1
        return $this->abstract;
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 1
    public function setConstants(Collection $constants)
129
    {
130 1
        $this->constants = $constants;
131 1
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 1
    public function getConstants()
137
    {
138 1
        return $this->constants;
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144 2
    public function getInheritedConstants()
145
    {
146 2
        if (!$this->getParent() || (!$this->getParent() instanceof self)) {
147 1
            return new Collection();
148
        }
149
150 1
        $inheritedConstants = clone $this->getParent()->getConstants();
151
152 1
        return $inheritedConstants->merge($this->getParent()->getInheritedConstants());
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 1
    public function setMethods(Collection $methods)
159
    {
160 1
        $this->methods = $methods;
161 1
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 1
    public function getMethods()
167
    {
168 1
        return $this->methods;
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174 4
    public function getInheritedMethods()
175
    {
176 4
        $inheritedMethods = new Collection();
177
178 4
        foreach ($this->getUsedTraits() as $trait) {
179 2
            if (!$trait instanceof TraitDescriptor) {
180 1
                continue;
181
            }
182
183 1
            $inheritedMethods = $inheritedMethods->merge(clone $trait->getMethods());
184
        }
185
186 4
        if (!$this->getParent() || (!$this->getParent() instanceof self)) {
187 3
            return $inheritedMethods;
188
        }
189
190 1
        $inheritedMethods = $inheritedMethods->merge(clone $this->getParent()->getMethods());
191
192 1
        return $inheritedMethods->merge($this->getParent()->getInheritedMethods());
193
    }
194
195
    /**
196
     * @return Collection
197
     */
198 2
    public function getMagicMethods()
199
    {
200
        /** @var Collection $methodTags */
201 2
        $methodTags = clone $this->getTags()->get('method', new Collection());
202
203 2
        $methods = new Collection();
204
205
        /** @var Tag\MethodDescriptor $methodTag */
206 2
        foreach ($methodTags as $methodTag) {
207 2
            $method = new MethodDescriptor();
208 2
            $method->setName($methodTag->getMethodName());
209 2
            $method->setDescription($methodTag->getDescription());
210 2
            $method->setStatic($methodTag->isStatic());
211 2
            $method->setParent($this);
212
213 2
            $returnTags = $method->getTags()->get('return', new Collection());
214 2
            $returnTags->add($methodTag->getResponse());
215
216 2
            foreach ($methodTag->getArguments() as $name => $argument) {
217
                $method->addArgument($name, $argument);
218
            }
219
220 2
            $methods->add($method);
221
        }
222
223 2
        if ($this->getParent() instanceof static) {
224 2
            $methods = $methods->merge($this->getParent()->getMagicMethods());
225
        }
226
227 2
        return $methods;
228
    }
229
230
    /**
231
     * {@inheritDoc}
232
     */
233 1
    public function setProperties(Collection $properties)
234
    {
235 1
        $this->properties = $properties;
236 1
    }
237
238
    /**
239
     * {@inheritDoc}
240
     */
241 1
    public function getProperties()
242
    {
243 1
        return $this->properties;
244
    }
245
246
    /**
247
     * {@inheritDoc}
248
     */
249 4
    public function getInheritedProperties()
250
    {
251 4
        $inheritedProperties = new Collection();
252
253 4
        foreach ($this->getUsedTraits() as $trait) {
254 2
            if (!$trait instanceof TraitDescriptor) {
255 1
                continue;
256
            }
257
258 1
            $inheritedProperties = $inheritedProperties->merge(clone $trait->getProperties());
259
        }
260
261 4
        if (!$this->getParent() || (!$this->getParent() instanceof self)) {
262 3
            return $inheritedProperties;
263
        }
264
265 1
        $inheritedProperties = $inheritedProperties->merge(clone $this->getParent()->getProperties());
266
267 1
        return $inheritedProperties->merge($this->getParent()->getInheritedProperties());
268
    }
269
270
    /**
271
     * @return Collection
272
     */
273 1
    public function getMagicProperties()
274
    {
275
        /** @var Collection $propertyTags */
276 1
        $propertyTags = clone $this->getTags()->get('property', new Collection());
277 1
        $propertyTags = $propertyTags->merge($this->getTags()->get('property-read', new Collection()));
278 1
        $propertyTags = $propertyTags->merge($this->getTags()->get('property-write', new Collection()));
279
280 1
        $properties = new Collection();
281
282
        /** @var Tag\PropertyDescriptor $propertyTag */
283 1
        foreach ($propertyTags as $propertyTag) {
284 1
            $property = new PropertyDescriptor();
285 1
            $property->setName(ltrim($propertyTag->getVariableName(), '$'));
286 1
            $property->setDescription($propertyTag->getDescription());
287 1
            $property->setType($propertyTag->getType());
288 1
            $property->setParent($this);
289
290 1
            $properties->add($property);
291
        }
292
293 1
        if ($this->getParent() instanceof self) {
294 1
            $properties = $properties->merge($this->getParent()->getMagicProperties());
295
        }
296
297 1
        return $properties;
298
    }
299
300
    /**
301
     * @param PackageDescriptor $package
302
     */
303 1
    public function setPackage($package)
304
    {
305 1
        parent::setPackage($package);
306
307 1
        foreach ($this->getConstants() as $constant) {
308
            // TODO #840: Workaround; for some reason there are NULLs in the constants array.
309 1
            if ($constant) {
310 1
                $constant->setPackage($package);
311
            }
312
        }
313
314 1
        foreach ($this->getProperties() as $property) {
315
            // TODO #840: Workaround; for some reason there are NULLs in the properties array.
316 1
            if ($property) {
317 1
                $property->setPackage($package);
318
            }
319
        }
320
321 1
        foreach ($this->getMethods() as $method) {
322
            // TODO #840: Workaround; for some reason there are NULLs in the methods array.
323 1
            if ($method) {
324 1
                $method->setPackage($package);
325
            }
326
        }
327 1
    }
328
329
    /**
330
     * Sets a collection of all traits used by this class.
331
     *
332
     * @param Collection $usedTraits
333
     */
334
    public function setUsedTraits($usedTraits)
335
    {
336
        $this->usedTraits = $usedTraits;
337
    }
338
339
    /**
340
     * Returns the traits used by this class.
341
     *
342
     * Returned values may either be a string (when the Trait is not in this project) or a TraitDescriptor.
343
     *
344
     * @return Collection
345
     */
346
    public function getUsedTraits()
347
    {
348
        return $this->usedTraits;
349
    }
350
351
    public function getInheritedElement()
352
    {
353
        return $this->getParent();
354
    }
355
}
356