Passed
Push — master ( 8154e5...402dc3 )
by Théo
02:32
created

isExposedConstantFromGlobalNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\Symbol;
6
7
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
8
use function ltrim;
9
use function str_contains;
10
use function strpos;
11
12
/**
13
 * Combines the API or the "traditional" reflector which is about to tell
14
 * if a symbol is internal or not with the more PHP-Scoper specific exposed
15
 * API.
16
 */
17
final class EnrichedReflector
18
{
19
    private Reflector $reflector;
20
    private SymbolsConfiguration $symbolsConfiguration;
21
22
    public function __construct(
23
        Reflector $reflector,
24
        SymbolsConfiguration $symbolsConfiguration
25
    ) {
26
        $this->reflector = $reflector;
27
        $this->symbolsConfiguration = $symbolsConfiguration;
28
    }
29
30
    public function belongsToExcludedNamespace(string $name): bool
31
    {
32
        return $this->symbolsConfiguration
33
            ->getExcludedNamespaces()
34
            ->belongsToRegisteredNamespace($name);
35
    }
36
37
    private function belongsToExposedNamespace(string $name): bool
38
    {
39
        return $this->symbolsConfiguration
40
            ->getExposedNamespaces()
41
            ->belongsToRegisteredNamespace($name);
42
    }
43
44
    public function isFunctionInternal(string $name): bool
45
    {
46
        return $this->reflector->isFunctionInternal($name);
47
    }
48
49
    public function isFunctionExcluded(string $name): bool
50
    {
51
        return $this->reflector->isFunctionInternal($name)
52
            || $this->belongsToExcludedNamespace($name);
53
    }
54
55
    public function isClassInternal(string $name): bool
56
    {
57
        return $this->reflector->isClassInternal($name);
58
    }
59
60
    public function isClassExcluded(string $name): bool
61
    {
62
        return $this->reflector->isClassInternal($name)
63
            || $this->belongsToExcludedNamespace($name);
64
    }
65
66
    public function isConstantInternal(string $name): bool
67
    {
68
        return $this->reflector->isConstantInternal($name);
69
    }
70
71
    public function isConstantExcluded(string $name): bool
72
    {
73
        // TODO: double check not sure that internal should mean excluded for constants
74
        return $this->reflector->isConstantInternal($name)
75
            || $this->belongsToExcludedNamespace($name);
76
    }
77
78
    public function isExposedFunction(string $resolvedName): bool
79
    {
80
        return !$this->belongsToExcludedNamespace($resolvedName)
81
            && !$this->reflector->isFunctionInternal($resolvedName)
82
            && (
83
                $this->_isExposedFunctionFromGlobalNamespace($resolvedName)
84
                || $this->symbolsConfiguration
85
                        ->getExposedFunctions()
86
                        ->matches($resolvedName)
87
                || $this->belongsToExposedNamespace($resolvedName)
88
            );
89
    }
90
91
    public function isExposedFunctionFromGlobalNamespace(string $resolvedName): bool
92
    {
93
        return !$this->belongsToExcludedNamespace($resolvedName)
94
            && !$this->reflector->isFunctionInternal($resolvedName)
95
            && $this->_isExposedFunctionFromGlobalNamespace($resolvedName);
96
    }
97
98
    public function isExposedClass(string $resolvedName): bool
99
    {
100
        return !$this->belongsToExcludedNamespace($resolvedName)
101
            && !$this->reflector->isClassInternal($resolvedName)
102
            && (
103
                $this->_isExposedClassFromGlobalNamespace($resolvedName)
104
                || $this->symbolsConfiguration
105
                    ->getExposedClasses()
106
                    ->matches($resolvedName)
107
                || $this->belongsToExposedNamespace($resolvedName)
108
            );
109
    }
110
111
    public function isExposedClassFromGlobalNamespace(string $resolvedName): bool
112
    {
113
        return !$this->belongsToExcludedNamespace($resolvedName)
114
            && !$this->reflector->isClassInternal($resolvedName)
115
            && $this->_isExposedClassFromGlobalNamespace($resolvedName);
116
    }
117
118
    public function isExposedConstant(string $name): bool
119
    {
120
        // Special case: internal constants must be treated as exposed symbols.
121
        //
122
        // Example: when declaring a new internal constant for compatibility
123
        // reasons, it must remain un-prefixed.
124
        return !$this->belongsToExcludedNamespace($name)
125
            && (
126
                $this->reflector->isConstantInternal($name)
127
                || $this->isExposedConstantFromGlobalNamespace($name)
128
                || $this->symbolsConfiguration
129
                    ->getExposedConstants()
130
                    ->matches($name)
131
                || $this->belongsToExposedNamespace($name)
132
            );
133
    }
134
135
    public function isExposedConstantFromGlobalNamespace(string $constantName): bool
136
    {
137
        // TODO: leverage belongsToGlobalNamespace
138
        return $this->symbolsConfiguration->shouldExposeGlobalConstants() && !strpos($constantName, '\\');
139
    }
140
141
    public function isExcludedNamespace(string $name): bool
142
    {
143
        return $this->symbolsConfiguration
144
            ->getExcludedNamespaces()
145
            ->isRegisteredNamespace($name);
146
    }
147
148
    private function _isExposedFunctionFromGlobalNamespace(string $functionName): bool
149
    {
150
        // TODO: leverage belongsToGlobalNamespace
151
        return $this->symbolsConfiguration->shouldExposeGlobalFunctions() && !strpos($functionName, '\\');
152
    }
153
154
    public function _isExposedClassFromGlobalNamespace(string $className): bool
155
    {
156
        // TODO: leverage belongsToGlobalNamespace
157
        return $this->symbolsConfiguration->shouldExposeGlobalClasses() && !strpos($className, '\\');
158
    }
159
160
    public function belongsToGlobalNamespace(string $symbolName): bool
161
    {
162
        return !str_contains(
163
            ltrim($symbolName, '\\'),
164
            '\\',
165
        );
166
    }
167
}
168