Passed
Push — master ( 907d34...35aaaf )
by Théo
02:07
created

Reflector::createWithPhpStormStubs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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