Passed
Push — master ( 3eb6b6...6c036d )
by Théo
02:22
created

Reflector::getInternalConstants()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 Roave\BetterReflection\Identifier\Exception\InvalidIdentifierName;
18
use Roave\BetterReflection\Reflection\ReflectionClass;
19
use Roave\BetterReflection\Reflection\ReflectionConstant;
20
use Roave\BetterReflection\Reflection\ReflectionFunction;
21
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
22
use Roave\BetterReflection\Reflector\Reflector as BetterReflectionReflector;
23
use function in_array;
24
25
/**
26
 * Main class used to determine if a given symbol is internal or not. As of the time of writing, it leverages
27
 * Roave\BetterReflection to determine if given class is internal or not which allows to do reflection:.
28
 *
29
 * - Without loading the code scanned code at all
30
 * - Do reliable reflection against non-loaded APIs, i.e. a class from a non-loaded extension will properly appear
31
 *   as internal whereas the regular reflection would not.
32
 *
33
 * However Roave\BetterReflection is still not supporting constants and functions hence requires some hacks here
34
 * meanwhile.
35
 *
36
 * @private
37
 */
38
final class Reflector
39
{
40
    private $classReflector;
41
    private $functionReflector;
42
    private $constantReflector;
43
44
    public function __construct(
45
        BetterReflectionReflector $classReflector,
46
        BetterReflectionReflector $functionReflector,
47
        BetterReflectionReflector $constantReflector
48
    ) {
49
        $this->classReflector = $classReflector;
50
        $this->functionReflector = $functionReflector;
51
        $this->constantReflector = $constantReflector;
52
    }
53
54
    public function isClassInternal(string $name): bool
55
    {
56
        try {
57
            /** @var ReflectionClass $classReflection */
58
            $classReflection = $this->classReflector->reflect($name);
59
60
            return $classReflection->isInternal();
61
        } catch (IdentifierNotFound | InvalidIdentifierName $exception) {
62
            return false;
63
        }
64
    }
65
66
    public function isFunctionInternal(string $name): bool
67
    {
68
        try {
69
            /** @var ReflectionFunction $functionReflection */
70
            $functionReflection = $this->functionReflector->reflect($name);
71
72
            return $functionReflection->isInternal();
73
        } catch (IdentifierNotFound | InvalidIdentifierName $exception) {
74
            return false;
75
        }
76 571
    }
77
78 571
    public function isConstantInternal(string $name): bool
79
    {
80
        if (in_array($name, ['STDIN', 'STDOUT', 'STDERR'], true)) {
81 310
            return true;
82
        }
83
84 310
        try {
85 44
            /** @var ReflectionConstant $constantReflection */
86 44
            $constantReflection = $this->constantReflector->reflect($name);
87
88
            return $constantReflection->isInternal();
89
        } catch (IdentifierNotFound | InvalidIdentifierName $exception) {
90 98
            return false;
91
        }
92 98
    }
93
}
94