Passed
Push — master ( 499990...210c58 )
by Théo
01:59
created

EnrichedReflector::isSymbolExposed()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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