EnrichedReflector::isConstantExcluded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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