Completed
Push — master ( b2e2db...3e3691 )
by Théo
04:39 queued 02:20
created

Reflector::isConstantInternal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
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 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();
0 ignored issues
show
Unused Code introduced by
return $this->functionRe...t($name)->isInternal(); does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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