Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

src/phpDocumentor/Descriptor/FileDescriptor.php (2 issues)

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-2018 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
use phpDocumentor\Descriptor\Interfaces\ClassInterface;
15
use phpDocumentor\Descriptor\Interfaces\FunctionInterface;
16
use phpDocumentor\Descriptor\Interfaces\InterfaceInterface;
17
use phpDocumentor\Descriptor\Interfaces\TraitInterface;
18
19
/**
20
 * Represents a file in the project.
21
 */
22
class FileDescriptor extends DescriptorAbstract implements Interfaces\FileInterface
23
{
24
    /** @var string $hash */
25
    protected $hash;
26
27
    /** @var string $path */
28
    protected $path = '';
29
30
    /** @var string|null $source */
31
    protected $source = null;
32
33
    /** @var Collection $namespaceAliases */
34
    protected $namespaceAliases;
35
36
    /** @var Collection $includes */
37
    protected $includes;
38
39
    /** @var Collection $constants */
40
    protected $constants;
41
42
    /** @var Collection $functions */
43
    protected $functions;
44
45
    /** @var Collection $classes */
46
    protected $classes;
47
48
    /** @var Collection $interfaces */
49
    protected $interfaces;
50
51
    /** @var Collection $traits */
52
    protected $traits;
53
54
    /** @var Collection $markers */
55
    protected $markers;
56
57
    /**
58
     * Initializes a new file descriptor with the given hash of its contents.
59
     *
60
     * @param string $hash An MD5 hash of the contents if this file.
61
     */
62 4
    public function __construct($hash)
63
    {
64 4
        parent::__construct();
65
66 4
        $this->setHash($hash);
67 4
        $this->setNamespaceAliases(new Collection());
68 4
        $this->setIncludes(new Collection());
69
70 4
        $this->setConstants(new Collection());
71 4
        $this->setFunctions(new Collection());
72 4
        $this->setClasses(new Collection());
73 4
        $this->setInterfaces(new Collection());
74 4
        $this->setTraits(new Collection());
75
76 4
        $this->setMarkers(new Collection());
77 4
    }
78
79
    /**
80
     * Returns the hash of the contents for this file.
81
     *
82
     * @return string
83
     */
84 1
    public function getHash()
85
    {
86 1
        return $this->hash;
87
    }
88
89
    /**
90
     * Sets the hash of the contents for this file.
91
     *
92
     * @param string $hash
93
     */
94
    protected function setHash($hash)
95
    {
96
        $this->hash = $hash;
97
    }
98
99
    /**
100
     * Retrieves the contents of this file.
101
     *
102
     * @return string|null
103
     */
104 1
    public function getSource()
105
    {
106 1
        return $this->source;
107
    }
108
109
    /**
110
     * Sets the source contents for this file.
111
     *
112
     * @param string|null $source
113
     */
114 1
    public function setSource($source)
115
    {
116 1
        $this->source = $source;
117 1
    }
118
119
    /**
120
     * Returns the namespace aliases that have been defined in this file.
121
     *
122
     * @return Collection
123
     */
124 1
    public function getNamespaceAliases()
125
    {
126 1
        return $this->namespaceAliases;
127
    }
128
129
    /**
130
     * Sets the collection of namespace aliases for this file.
131
     */
132 1
    public function setNamespaceAliases(Collection $namespaceAliases)
133
    {
134 1
        $this->namespaceAliases = $namespaceAliases;
135 1
    }
136
137
    /**
138
     * Returns a list of all includes that have been declared in this file.
139
     *
140
     * @return Collection
141
     */
142 1
    public function getIncludes()
143
    {
144 1
        return $this->includes;
145
    }
146
147
    /**
148
     * Sets a list of all includes that have been declared in this file.
149
     */
150 1
    public function setIncludes(Collection $includes)
151
    {
152 1
        $this->includes = $includes;
153 1
    }
154
155
    /**
156
     * Returns a list of constant descriptors contained in this file.
157
     *
158
     * @return Collection
159
     */
160 1
    public function getConstants()
161
    {
162 1
        return $this->constants;
163
    }
164
165
    /**
166
     * Sets a list of constant descriptors contained in this file.
167
     */
168 1
    public function setConstants(Collection $constants)
169
    {
170 1
        $this->constants = $constants;
171 1
    }
172
173
    /**
174
     * Returns a list of function descriptors contained in this file.
175
     *
176
     * @return Collection|FunctionInterface[]
177
     */
178 1
    public function getFunctions()
179
    {
180 1
        return $this->functions;
181
    }
182
183
    /**
184
     * Sets a list of function descriptors contained in this file.
185
     */
186 1
    public function setFunctions(Collection $functions)
187
    {
188 1
        $this->functions = $functions;
189 1
    }
190
191
    /**
192
     * Returns a list of class descriptors contained in this file.
193
     *
194
     * @return Collection|ClassInterface[]
195
     */
196 1
    public function getClasses()
197
    {
198 1
        return $this->classes;
199
    }
200
201
    /**
202
     * Sets a list of class descriptors contained in this file.
203
     */
204 1
    public function setClasses(Collection $classes)
205
    {
206 1
        $this->classes = $classes;
207 1
    }
208
209
    /**
210
     * Returns a list of interface descriptors contained in this file.
211
     *
212
     * @return Collection|InterfaceInterface[]
213
     */
214 1
    public function getInterfaces()
215
    {
216 1
        return $this->interfaces;
217
    }
218
219
    /**
220
     * Sets a list of interface descriptors contained in this file.
221
     */
222 1
    public function setInterfaces(Collection $interfaces)
223
    {
224 1
        $this->interfaces = $interfaces;
225 1
    }
226
227
    /**
228
     * Returns a list of trait descriptors contained in this file.
229
     *
230
     * @return Collection|TraitInterface[]
231
     */
232 1
    public function getTraits()
233
    {
234 1
        return $this->traits;
235
    }
236
237
    /**
238
     * Sets a list of trait descriptors contained in this file.
239
     */
240 1
    public function setTraits(Collection $traits)
241
    {
242 1
        $this->traits = $traits;
243 1
    }
244
245
    /**
246
     * Returns a series of markers contained in this file.
247
     *
248
     * A marker is a special inline comment that starts with a keyword and is followed by a single line description.
249
     *
250
     * Example:
251
     * ```
252
     * // TODO: This is an item that needs to be done.
253
     * ```
254
     *
255
     * @return Collection
256
     */
257 1
    public function getMarkers()
258
    {
259 1
        return $this->markers;
260
    }
261
262
    /**
263
     * Sets a series of markers contained in this file.
264
     *
265
     * @see getMarkers() for more information on markers.
266
     */
267 1
    public function setMarkers(Collection $markers)
268
    {
269 1
        $this->markers = $markers;
270 1
    }
271
272
    /**
273
     * Returns a list of all errors in this file and all its child elements.
274
     *
275
     * @return Collection
276
     */
277 2
    public function getAllErrors()
278
    {
279 2
        $errors = $this->getErrors();
280
281 2
        $types = $this->getClasses()->merge($this->getInterfaces())->merge($this->getTraits());
0 ignored issues
show
$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...
282
283 2
        $elements = $this->getFunctions()->merge($this->getConstants())->merge($types);
0 ignored issues
show
$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...
284
285 2
        foreach ($elements as $element) {
286 1
            if (!$element) {
287
                continue;
288
            }
289
290 1
            $errors = $errors->merge($element->getErrors());
291
        }
292
293 2
        foreach ($types as $element) {
294 1
            if (!$element) {
295
                continue;
296
            }
297
298 1
            foreach ($element->getMethods() as $item) {
299 1
                if (!$item) {
300
                    continue;
301
                }
302
303 1
                $errors = $errors->merge($item->getErrors());
304
            }
305
306 1
            if (method_exists($element, 'getConstants')) {
307 1
                foreach ($element->getConstants() as $item) {
308 1
                    if (!$item) {
309
                        continue;
310
                    }
311
312 1
                    $errors = $errors->merge($item->getErrors());
313
                }
314
            }
315
316 1
            if (method_exists($element, 'getProperties')) {
317 1
                foreach ($element->getProperties() as $item) {
318 1
                    if (!$item) {
319
                        continue;
320
                    }
321
322 1
                    $errors = $errors->merge($item->getErrors());
323
                }
324
            }
325
        }
326
327 2
        return $errors;
328
    }
329
330
    /**
331
     * Sets the file path for this file relative to the project's root.
332
     *
333
     * @param string $path
334
     */
335 1
    public function setPath($path)
336
    {
337 1
        $this->path = $path;
338 1
    }
339
340
    /**
341
     * Returns the file path relative to the project's root.
342
     *
343
     * @return string
344
     */
345 1
    public function getPath()
346
    {
347 1
        return $this->path;
348
    }
349
}
350