DescriptorAbstract::setPackage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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
use phpDocumentor\Descriptor\Filter\Filterable;
19
20
/**
21
 * Base class for descriptors containing the most used options.
22
 */
23
abstract class DescriptorAbstract implements Descriptor, Filterable
24
{
25
    /**
26
     * @var string Fully Qualified Structural Element Name; the FQCN including method, property of constant name
27
     */
28
    protected $fqsen = '';
29
30
    /** @var string $name The local name for this element */
31
    protected $name = '';
32
33
    /** @var NamespaceDescriptor|string $namespace The namespace for this element */
34
    protected $namespace = '';
35
36
    /** @var PackageDescriptor $package The package with which this element is associated */
37
    protected $package;
38
39
    /** @var string $summary A summary describing the function of this element in short. */
40
    protected $summary = '';
41
42
    /** @var string $description A more extensive description of this element. */
43
    protected $description = '';
44
45
    /** @var FileDescriptor|null $file The file to which this element belongs; if applicable */
46
    protected $fileDescriptor;
47
48
    /** @var int $line The line number on which this element occurs. */
49
    protected $line = 0;
50
51
    /** @var Collection $tags The tags associated with this element. */
52
    protected $tags;
53
54
    /** @var Collection $errors A list of errors found while building this element. */
55
    protected $errors;
56
57
    /** @var DescriptorAbstract|null the element from which to inherit information in this element */
58
    protected $inheritedElement = null;
59
60
    /**
61
     * Initializes this descriptor.
62
     */
63 2
    public function __construct()
64
    {
65 2
        $this->setTags(new Collection());
66 2
        $this->setErrors(new Collection());
67 2
    }
68
69
    /**
70
     * Sets the Fully Qualified Structural Element Name (FQSEN) for this element.
71
     *
72
     * @param string $name
73
     */
74 2
    public function setFullyQualifiedStructuralElementName($name)
75
    {
76 2
        $this->fqsen = $name;
77 2
    }
78
79
    /**
80
     * Returns the Fully Qualified Structural Element Name (FQSEN) for this element.
81
     *
82
     * @return string
83
     */
84 1
    public function getFullyQualifiedStructuralElementName()
85
    {
86 1
        return $this->fqsen;
87
    }
88
89
    /**
90
     * Sets the local name for this element.
91
     *
92
     * @param string $name
93
     */
94 2
    public function setName($name)
95
    {
96 2
        $this->name = $name;
97 2
    }
98
99
    /**
100
     * Returns the local name for this element.
101
     *
102
     * @return string
103
     */
104 1
    public function getName()
105
    {
106 1
        return $this->name;
107
    }
108
109
    /**
110
     * Sets the namespace (name) for this element.
111
     *
112
     * @param NamespaceDescriptor|string $namespace
113
     */
114 1
    public function setNamespace($namespace)
115
    {
116 1
        $this->namespace = $namespace;
117 1
    }
118
119
    /**
120
     * Returns the namespace for this element (defaults to global "\")
121
     *
122
     * @return NamespaceDescriptor|string
123
     */
124 1
    public function getNamespace()
125
    {
126 1
        return $this->namespace;
127
    }
128
129
    /**
130
     * Sets the summary describing this element in short.
131
     *
132
     * @param string $summary
133
     */
134 1
    public function setSummary($summary)
135
    {
136 1
        $this->summary = $summary;
137 1
    }
138
139
    /**
140
     * Returns the summary which describes this element.
141
     *
142
     * This method will automatically attempt to inherit the parent's summary if this one has none.
143
     *
144
     * @return string
145
     */
146 7
    public function getSummary()
147
    {
148 7
        if ($this->summary && strtolower(trim($this->summary)) !== '{@inheritdoc}') {
149 7
            return $this->summary;
150
        }
151
152 7
        $parent = $this->getInheritedElement();
153 7
        if ($parent instanceof self) {
154 6
            return $parent->getSummary();
155
        }
156
157 1
        return $this->summary;
158
    }
159
160
    /**
161
     * Sets a description for this element.
162
     *
163
     * @param string $description
164
     */
165 1
    public function setDescription($description)
166
    {
167 1
        $this->description = $description;
168 1
    }
169
170
    /**
171
     * Returns the description for this element.
172
     *
173
     * This method will automatically attempt to inherit the parent's description if this one has none.
174
     *
175
     * @return string
176
     */
177 14
    public function getDescription()
178
    {
179 14
        if ($this->description && strpos(strtolower((string) $this->description), '{@inheritdoc}') === false) {
180 14
            return $this->description;
181
        }
182
183 13
        $parentElement = $this->getInheritedElement();
184 13
        if ($parentElement instanceof self) {
185 12
            $parentDescription = $parentElement->getDescription();
186
187 12
            return $this->description
188 6
                ? str_ireplace('{@inheritdoc}', $parentDescription, $this->description)
189 12
                : $parentDescription;
190
        }
191
192 1
        return $this->description;
193
    }
194
195
    /**
196
     * Sets the file and linenumber where this element is at.
197
     *
198
     * @param int $line
199
     */
200 1
    public function setLocation(FileDescriptor $file, $line = 0)
201
    {
202 1
        $this->setFile($file);
203 1
        $this->line = $line;
204 1
    }
205
206
    /**
207
     * Returns the path to the file containing this element relative to the project's root.
208
     *
209
     * @return string
210
     */
211 1
    public function getPath()
212
    {
213 1
        return $this->fileDescriptor ? $this->fileDescriptor->getPath() : '';
214
    }
215
216
    /**
217
     * Returns the file in which this element resides or null in case the element is not bound to a file..
218
     *
219
     * @return FileDescriptor|null
220
     */
221 1
    public function getFile()
222
    {
223 1
        return $this->fileDescriptor;
224
    }
225
226
    /**
227
     * Sets the file to which this element is associated.
228
     *
229
     * @return bool
230
     */
231
    public function setFile(FileDescriptor $file)
232
    {
233
        $this->fileDescriptor = $file;
234
    }
235
236
    /**
237
     * Returns the line number where the definition for this element can be found.
238
     *
239
     * @return int
240
     */
241 2
    public function getLine()
242
    {
243 2
        return $this->line;
244
    }
245
246
    /**
247
     * Sets the line number for this element's location in the source file.
248
     *
249
     * @param integer $lineNumber
250
     */
251 1
    public function setLine($lineNumber)
252
    {
253 1
        $this->line = $lineNumber;
254 1
    }
255
256
    /**
257
     * Sets the tags associated with this element.
258
     */
259 2
    public function setTags(Collection $tags)
260
    {
261 2
        $this->tags = $tags;
262 2
    }
263
264
    /**
265
     * Returns the tags associated with this element.
266
     *
267
     * @return Collection
268
     */
269 1
    public function getTags()
270
    {
271 1
        return $this->tags;
272
    }
273
274
    /**
275
     * Sets the name of the package to which this element belongs.
276
     *
277
     * @param PackageDescriptor $package
278
     */
279 1
    public function setPackage($package)
280
    {
281 1
        $this->package = $package;
282 1
    }
283
284
    /**
285
     * Returns the package name for this element.
286
     *
287
     * @return PackageDescriptor|null
288
     */
289 2
    public function getPackage()
290
    {
291 2
        $inheritedElement = $this->getInheritedElement();
292 2
        if ($this->package instanceof PackageDescriptor
293 2
            && ! ($this->package->getName() === '\\' && $inheritedElement)) {
294 2
            return $this->package;
295
        }
296
297 2
        if ($inheritedElement instanceof self) {
298 1
            return $inheritedElement->getPackage();
299
        }
300
301 1
        return null;
302
    }
303
304
    /**
305
     * @return Collection
306
     */
307 5
    public function getAuthor()
308
    {
309
        /** @var Collection $author */
310 5
        $author = $this->getTags()->get('author', new Collection());
311 5
        if ($author->count() !== 0) {
312 5
            return $author;
313
        }
314
315 4
        $inheritedElement = $this->getInheritedElement();
316 4
        if ($inheritedElement) {
317 4
            return $inheritedElement->getAuthor();
318
        }
319
320
        return new Collection();
321
    }
322
323
    /**
324
     * Returns the versions for this element.
325
     *
326
     * @return Collection
327
     */
328 5
    public function getVersion()
329
    {
330
        /** @var Collection $version */
331 5
        $version = $this->getTags()->get('version', new Collection());
332 5
        if ($version->count() !== 0) {
333 5
            return $version;
334
        }
335
336 4
        $inheritedElement = $this->getInheritedElement();
337 4
        if ($inheritedElement) {
338 4
            return $inheritedElement->getVersion();
339
        }
340
341
        return new Collection();
342
    }
343
344
    /**
345
     * Returns the copyrights for this element.
346
     *
347
     * @return Collection
348
     */
349 5
    public function getCopyright()
350
    {
351
        /** @var Collection $copyright */
352 5
        $copyright = $this->getTags()->get('copyright', new Collection());
353 5
        if ($copyright->count() !== 0) {
354 5
            return $copyright;
355
        }
356
357 4
        $inheritedElement = $this->getInheritedElement();
358 4
        if ($inheritedElement) {
359 4
            return $inheritedElement->getCopyright();
360
        }
361
362
        return new Collection();
363
    }
364
365
    /**
366
     * Checks whether this element is deprecated.
367
     *
368
     * @return boolean
369
     */
370 1
    public function isDeprecated()
371
    {
372 1
        return isset($this->tags['deprecated']);
373
    }
374
375
    /**
376
     * Sets a list of all errors associated with this element.
377
     */
378 2
    public function setErrors(Collection $errors): void
379
    {
380 2
        $this->errors = $errors;
381 2
    }
382
383
    /**
384
     * Returns all errors that occur in this element.
385
     *
386
     * @return Collection
387
     */
388 1
    public function getErrors()
389
    {
390 1
        return $this->errors;
391
    }
392
393
    /**
394
     * Dynamically constructs a set of getters to retrieve tag (collections) with.
395
     *
396
     * Important: __call() is not a fast method of access; it is preferred to directly use the getTags() collection.
397
     * This interface is provided to allow for uniform and easy access to certain tags.
398
     *
399
     * @param string  $name
400
     * @param mixed[] $arguments
401
     *
402
     * @return Collection|null
403
     */
404 1
    public function __call($name, $arguments)
405
    {
406 1
        if (substr($name, 0, 3) !== 'get') {
407 1
            return null;
408
        }
409
410 1
        $tagName = substr($name, 3);
411 1
        $tagName[0] = strtolower($tagName[0]); // lowercase the first letter
412
413 1
        return $this->getTags()->get($tagName, new Collection());
414
    }
415
416
    /**
417
     * Represents this object by its unique identifier, the Fully Qualified Structural Element Name.
418
     *
419
     * @return string
420
     */
421 1
    public function __toString(): string
422
    {
423 1
        return (string) $this->getFullyQualifiedStructuralElementName();
424
    }
425
426
    /**
427
     * @return DescriptorAbstract|null
428
     */
429
    public function getInheritedElement()
430
    {
431
        return $this->inheritedElement;
432
    }
433
}
434