FileDescriptor::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
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\Interfaces\ClassInterface;
19
use phpDocumentor\Descriptor\Interfaces\FunctionInterface;
20
use phpDocumentor\Descriptor\Interfaces\InterfaceInterface;
21
use phpDocumentor\Descriptor\Interfaces\TraitInterface;
22
23
/**
24
 * Represents a file in the project.
25
 */
26
class FileDescriptor extends DescriptorAbstract implements Interfaces\FileInterface
27
{
28
    /** @var string $hash */
29
    protected $hash;
30
31
    /** @var string $path */
32
    protected $path = '';
33
34
    /** @var string|null $source */
35
    protected $source = null;
36
37
    /** @var Collection $namespaceAliases */
38
    protected $namespaceAliases;
39
40
    /** @var Collection $includes */
41
    protected $includes;
42
43
    /** @var Collection $constants */
44
    protected $constants;
45
46
    /** @var Collection $functions */
47
    protected $functions;
48
49
    /** @var Collection $classes */
50
    protected $classes;
51
52
    /** @var Collection $interfaces */
53
    protected $interfaces;
54
55
    /** @var Collection $traits */
56
    protected $traits;
57
58
    /** @var Collection $markers */
59
    protected $markers;
60
61
    /**
62
     * Initializes a new file descriptor with the given hash of its contents.
63
     *
64
     * @param string $hash An MD5 hash of the contents if this file.
65
     */
66 5
    public function __construct($hash)
67
    {
68 5
        parent::__construct();
69
70 5
        $this->setHash($hash);
71 5
        $this->setNamespaceAliases(new Collection());
72 5
        $this->setIncludes(new Collection());
73
74 5
        $this->setConstants(new Collection());
75 5
        $this->setFunctions(new Collection());
76 5
        $this->setClasses(new Collection());
77 5
        $this->setInterfaces(new Collection());
78 5
        $this->setTraits(new Collection());
79
80 5
        $this->setMarkers(new Collection());
81 5
    }
82
83
    /**
84
     * Returns the hash of the contents for this file.
85
     *
86
     * @return string
87
     */
88 1
    public function getHash()
89
    {
90 1
        return $this->hash;
91
    }
92
93
    /**
94
     * Sets the hash of the contents for this file.
95
     *
96
     * @param string $hash
97
     */
98 1
    protected function setHash($hash)
99
    {
100 1
        $this->hash = $hash;
101 1
    }
102
103
    /**
104
     * Retrieves the contents of this file.
105
     *
106
     * @return string|null
107
     */
108 2
    public function getSource()
109
    {
110 2
        return $this->source;
111
    }
112
113
    /**
114
     * Sets the source contents for this file.
115
     *
116
     * @param string|null $source
117
     */
118 2
    public function setSource($source)
119
    {
120 2
        $this->source = $source;
121 2
    }
122
123
    /**
124
     * Returns the namespace aliases that have been defined in this file.
125
     *
126
     * @return Collection
127
     */
128 1
    public function getNamespaceAliases()
129
    {
130 1
        return $this->namespaceAliases;
131
    }
132
133
    /**
134
     * Sets the collection of namespace aliases for this file.
135
     */
136 2
    public function setNamespaceAliases(Collection $namespaceAliases)
137
    {
138 2
        $this->namespaceAliases = $namespaceAliases;
139 2
    }
140
141
    /**
142
     * Returns a list of all includes that have been declared in this file.
143
     *
144
     * @return Collection
145
     */
146 1
    public function getIncludes()
147
    {
148 1
        return $this->includes;
149
    }
150
151
    /**
152
     * Sets a list of all includes that have been declared in this file.
153
     */
154 2
    public function setIncludes(Collection $includes)
155
    {
156 2
        $this->includes = $includes;
157 2
    }
158
159
    /**
160
     * Returns a list of constant descriptors contained in this file.
161
     *
162
     * @return Collection
163
     */
164 1
    public function getConstants()
165
    {
166 1
        return $this->constants;
167
    }
168
169
    /**
170
     * Sets a list of constant descriptors contained in this file.
171
     */
172 2
    public function setConstants(Collection $constants)
173
    {
174 2
        $this->constants = $constants;
175 2
    }
176
177
    /**
178
     * Returns a list of function descriptors contained in this file.
179
     *
180
     * @return Collection|FunctionInterface[]
181
     */
182 1
    public function getFunctions()
183
    {
184 1
        return $this->functions;
185
    }
186
187
    /**
188
     * Sets a list of function descriptors contained in this file.
189
     */
190 2
    public function setFunctions(Collection $functions)
191
    {
192 2
        $this->functions = $functions;
193 2
    }
194
195
    /**
196
     * Returns a list of class descriptors contained in this file.
197
     *
198
     * @return Collection|ClassInterface[]
199
     */
200 1
    public function getClasses()
201
    {
202 1
        return $this->classes;
203
    }
204
205
    /**
206
     * Sets a list of class descriptors contained in this file.
207
     */
208 2
    public function setClasses(Collection $classes)
209
    {
210 2
        $this->classes = $classes;
211 2
    }
212
213
    /**
214
     * Returns a list of interface descriptors contained in this file.
215
     *
216
     * @return Collection|InterfaceInterface[]
217
     */
218 1
    public function getInterfaces()
219
    {
220 1
        return $this->interfaces;
221
    }
222
223
    /**
224
     * Sets a list of interface descriptors contained in this file.
225
     */
226 2
    public function setInterfaces(Collection $interfaces)
227
    {
228 2
        $this->interfaces = $interfaces;
229 2
    }
230
231
    /**
232
     * Returns a list of trait descriptors contained in this file.
233
     *
234
     * @return Collection|TraitInterface[]
235
     */
236 1
    public function getTraits()
237
    {
238 1
        return $this->traits;
239
    }
240
241
    /**
242
     * Sets a list of trait descriptors contained in this file.
243
     */
244 2
    public function setTraits(Collection $traits)
245
    {
246 2
        $this->traits = $traits;
247 2
    }
248
249
    /**
250
     * Returns a series of markers contained in this file.
251
     *
252
     * A marker is a special inline comment that starts with a keyword and is followed by a single line description.
253
     *
254
     * Example:
255
     * ```
256
     * // TODO: This is an item that needs to be done.
257
     * ```
258
     *
259
     * @return Collection
260
     */
261 2
    public function getMarkers()
262
    {
263 2
        return $this->markers;
264
    }
265
266
    /**
267
     * Sets a series of markers contained in this file.
268
     *
269
     * @see getMarkers() for more information on markers.
270
     */
271 2
    public function setMarkers(Collection $markers)
272
    {
273 2
        $this->markers = $markers;
274 2
    }
275
276
    /**
277
     * Returns a list of all errors in this file and all its child elements.
278
     *
279
     * @return Collection
280
     */
281 2
    public function getAllErrors()
282
    {
283 2
        $errors = $this->getErrors();
284
285 2
        $types = $this->getClasses()->merge($this->getInterfaces())->merge($this->getTraits());
0 ignored issues
show
Documentation introduced by
$this->getInterfaces() is of type object<phpDocumentor\Des...es\InterfaceInterface>>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
286
287 2
        $elements = $this->getFunctions()->merge($this->getConstants())->merge($types);
0 ignored issues
show
Documentation introduced by
$this->getConstants() is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
288
289 2
        foreach ($elements as $element) {
290 1
            if (!$element) {
291
                continue;
292
            }
293
294 1
            $errors = $errors->merge($element->getErrors());
295
        }
296
297 2
        foreach ($types as $element) {
298 1
            if (!$element) {
299
                continue;
300
            }
301
302 1
            foreach ($element->getMethods() as $item) {
303 1
                if (!$item) {
304
                    continue;
305
                }
306
307 1
                $errors = $errors->merge($item->getErrors());
308
            }
309
310 1
            if (method_exists($element, 'getConstants')) {
311 1
                foreach ($element->getConstants() as $item) {
312 1
                    if (!$item) {
313
                        continue;
314
                    }
315
316 1
                    $errors = $errors->merge($item->getErrors());
317
                }
318
            }
319
320 1
            if (method_exists($element, 'getProperties')) {
321 1
                foreach ($element->getProperties() as $item) {
322 1
                    if (!$item) {
323
                        continue;
324
                    }
325
326 1
                    $errors = $errors->merge($item->getErrors());
327
                }
328
            }
329
        }
330
331 2
        return $errors;
332
    }
333
334
    /**
335
     * Sets the file path for this file relative to the project's root.
336
     *
337
     * @param string $path
338
     */
339 1
    public function setPath($path)
340
    {
341 1
        $this->path = $path;
342 1
    }
343
344
    /**
345
     * Returns the file path relative to the project's root.
346
     *
347
     * @return string
348
     */
349 1
    public function getPath()
350
    {
351 1
        return $this->path;
352
    }
353
}
354