Issues (138)

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 (3 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
    /** @var BetterReflectionClass */
26
    private $betterReflectionClass;
27
28 66
    public function __construct(BetterReflectionClass $betterReflectionClass)
29
    {
30 66
        $this->betterReflectionClass = $betterReflectionClass;
31 66
    }
32
33
    /**
34
     * {@inheritDoc}
35
     *
36
     * @throws CoreReflectionException
37
     */
38 1
    public static function export($argument, $return = false)
39
    {
40 1
        if (is_string($argument) || is_object($argument)) {
41 1
            if (is_string($argument)) {
42 1
                $output = BetterReflectionClass::createFromName($argument)->__toString();
43
            } else {
44
                $output = BetterReflectionObject::createFromInstance($argument)->__toString();
45
            }
46
47 1
            if ($return) {
48 1
                return $output;
49
            }
50
51
            echo $output;
52
53
            return null;
54
        }
55
56
        throw new InvalidArgumentException('Class name must be provided');
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 1
    public function __toString()
63
    {
64 1
        return $this->betterReflectionClass->__toString();
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 1
    public function getName()
71
    {
72 1
        return $this->betterReflectionClass->getName();
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 1
    public function isAnonymous()
79
    {
80 1
        return $this->betterReflectionClass->isAnonymous();
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 1
    public function isInternal()
87
    {
88 1
        return $this->betterReflectionClass->isInternal();
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 1
    public function isUserDefined()
95
    {
96 1
        return $this->betterReflectionClass->isUserDefined();
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 1
    public function isInstantiable()
103
    {
104 1
        return $this->betterReflectionClass->isInstantiable();
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 1
    public function isCloneable()
111
    {
112 1
        return $this->betterReflectionClass->isCloneable();
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 2
    public function getFileName()
119
    {
120 2
        return $this->betterReflectionClass->getFileName() ?? false;
121
    }
122
123
    /**
124
     * {@inheritDoc}
125
     */
126 1
    public function getStartLine()
127
    {
128 1
        return $this->betterReflectionClass->getStartLine();
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 1
    public function getEndLine()
135
    {
136 1
        return $this->betterReflectionClass->getEndLine();
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 2
    public function getDocComment()
143
    {
144 2
        return $this->betterReflectionClass->getDocComment() ?: false;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 2
    public function getConstructor()
151
    {
152
        try {
153 2
            return new ReflectionMethod($this->betterReflectionClass->getConstructor());
154 1
        } catch (OutOfBoundsException $e) {
155 1
            return null;
156
        }
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162 2
    public function hasMethod($name)
163
    {
164 2
        return $this->betterReflectionClass->hasMethod($this->getMethodRealName($name));
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170 2
    public function getMethod($name)
171
    {
172 2
        return new ReflectionMethod($this->betterReflectionClass->getMethod($this->getMethodRealName($name)));
173
    }
174
175 4
    private function getMethodRealName(string $name) : string
176
    {
177
        $realMethodNames = array_map(static function (BetterReflectionMethod $method) : string {
178 2
            return $method->getName();
179 4
        }, $this->betterReflectionClass->getMethods());
180
181
        $methodNames = array_combine(array_map(static function (string $methodName) : string {
182 2
            return strtolower($methodName);
183 4
        }, $realMethodNames), $realMethodNames);
184
185 4
        return $methodNames[strtolower($name)] ?? $name;
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191 1
    public function getMethods($filter = null)
192
    {
193 1
        $methods = $this->betterReflectionClass->getMethods();
194
195 1
        $wrappedMethods = [];
196 1
        foreach ($methods as $key => $method) {
197 1
            $wrappedMethods[$key] = new ReflectionMethod($method);
198
        }
199
200 1
        return $wrappedMethods;
201
    }
202
203
    /**
204
     * {@inheritDoc}
205
     */
206 1
    public function hasProperty($name)
207
    {
208 1
        return $this->betterReflectionClass->hasProperty($name);
209
    }
210
211
    /**
212
     * {@inheritDoc}
213
     */
214 2
    public function getProperty($name)
215
    {
216 2
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
217
218 2
        if ($betterReflectionProperty === null) {
219 1
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
220
        }
221
222 1
        return new ReflectionProperty($betterReflectionProperty);
223
    }
224
225
    /**
226
     * {@inheritDoc}
227
     */
228 1
    public function getProperties($filter = null)
229
    {
230 1
        $properties = $this->betterReflectionClass->getProperties();
231
232 1
        $wrappedProperties = [];
233 1
        foreach ($properties as $key => $property) {
234 1
            $wrappedProperties[$key] = new ReflectionProperty($property);
235
        }
236
237 1
        return $wrappedProperties;
238
    }
239
240
    /**
241
     * {@inheritDoc}
242
     */
243 1
    public function hasConstant($name)
244
    {
245 1
        return $this->betterReflectionClass->hasConstant($name);
246
    }
247
248
    /**
249
     * {@inheritDoc}
250
     */
251 1
    public function getConstants()
252
    {
253 1
        return $this->betterReflectionClass->getConstants();
254
    }
255
256
    /**
257
     * {@inheritDoc}
258
     */
259 1
    public function getConstant($name)
260
    {
261 1
        return $this->betterReflectionClass->getConstant($name);
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267 1
    public function getReflectionConstant($name)
268
    {
269 1
        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
        );
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     *
277 1
     * @psalm-suppress InvalidReturnType see #530
278
     */
279
    public function getReflectionConstants()
280 1
    {
281 1
        return array_map(static function (BetterReflectionClassConstant $betterConstant) : ReflectionClassConstant {
282
            return new ReflectionClassConstant($betterConstant);
283
        }, $this->betterReflectionClass->getReflectionConstants());
284
    }
285
286
    /**
287 1
     * {@inheritDoc}
288
     */
289 1
    public function getInterfaces()
290
    {
291 1
        $interfaces = $this->betterReflectionClass->getInterfaces();
292 1
293 1
        $wrappedInterfaces = [];
294
        foreach ($interfaces as $key => $interface) {
295
            $wrappedInterfaces[$key] = new self($interface);
296 1
        }
297
298
        return $wrappedInterfaces;
299
    }
300
301
    /**
302 1
     * {@inheritDoc}
303
     */
304 1
    public function getInterfaceNames()
305
    {
306
        return $this->betterReflectionClass->getInterfaceNames();
307
    }
308
309
    /**
310 1
     * {@inheritDoc}
311
     */
312 1
    public function isInterface()
313
    {
314
        return $this->betterReflectionClass->isInterface();
315
    }
316
317
    /**
318 1
     * {@inheritDoc}
319
     *
320 1
     * @psalm-suppress InvalidReturnType see #530
321
     */
322 1
    public function getTraits()
323 1
    {
324 1
        $traits = $this->betterReflectionClass->getTraits();
325
326
        $wrappedTraits = [];
327 1
        foreach ($traits as $key => $trait) {
328
            $wrappedTraits[$key] = new self($trait);
329
        }
330
331
        return $wrappedTraits;
332
    }
333 1
334
    /**
335 1
     * {@inheritDoc}
336
     */
337
    public function getTraitNames()
338
    {
339
        return $this->betterReflectionClass->getTraitNames();
340
    }
341 1
342
    /**
343 1
     * {@inheritDoc}
344
     */
345
    public function getTraitAliases()
346
    {
347
        return $this->betterReflectionClass->getTraitAliases();
348
    }
349 1
350
    /**
351 1
     * {@inheritDoc}
352
     */
353
    public function isTrait()
354
    {
355
        return $this->betterReflectionClass->isTrait();
356
    }
357 1
358
    /**
359 1
     * {@inheritDoc}
360
     */
361
    public function isAbstract()
362
    {
363
        return $this->betterReflectionClass->isAbstract();
364
    }
365 1
366
    /**
367 1
     * {@inheritDoc}
368
     */
369
    public function isFinal()
370
    {
371
        return $this->betterReflectionClass->isFinal();
372
    }
373 1
374
    /**
375 1
     * {@inheritDoc}
376
     */
377
    public function getModifiers()
378
    {
379
        return $this->betterReflectionClass->getModifiers();
380
    }
381 1
382
    /**
383 1
     * {@inheritDoc}
384
     */
385
    public function isInstance($object)
386
    {
387
        return $this->betterReflectionClass->isInstance($object);
388
    }
389 1
390
    /**
391 1
     * {@inheritDoc}
392
     */
393
    public function newInstance($arg = null, ...$args)
394
    {
395
        throw new Exception\NotImplemented('Not implemented');
396
    }
397 1
398
    /**
399 1
     * {@inheritDoc}
400
     */
401
    public function newInstanceWithoutConstructor()
402
    {
403
        throw new Exception\NotImplemented('Not implemented');
404
    }
405 1
406
    /**
407 1
     * {@inheritDoc}
408
     */
409
    public function newInstanceArgs(?array $args = null)
410
    {
411
        throw new Exception\NotImplemented('Not implemented');
412
    }
413 2
414
    /**
415 2
     * {@inheritDoc}
416
     */
417 2
    public function getParentClass()
418 1
    {
419
        $parentClass = $this->betterReflectionClass->getParentClass();
420
421 1
        if ($parentClass === null) {
422
            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...
423
        }
424
425
        return new self($parentClass);
426
    }
427 3
428
    /**
429 3
     * {@inheritDoc}
430
     */
431
    public function isSubclassOf($class)
432 1
    {
433 3
        $realParentClassNames = $this->betterReflectionClass->getParentClassNames();
434
435 3
        $parentClassNames = array_combine(array_map(static function (string $parentClassName) : string {
436
            return strtolower($parentClassName);
437 3
        }, $realParentClassNames), $realParentClassNames);
438
439
        $realParentClassName = $parentClassNames[strtolower($class)] ?? $class;
440
441
        return $this->betterReflectionClass->isSubclassOf($realParentClassName) || $this->implementsInterface($class);
442
    }
443 1
444
    /**
445 1
     * {@inheritDoc}
446
     */
447
    public function getStaticProperties()
448
    {
449
        return $this->betterReflectionClass->getStaticProperties();
450
    }
451 4
452
    /**
453 4
     * {@inheritDoc}
454
     */
455 4
    public function getStaticPropertyValue($name, $default = null)
456 2
    {
457 1
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
458
459
        if ($betterReflectionProperty === null) {
460 1
            if (func_num_args() === 2) {
461
                return $default;
462
            }
463 2
464
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
465 2
        }
466 1
467
        $property = new ReflectionProperty($betterReflectionProperty);
468
469 1
        if (! $property->isAccessible()) {
470
            throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
471
        }
472
473 1
        if (! $property->isStatic()) {
474
            throw new CoreReflectionException(sprintf('Property "%s" is not static', $name));
475
        }
476
477
        return $property->getValue();
478
    }
479 3
480
    /**
481 3
     * {@inheritDoc}
482
     */
483 3
    public function setStaticPropertyValue($name, $value)
484 1
    {
485
        $betterReflectionProperty = $this->betterReflectionClass->getProperty($name);
486
487 2
        if ($betterReflectionProperty === null) {
488
            throw new CoreReflectionException(sprintf('Property "%s" does not exist', $name));
489 2
        }
490 1
491
        $property = new ReflectionProperty($betterReflectionProperty);
492
493 1
        if (! $property->isAccessible()) {
494
            throw new CoreReflectionException(sprintf('Property "%s" is not accessible', $name));
495
        }
496
497 1
        if (! $property->isStatic()) {
498 1
            throw new CoreReflectionException(sprintf('Property "%s" is not static', $name));
499
        }
500
501
        $property->setValue($value);
502
    }
503 1
504
    /**
505 1
     * {@inheritDoc}
506
     */
507
    public function getDefaultProperties()
508
    {
509
        return $this->betterReflectionClass->getDefaultProperties();
510
    }
511 1
512
    /**
513 1
     * {@inheritDoc}
514
     */
515
    public function isIterateable()
516
    {
517
        return $this->betterReflectionClass->isIterateable();
518
    }
519 3
520
    /**
521 3
     * {@inheritDoc}
522
     */
523
    public function implementsInterface($interface)
524 2
    {
525 3
        $realInterfaceNames = $this->betterReflectionClass->getInterfaceNames();
526
527 3
        $interfaceNames = array_combine(array_map(static function (string $interfaceName) : string {
528
            return strtolower($interfaceName);
529 3
        }, $realInterfaceNames), $realInterfaceNames);
530
531
        $realInterfaceName = $interfaceNames[strtolower($interface)] ?? $interface;
532
533
        return $this->betterReflectionClass->implementsInterface($realInterfaceName);
534
    }
535 1
536
    /**
537 1
     * {@inheritDoc}
538
     */
539
    public function getExtension()
540
    {
541
        throw new Exception\NotImplemented('Not implemented');
542
    }
543 2
544
    /**
545 2
     * {@inheritDoc}
546
     */
547
    public function getExtensionName()
548
    {
549
        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...
550
    }
551 1
552
    /**
553 1
     * {@inheritDoc}
554
     */
555
    public function inNamespace()
556
    {
557
        return $this->betterReflectionClass->inNamespace();
558
    }
559 1
560
    /**
561 1
     * {@inheritDoc}
562
     */
563
    public function getNamespaceName()
564
    {
565
        return $this->betterReflectionClass->getNamespaceName();
566
    }
567 1
568
    /**
569 1
     * {@inheritDoc}
570
     */
571
    public function getShortName()
572
    {
573
        return $this->betterReflectionClass->getShortName();
574
    }
575
}
576