1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper; |
16
|
|
|
|
17
|
|
|
use ReflectionException; |
18
|
|
|
use ReflectionFunction; |
19
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
20
|
|
|
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound; |
21
|
|
|
use Roave\BetterReflection\Reflector\FunctionReflector; |
22
|
|
|
use function array_key_exists; |
23
|
|
|
use function array_values; |
24
|
|
|
use function get_defined_constants; |
25
|
|
|
use function strtoupper; |
26
|
|
|
|
27
|
|
|
final class Reflector |
28
|
|
|
{ |
29
|
|
|
private $classReflector; |
30
|
|
|
private $functionReflector; |
31
|
|
|
private $constants; |
32
|
|
|
|
33
|
347 |
|
public function __construct(ClassReflector $classReflector, FunctionReflector $functionReflector) |
34
|
|
|
{ |
35
|
347 |
|
$this->classReflector = $classReflector; |
36
|
347 |
|
$this->functionReflector = $functionReflector; |
37
|
|
|
} |
38
|
|
|
|
39
|
200 |
|
public function isClassInternal(string $name): bool |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
200 |
|
return $this->classReflector->reflect($name)->isInternal(); |
43
|
3 |
|
} catch (IdentifierNotFound $exception) { |
44
|
3 |
|
return false; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
92 |
|
public function isFunctionInternal(string $name): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
92 |
|
return (new ReflectionFunction($name))->isInternal(); |
52
|
|
|
|
53
|
|
|
// TODO: leverage Better Reflection instead |
54
|
|
|
return $this->functionReflector->reflect($name)->isInternal(); |
|
|
|
|
55
|
30 |
|
} catch (ReflectionException $exception) { |
56
|
30 |
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
96 |
|
public function isConstantInternal(string $name): bool |
61
|
|
|
{ |
62
|
96 |
|
if (null === $this->constants) { |
63
|
96 |
|
$constants = get_defined_constants(true); |
64
|
|
|
|
65
|
96 |
|
unset($constants['user']); |
66
|
|
|
|
67
|
96 |
|
$this->constants = array_merge(...array_values($constants)); |
68
|
|
|
|
69
|
96 |
|
unset($constants); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// TODO: find a better solution |
73
|
96 |
|
return array_key_exists(strtoupper($name), $this->constants); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.