Passed
Push — master ( a6702a...790ab7 )
by Théo
02:26
created

Reflector::withSymbols()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
/** @noinspection ClassConstantCanBeUsedInspection */
4
5
declare(strict_types=1);
6
7
/*
8
 * This file is part of the humbug/php-scoper package.
9
 *
10
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
11
 *                    Pádraic Brady <[email protected]>
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace Humbug\PhpScoper;
18
19
use Humbug\PhpScoper\Symbol\SymbolRegistry;
20
use JetBrains\PHPStormStub\PhpStormStubsMap;
21
use function array_keys;
22
use function array_merge;
23
24
/**
25
 * @private
26
 */
27
final class Reflector
28
{
29
    private const MISSING_CLASSES = [
30
        // https://github.com/JetBrains/phpstorm-stubs/pull/594
31
        'parallel\Channel',
32
        'parallel\Channel\Error',
33
        'parallel\Channel\Error\Closed',
34
        'parallel\Channel\Error\Existence',
35
        'parallel\Channel\Error\IllegalValue',
36
        'parallel\Error',
37
        'parallel\Events',
38
        'parallel\Events\Error',
39
        'parallel\Events\Error\Existence',
40
        'parallel\Events\Error\Timeout',
41
        'parallel\Events\Event',
42
        'parallel\Events\Event\Type',
43
        'parallel\Events\Input',
44
        'parallel\Events\Input\Error',
45
        'parallel\Events\Input\Error\Existence',
46
        'parallel\Events\Input\Error\IllegalValue',
47
        'parallel\Future',
48
        'parallel\Future\Error',
49
        'parallel\Future\Error\Cancelled',
50
        'parallel\Future\Error\Foreign',
51
        'parallel\Future\Error\Killed',
52
        'parallel\Runtime',
53
        'parallel\Runtime\Bootstrap',
54
        'parallel\Runtime\Error',
55
        'parallel\Runtime\Error\Bootstrap',
56
        'parallel\Runtime\Error\Closed',
57
        'parallel\Runtime\Error\IllegalFunction',
58
        'parallel\Runtime\Error\IllegalInstruction',
59
        'parallel\Runtime\Error\IllegalParameter',
60
        'parallel\Runtime\Error\IllegalReturn',
61
    ];
62
63
    private const MISSING_FUNCTIONS = [];
64
65
    /**
66
     * Basically mirrors https://github.com/nikic/PHP-Parser/blob/9aebf377fcdf205b2156cb78c0bd6e7b2003f106/lib/PhpParser/Lexer.php#L430
67
     */
68
    private const MISSING_CONSTANTS = [
69
        'STDIN',
70
        'STDOUT',
71
        'STDERR',
72
        // Added in PHP 7.4
73
        'T_BAD_CHARACTER',
74
        'T_FN',
75
        'T_COALESCE_EQUAL',
76 571
        // Added in PHP 8.0
77
        'T_NAME_QUALIFIED',
78 571
        'T_NAME_FULLY_QUALIFIED',
79
        'T_NAME_RELATIVE',
80
        'T_MATCH',
81 310
        'T_NULLSAFE_OBJECT_OPERATOR',
82
        'T_ATTRIBUTE',
83
        // Added in PHP 8.1
84 310
        'T_ENUM',
85 44
        'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG',
86 44
        'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG',
87
        'T_READONLY',
88
    ];
89
90 98
    private SymbolRegistry $classes;
91
    private SymbolRegistry $functions;
92 98
    private SymbolRegistry $constants;
93 2
94
    public static function createWithPhpStormStubs(): self
95
    {
96
        return new self(
97 96
            self::createSymbolList(
98 77
                array_keys(PhpStormStubsMap::CLASSES),
99 77
                self::MISSING_CLASSES,
100
            ),
101
            self::createSymbolList(
102
                array_keys(PhpStormStubsMap::FUNCTIONS),
103 79
                self::MISSING_FUNCTIONS,
104
            ),
105 79
            self::createConstantSymbolList(
106 1
                array_keys(PhpStormStubsMap::CONSTANTS),
107
                self::MISSING_CONSTANTS,
108
            ),
109 78
        );
110
    }
111
112 78
    public static function createEmpty(): self
113
    {
114 78
        return new self(
115 28
            SymbolRegistry::create(),
116
            SymbolRegistry::create(),
117
            SymbolRegistry::createForConstants(),
118 78
        );
119
    }
120 78
121
    private function __construct(
122 78
        SymbolRegistry $classes,
123
        SymbolRegistry $functions,
124 78
        SymbolRegistry $constants
125
    ) {
126
        $this->classes = $classes;
127
        $this->functions = $functions;
128
        $this->constants = $constants;
129
    }
130
131
    /**
132
     * @param string[] $classNames
133
     * @param string[] $functionNames
134
     * @param string[] $constantNames
135
     */
136
    public function withSymbols(
137
        array $classNames,
138
        array $functionNames,
139
        array $constantNames
140
    ): self
141
    {
142
        return new self(
143
            $this->classes->withAdditionalSymbols($classNames),
144
            $this->functions->withAdditionalSymbols($functionNames),
145
            $this->constants->withAdditionalSymbols($constantNames),
146
        );
147
    }
148
149
    public function isClassInternal(string $name): bool
150
    {
151
        return $this->classes->matches($name);
152
    }
153
154
    public function isFunctionInternal(string $name): bool
155
    {
156
        return $this->functions->matches($name);
157
    }
158
159
    public function isConstantInternal(string $name): bool
160
    {
161
        return $this->constants->matches($name);
162
    }
163
164
    /**
165
     * @param string[] $sources
166
     */
167
    private static function createSymbolList(array ...$sources): SymbolRegistry
168
    {
169
        return SymbolRegistry::create(
170
            array_merge(...$sources),
171
        );
172
    }
173
174
    /**
175
     * @param string[] $sources
176
     */
177
    private static function createConstantSymbolList(array ...$sources): SymbolRegistry
178
    {
179
        return SymbolRegistry::createForConstants(
180
            array_merge(...$sources),
181
        );
182
    }
183
}
184