Passed
Push — master ( 078b09...c0db64 )
by Théo
04:10 queued 02:16
created

Reflector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
eloc 82
dl 0
loc 165
ccs 23
cts 23
cp 1
rs 10
c 8
b 1
f 0
wmc 8

8 Methods

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