Passed
Push — master ( 206034...319324 )
by Théo
02:08
created

EnrichedReflector::belongsToExcludedNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\Symbol;
6
7
use Humbug\PhpScoper\Reflector;
8
use Humbug\PhpScoper\Whitelist;
9
10
/**
11
 * Combines the API or the "traditional" reflector which is about to tell
12
 * if a symbol is internal or not with the more PHP-Scoper specific exposed
13
 * API.
14
 */
15
final class EnrichedReflector
16
{
17
    private Reflector $reflector;
18
    private Whitelist $whitelist;
19
20
    public function __construct(Reflector $reflector, Whitelist $whitelist)
21
    {
22
        $this->reflector = $reflector;
23
        $this->whitelist = $whitelist;
24
    }
25
26
    public function belongsToExcludedNamespace(string $name): bool
27
    {
28
        return $this->whitelist->belongsToExcludedNamespace($name);
29
    }
30
31
    public function isFunctionInternal(string $name): bool
32
    {
33
        return $this->reflector->isFunctionInternal($name);
34
    }
35
36
    public function isFunctionExcluded(string $name): bool
37
    {
38
        return $this->reflector->isFunctionInternal($name)
39
            || $this->whitelist->belongsToExcludedNamespace($name);
40
    }
41
42
    public function isClassInternal(string $name): bool
43
    {
44
        return $this->reflector->isClassInternal($name);
45
    }
46
47
    public function isClassExcluded(string $name): bool
48
    {
49
        return $this->reflector->isClassInternal($name)
50
            || $this->whitelist->belongsToExcludedNamespace($name);
51
    }
52
53
    public function isConstantInternal(string $name): bool
54
    {
55
        return $this->reflector->isConstantInternal($name);
56
    }
57
58
    public function isConstantExcluded(string $name): bool
59
    {
60
        // TODO: double check not sure that internal should mean excluded for constants
61
        return $this->reflector->isConstantInternal($name)
62
            || $this->whitelist->belongsToExcludedNamespace($name);
63
    }
64
65
    public function isExposedFunction(string $resolvedName): bool
66
    {
67
        return !$this->whitelist->belongsToExcludedNamespace($resolvedName)
68
            && !$this->reflector->isFunctionInternal($resolvedName)
69
            && (
70
                $this->whitelist->isExposedFunctionFromGlobalNamespace($resolvedName)
71
                || $this->whitelist->isSymbolExposed($resolvedName)
72
            );
73
    }
74
75
    public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool
76
    {
77
        return !$this->whitelist->belongsToExcludedNamespace($resolvedName)
78
            && !$this->reflector->isFunctionInternal($resolvedName)
79
            && $this->whitelist->isExposedFunctionFromGlobalNamespace($resolvedName);
80
    }
81
82
    public function isExposedClass(string $resolvedName): bool
83
    {
84
        return !$this->whitelist->belongsToExcludedNamespace($resolvedName)
85
            && !$this->reflector->isClassInternal($resolvedName)
86
            && (
87
                $this->whitelist->isExposedClassFromGlobalNamespace($resolvedName)
88
                || $this->whitelist->isSymbolExposed($resolvedName)
89
            );
90
    }
91
92
    public function isExposedClassFromGlobalNamespace(string $resolvedName): bool
93
    {
94
        return !$this->whitelist->belongsToExcludedNamespace($resolvedName)
95
            && !$this->reflector->isClassInternal($resolvedName)
96
            && $this->whitelist->isExposedClassFromGlobalNamespace($resolvedName);
97
    }
98
99
    public function isExposedConstant(string $name): bool
100
    {
101
        // Special case: internal constants must be treated as exposed symbols.
102
        //
103
        // Example: when declaring a new internal constant for compatibility
104
        // reasons, it must remain un-prefixed.
105
        return !$this->whitelist->belongsToExcludedNamespace($name)
106
            && (
107
                $this->reflector->isConstantInternal($name)
108
                || $this->whitelist->isExposedConstantFromGlobalNamespace($name)
109
                || $this->whitelist->isSymbolExposed($name, true)
110
            );
111
    }
112
113
    public function isExposedConstantFromGlobalNamespace(string $constantName): bool
114
    {
115
        return $this->whitelist->isExposedConstantFromGlobalNamespace($constantName);
116
    }
117
}
118