Passed
Push — master ( 034eb5...6e4694 )
by Théo
03:24 queued 01:46
created

Reflector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
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;
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
    private const MISSING_CONSTANTS = [
67
        'STDIN',
68
        'STDOUT',
69
        'STDERR',
70
        // Added in PHP 7.4
71
        'T_BAD_CHARACTER',
72
        'T_FN',
73
        'T_COALESCE_EQUAL',
74
        // Added in PHP 8.0
75
        'T_NAME_QUALIFIED',
76 571
        'T_NAME_FULLY_QUALIFIED',
77
        'T_NAME_RELATIVE',
78 571
        'T_MATCH',
79
        'T_NULLSAFE_OBJECT_OPERATOR',
80
        'T_ATTRIBUTE',
81 310
    ];
82
83
    /**
84 310
     * @var array<string,true>
85 44
     */
86 44
    private array $classes;
87
88
    /**
89
     * @var array<string,true>
90 98
     */
91
    private array $functions;
92 98
93 2
    /**
94
     * @var array<string,true>
95
     */
96
    private array $constants;
97 96
98 77
    public static function createWithPhpStormStubs(): self
99 77
    {
100
        return new self(
101
            self::createSymbolList(
102
                array_keys(PhpStormStubsMap::CLASSES),
103 79
                self::MISSING_CLASSES,
104
            ),
105 79
            self::createSymbolList(
106 1
                array_keys(PhpStormStubsMap::FUNCTIONS),
107
                self::MISSING_FUNCTIONS,
108
            ),
109 78
            self::createSymbolList(
110
                array_keys(PhpStormStubsMap::CONSTANTS),
111
                self::MISSING_CONSTANTS,
112 78
            ),
113
        );
114 78
    }
115 28
116
    /**
117
     * @param array<string, true> $classes
118 78
     * @param array<string, true> $functions
119
     * @param array<string, true> $constants
120 78
     */
121
    public function __construct(array $classes, array $functions, array $constants)
122 78
    {
123
        $this->classes = $classes;
124 78
        $this->functions = $functions;
125
        $this->constants = $constants;
126
    }
127
128
    /**
129
     * @param string[] $classes
130
     * @param string[] $functions
131
     * @param string[] $constants
132
     */
133
    public function withSymbols(
134
        array $classes,
135
        array $functions,
136
        array $constants
137
    ): self
138
    {
139
        return new self(
140
            self::createSymbolList(
141
                array_keys($this->classes),
142
                $classes,
143
            ),
144
            self::createSymbolList(
145
                array_keys($this->functions),
146
                $functions,
147
            ),
148
            self::createSymbolList(
149
                array_keys($this->constants),
150
                $constants,
151
            ),
152
        );
153
    }
154
155
    public function isClassInternal(string $name): bool
156
    {
157
        return isset($this->classes[$name]);
158
    }
159
160
    public function isFunctionInternal(string $name): bool
161
    {
162
        return isset($this->functions[strtolower($name)]);
163
    }
164
165
    public function isConstantInternal(string $name): bool
166
    {
167
        return isset($this->constants[$name]);
168
    }
169
170
    /**
171
     * @param string[][] $sources
172
     *
173
     * @return array<string, true>
174
     */
175
    private static function createSymbolList(array ...$sources): array
176
    {
177
        return array_fill_keys(
178
            array_merge(...$sources),
179
            true
180
        );
181
    }
182
}
183