Issues (127)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Reflection/Adapter/ReflectionClass.php (6 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
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\Reflection\Adapter;
6
7
use InvalidArgumentException;
8
use OutOfBoundsException;
9
use ReflectionClass as CoreReflectionClass;
10
use ReflectionException as CoreReflectionException;
11
use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
12
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
13
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
14
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
15
use function array_combine;
16
use function array_map;
17
use function func_num_args;
18
use function is_object;
19
use function is_string;
20
use function sprintf;
21
use function strtolower;
22
23
class ReflectionClass extends CoreReflectionClass
24
{
25 66
    /** @var BetterReflectionClass */
26
    private $betterReflectionClass;
27 66
28 66
    public function __construct(BetterReflectionClass $betterReflectionClass)
29
    {
30
        $this->betterReflectionClass = $betterReflectionClass;
31
    }
32
33 1
    /**
34
     * {@inheritDoc}
35 1
     *
36
     * @throws CoreReflectionException
37
     */
38
    public static function export($argument, $return = false)
39
    {
40
        if (is_string($argument) || is_object($argument)) {
41 1
            if (is_string($argument)) {
42
                $output = BetterReflectionClass::createFromName($argument)->__toString();
43 1
            } else {
44
                $output = BetterReflectionObject::createFromInstance($argument)->__toString();
45
            }
46
47
            if ($return) {
48
                return $output;
49 1
            }
50
51 1
            echo $output;
52
53
            return null;
54
        }
55
56
        throw new InvalidArgumentException('Class name must be provided');
57 1
    }
58
59 1
    /**
60
     * {@inheritDoc}
61
     */
62
    public function __toString()
63
    {
64
        return $this->betterReflectionClass->__toString();
65 1
    }
66
67 1
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getName()
71
    {
72
        return $this->betterReflectionClass->getName();
73 1
    }
74
75 1
    /**
76
     * {@inheritDoc}
77
     */
78
    public function isAnonymous()
79
    {
80
        return $this->betterReflectionClass->isAnonymous();
81 1
    }
82
83 1
    /**
84
     * {@inheritDoc}
85
     */
86
    public function isInternal()
87
    {
88
        return $this->betterReflectionClass->isInternal();
89 1
    }
90
91 1
    /**
92
     * {@inheritDoc}
93
     */
94
    public function isUserDefined()
95
    {
96
        return $this->betterReflectionClass->isUserDefined();
97 2
    }
98
99 2
    /**
100
     * {@inheritDoc}
101
     */
102
    public function isInstantiable()
103
    {
104
        return $this->betterReflectionClass->isInstantiable();
105 1
    }
106
107 1
    /**
108
     * {@inheritDoc}
109
     */
110
    public function isCloneable()
111
    {
112
        return $this->betterReflectionClass->isCloneable();
113 1
    }
114
115 1
    /**
116
     * {@inheritDoc}
117
     */
118
    public function getFileName()
119
    {
120
        return $this->betterReflectionClass->getFileName() ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaroslav Hanslík
The expression $this->betterReflectionC...getFileName() ?? false; of type string|false adds false to the return on line 120 which is incompatible with the return type of the parent method ReflectionClass::getFileName of type string. It seems like you forgot to handle an error condition.
Loading history...
121 2
    }
122
123 2
    /**
124
     * {@inheritDoc}
125
     */
126
    public function getStartLine()
127
    {
128
        return $this->betterReflectionClass->getStartLine();
129 2
    }
130
131
    /**
132 2
     * {@inheritDoc}
133 1
     */
134 1
    public function getEndLine()
135
    {
136
        return $this->betterReflectionClass->getEndLine();
137
    }
138
139
    /**
140
     * {@inheritDoc}
141 2
     */
142
    public function getDocComment()
143 2
    {
144
        return $this->betterReflectionClass->getDocComment() ?: false;
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaroslav Hanslík
The expression $this->betterReflectionC...tDocComment() ?: false; of type string|false adds false to the return on line 144 which is incompatible with the return type of the parent method ReflectionClass::getDocComment of type string. It seems like you forgot to handle an error condition.
Loading history...
145
    }
146
147
    /**
148
     * {@inheritDoc}
149 2
     */
150
    public function getConstructor()
151 2
    {
152
        try {
153
            return new ReflectionMethod($this->betterReflectionClass->getConstructor());
154 4
        } catch (OutOfBoundsException $e) {
155
            return null;
156
        }
157 2
    }
158 4
159
    /**
160
     * {@inheritDoc}
161 2
     */
162 4
    public function hasMethod($name)
163
    {
164 4
        return $this->betterReflectionClass->hasMethod($this->getMethodRealName($name));
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170 1
    public function getMethod($name)
171
    {
172 1
        return new ReflectionMethod($this->betterReflectionClass->getMethod($this->getMethodRealName($name)));
173
    }
174 1
175 1
    private function getMethodRealName(string $name) : string
176 1
    {
177
        $realMethodNames = array_map(static function (BetterReflectionMethod $method) : string {
178
            return $method->getName();
179 1
        }, $this->betterReflectionClass->getMethods());
180
181
        $methodNames = array_combine(array_map(static function (string $methodName) : string {
182
            return strtolower($methodName);
183
        }, $realMethodNames), $realMethodNames);
184
185 1
        return $methodNames[strtolower($name)] ?? $name;
186
    }
187 1
188
    /**
189
     * {@inheritDoc}
190
     */
191
    public function getMethods($filter = null)
192
    {
193 2
        $methods = $this->betterReflectionClass->getMethods();
194
195 2
        $wrappedMethods = [];
196
        foreach ($methods as $key => $method) {
197 2
            $wrappedMethods[$key] = new ReflectionMethod($method);
198 1
        }
199
200
        return $wrappedMethods;
201 1
    }
202
203
    /**
204
     * {@inheritDoc}
205
     */
206
    public function hasProperty($name)
207 1
    {
208
        return $this->betterReflectionClass->hasProperty($name);
209 1
    }
210
211 1
    /**
212 1
     * {@inheritDoc}
213 1
     */
214
    public function getProperty($name)
215
    {
216 1
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
217
218
        if ($betterReflectionProperty === null) {
219
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
220
        }
221
222 1
        return new ReflectionProperty($betterReflectionProperty);
223
    }
224 1
225
    /**
226
     * {@inheritDoc}
227
     */
228
    public function getProperties($filter = null)
229
    {
230 1
        $properties = $this->betterReflectionClass->getProperties();
231
232 1
        $wrappedProperties = [];
233
        foreach ($properties as $key => $property) {
234
            $wrappedProperties[$key] = new ReflectionProperty($property);
235
        }
236
237
        return $wrappedProperties;
238 1
    }
239
240 1
    /**
241
     * {@inheritDoc}
242
     */
243
    public function hasConstant($name)
244
    {
245
        return $this->betterReflectionClass->hasConstant($name);
246 1
    }
247
248 1
    /**
249 1
     * {@inheritDoc}
250
     */
251
    public function getConstants()
252
    {
253
        return $this->betterReflectionClass->getConstants();
254
    }
255
256 1
    /**
257
     * {@inheritDoc}
258
     */
259 1
    public function getConstant($name)
260 1
    {
261
        return $this->betterReflectionClass->getConstant($name);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266 1
     */
267
    public function getReflectionConstant($name)
268 1
    {
269
        return new ReflectionClassConstant(
270 1
            $this->betterReflectionClass->getReflectionConstant($name)
0 ignored issues
show
It seems like $this->betterReflectionC...flectionConstant($name) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
271 1
        );
272 1
    }
273
274
    /**
275 1
     * {@inheritdoc}
276
     */
277
    public function getReflectionConstants()
278
    {
279
        return array_map(static function (BetterReflectionClassConstant $betterConstant) : ReflectionClassConstant {
280
            return new ReflectionClassConstant($betterConstant);
281 1
        }, $this->betterReflectionClass->getReflectionConstants());
282
    }
283 1
284
    /**
285
     * {@inheritDoc}
286
     */
287
    public function getInterfaces()
288
    {
289 1
        $interfaces = $this->betterReflectionClass->getInterfaces();
290
291 1
        $wrappedInterfaces = [];
292
        foreach ($interfaces as $key => $interface) {
293
            $wrappedInterfaces[$key] = new self($interface);
294
        }
295
296
        return $wrappedInterfaces;
297 1
    }
298
299 1
    /**
300
     * {@inheritDoc}
301 1
     */
302 1
    public function getInterfaceNames()
303 1
    {
304
        return $this->betterReflectionClass->getInterfaceNames();
305
    }
306 1
307
    /**
308
     * {@inheritDoc}
309
     */
310
    public function isInterface()
311
    {
312 1
        return $this->betterReflectionClass->isInterface();
313
    }
314 1
315
    /**
316
     * {@inheritDoc}
317
     */
318
    public function getTraits()
319
    {
320 1
        $traits = $this->betterReflectionClass->getTraits();
321
322 1
        $wrappedTraits = [];
323
        foreach ($traits as $key => $trait) {
324
            $wrappedTraits[$key] = new self($trait);
325
        }
326
327
        return $wrappedTraits;
328 1
    }
329
330 1
    /**
331
     * {@inheritDoc}
332
     */
333
    public function getTraitNames()
334
    {
335
        return $this->betterReflectionClass->getTraitNames();
336 1
    }
337
338 1
    /**
339
     * {@inheritDoc}
340
     */
341
    public function getTraitAliases()
342
    {
343
        return $this->betterReflectionClass->getTraitAliases();
344 1
    }
345
346 1
    /**
347
     * {@inheritDoc}
348
     */
349
    public function isTrait()
350
    {
351
        return $this->betterReflectionClass->isTrait();
352 1
    }
353
354 1
    /**
355
     * {@inheritDoc}
356
     */
357
    public function isAbstract()
358
    {
359
        return $this->betterReflectionClass->isAbstract();
360 1
    }
361
362 1
    /**
363
     * {@inheritDoc}
364
     */
365
    public function isFinal()
366
    {
367
        return $this->betterReflectionClass->isFinal();
368 1
    }
369
370 1
    /**
371
     * {@inheritDoc}
372
     */
373
    public function getModifiers()
374
    {
375
        return $this->betterReflectionClass->getModifiers();
376 1
    }
377
378 1
    /**
379
     * {@inheritDoc}
380
     */
381
    public function isInstance($object)
382
    {
383
        return $this->betterReflectionClass->isInstance($object);
384 1
    }
385
386 1
    /**
387
     * {@inheritDoc}
388
     */
389
    public function newInstance($arg = null, ...$args)
0 ignored issues
show
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
390
    {
391
        throw new Exception\NotImplemented('Not implemented');
392 2
    }
393
394 2
    /**
395
     * {@inheritDoc}
396 2
     */
397 1
    public function newInstanceWithoutConstructor()
398
    {
399
        throw new Exception\NotImplemented('Not implemented');
400 1
    }
401
402
    /**
403
     * {@inheritDoc}
404
     */
405
    public function newInstanceArgs(?array $args = null)
406 3
    {
407
        throw new Exception\NotImplemented('Not implemented');
408 3
    }
409
410
    /**
411 1
     * {@inheritDoc}
412 3
     */
413
    public function getParentClass()
414 3
    {
415
        $parentClass = $this->betterReflectionClass->getParentClass();
416 3
417
        if ($parentClass === null) {
418
            return false;
0 ignored issues
show
Bug Best Practice introduced by Jaroslav Hanslík
The return type of return false; (false) is incompatible with the return type of the parent method ReflectionClass::getParentClass of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
419
        }
420
421
        return new self($parentClass);
422 1
    }
423
424 1
    /**
425
     * {@inheritDoc}
426
     */
427
    public function isSubclassOf($class)
428
    {
429
        $realParentClassNames = $this->betterReflectionClass->getParentClassNames();
430 4
431
        $parentClassNames = array_combine(array_map(static function (string $parentClassName) : string {
432 4
            return strtolower($parentClassName);
433
        }, $realParentClassNames), $realParentClassNames);
434 4
435 2
        $realParentClassName = $parentClassNames[strtolower($class)] ?? $class;
436 1
437
        return $this->betterReflectionClass->isSubclassOf($realParentClassName) || $this->implementsInterface($class);
438
    }
439 1
440
    /**
441
     * {@inheritDoc}
442 2
     */
443
    public function getStaticProperties()
444 2
    {
445 1
        return $this->betterReflectionClass->getStaticProperties();
446
    }
447
448 1
    /**
449
     * {@inheritDoc}
450
     */
451
    public function getStaticPropertyValue($name, $default = null)
452 1
    {
453
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
454
455
        if ($betterReflectionProperty === null) {
456
            if (func_num_args() === 2) {
457
                return $default;
458 3
            }
459
460 3
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
461
        }
462 3
463 1
        $property = new ReflectionProperty($betterReflectionProperty);
464
465
        if (! $property->isAccessible()) {
466 2
            throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
467
        }
468 2
469 1
        if (! $property->isStatic()) {
470
            throw new CoreReflectionException(sprintf('Property "%s" is not static', $name));
471
        }
472 1
473
        return $property->getValue();
474
    }
475
476 1
    /**
477 1
     * {@inheritDoc}
478
     */
479
    public function setStaticPropertyValue($name, $value)
480
    {
481
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
482 1
483
        if ($betterReflectionProperty === null) {
484 1
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
485
        }
486
487
        $property = new ReflectionProperty($betterReflectionProperty);
488
489
        if (! $property->isAccessible()) {
490 1
            throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
491
        }
492 1
493
        if (! $property->isStatic()) {
494
            throw new CoreReflectionException(sprintf('Property "%s" is not static', $name));
495
        }
496
497
        $property->setValue($value);
498 3
    }
499
500 3
    /**
501
     * {@inheritDoc}
502
     */
503 2
    public function getDefaultProperties()
504 3
    {
505
        return $this->betterReflectionClass->getDefaultProperties();
506 3
    }
507
508 3
    /**
509
     * {@inheritDoc}
510
     */
511
    public function isIterateable()
512
    {
513
        return $this->betterReflectionClass->isIterateable();
514 1
    }
515
516 1
    /**
517
     * {@inheritDoc}
518
     */
519
    public function implementsInterface($interface)
520
    {
521
        $realInterfaceNames = $this->betterReflectionClass->getInterfaceNames();
522 2
523
        $interfaceNames = array_combine(array_map(static function (string $interfaceName) : string {
524 2
            return strtolower($interfaceName);
525
        }, $realInterfaceNames), $realInterfaceNames);
526
527
        $realInterfaceName = $interfaceNames[strtolower($interface)] ?? $interface;
528
529
        return $this->betterReflectionClass->implementsInterface($realInterfaceName);
530 1
    }
531
532 1
    /**
533
     * {@inheritDoc}
534
     */
535
    public function getExtension()
536
    {
537
        throw new Exception\NotImplemented('Not implemented');
538 1
    }
539
540 1
    /**
541
     * {@inheritDoc}
542
     */
543
    public function getExtensionName()
544
    {
545
        return $this->betterReflectionClass->getExtensionName() ?? false;
0 ignored issues
show
Bug Best Practice introduced by Jaroslav Hanslík
The return type of return $this->betterRefl...tensionName() ?? false; (false) is incompatible with the return type of the parent method ReflectionClass::getExtensionName of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
546 1
    }
547
548 1
    /**
549
     * {@inheritDoc}
550
     */
551
    public function inNamespace()
552
    {
553
        return $this->betterReflectionClass->inNamespace();
554
    }
555
556
    /**
557
     * {@inheritDoc}
558
     */
559
    public function getNamespaceName()
560
    {
561
        return $this->betterReflectionClass->getNamespaceName();
562
    }
563
564
    /**
565
     * {@inheritDoc}
566
     */
567
    public function getShortName()
568
    {
569
        return $this->betterReflectionClass->getShortName();
570
    }
571
}
572