Passed
Push — 7.x ( 42e260...f51c13 )
by Adrien
09:26
created

getObjectReflectionNamedType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Part of SplTypes package.
5
 *
6
 * (c) Adrien Loyant <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Ducks\Component\SplTypes\Reflection;
15
16
/**
17
 * The ReflectionEnum class reports information about an Enum.
18
 *
19
 * @link https://php.net/manual/en/class.reflectionenum.php
20
 */
21
final class SplReflectionEnumHelper
22
{
23
    /**
24
     * Array of \ReflectionNamedType for types
25
     *
26
     * @var \ReflectionNamedType[]
27
     *
28
     * @phpstan-var array<string,\ReflectionNamedType>
29
     */
30
    private static array $rnt = [];
31
32
    /**
33
     * @psalm-suppress UnusedConstructor
34
     */
35
    private function __construct()
36
    {
37
    }
38
39
    /**
40
     * Get \ReflectionNamedType from internal ReflectionFunction closure.
41
     *
42
     * @param \ReflectionFunction $func
43
     *
44
     * @return \ReflectionNamedType
45
     */
46
    private static function getReflectionNamedType(\ReflectionFunction $func): \ReflectionNamedType
47
    {
48
        /** @var \ReflectionNamedType $type */
49
        $type = ($func->getParameters()[0])->getType();
50
51
        return $type;
52
    }
53
54
    /**
55
     * Get SplReflectionNamedType from a type.
56
     *
57
     * @param string $type
58
     * @return \ReflectionNamedType
59
     *
60
     * @phpstan-param non-empty-string $type
61
     * @phpstan-return \ReflectionNamedType
62
     */
63
    private static function getTypedReflectionNamedType(string $type): \ReflectionNamedType
64
    {
65
        if (!isset(static::$rnt[$type])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
66
            static::$rnt[$type] = new SplReflectionNamedType($type);
67
        }
68
69
        return static::$rnt[$type];
70
    }
71
72
    /**
73
     * Return ReflectionNamedType from a $type (Internal use).
74
     *
75
     * @param string $type
76
     * @return \ReflectionNamedType
77
     */
78
    public static function getReflectionNamedTypeFromType(string $type): \ReflectionNamedType
79
    {
80
        switch ($type) {
81
            case 'string':
82
                $result = self::getStringReflectionNamedType();
83
                break;
84
85
            case 'integer':
86
            case 'int':
87
                $result = self::getIntReflectionNamedType();
88
                break;
89
90
            case 'double':
91
            case 'float':
92
                $result = self::getFloatReflectionNamedType();
93
                break;
94
95
            case 'boolean':
96
            case 'bool':
97
                $result = self::getBoolReflectionNamedType();
98
                break;
99
100
            case 'array':
101
                $result = self::getArrayReflectionNamedType();
102
                break;
103
104
            case 'object':
105
                $result = self::getObjectReflectionNamedType();
106
                break;
107
108
            case 'mixed':
109
            case 'unknown type':
110
                $result = self::getTypedReflectionNamedType('mixed');
111
                break;
112
113
            default:
114
                /** @phpstan-var non-empty-string $type */
115
                $result = self::getTypedReflectionNamedType($type);
116
                break;
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * Only way to generate a string ReflectionNamedType (Internal use).
124
     *
125
     * @return \ReflectionNamedType
126
     */
127
    public static function getStringReflectionNamedType(): \ReflectionNamedType
128
    {
129
        if (!isset(static::$rnt['string'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
130
            static::$rnt['string'] = static::getReflectionNamedType(
131
                new \ReflectionFunction(static fn(string $param): string => $param)
132
            );
133
        }
134
135
        return static::$rnt['string'];
136
    }
137
138
    /**
139
     * Only way to generate an int ReflectionNamedType (Internal use).
140
     *
141
     * @return \ReflectionNamedType
142
     */
143
    public static function getIntReflectionNamedType(): \ReflectionNamedType
144
    {
145
        if (!isset(static::$rnt['int'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
146
            static::$rnt['int'] = static::getReflectionNamedType(
147
                new \ReflectionFunction(static fn (int $param): int => $param)
148
            );
149
        }
150
151
        return static::$rnt['int'];
152
    }
153
154
    /**
155
     * Only way to generate a float ReflectionNamedType (Internal use).
156
     *
157
     * @return \ReflectionNamedType
158
     */
159
    public static function getFloatReflectionNamedType(): \ReflectionNamedType
160
    {
161
        if (!isset(static::$rnt['float'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
162
            static::$rnt['float'] = static::getReflectionNamedType(
163
                new \ReflectionFunction(static fn(float $param): float => $param)
164
            );
165
        }
166
167
        return static::$rnt['float'];
168
    }
169
170
    /**
171
     * Only way to generate a bool ReflectionNamedType (Internal use).
172
     *
173
     * @return \ReflectionNamedType
174
     */
175
    public static function getBoolReflectionNamedType(): \ReflectionNamedType
176
    {
177
        if (!isset(static::$rnt['bool'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
178
            static::$rnt['bool'] = static::getReflectionNamedType(
179
                new \ReflectionFunction(static fn(bool $param): bool => $param)
180
            );
181
        }
182
183
        return static::$rnt['bool'];
184
    }
185
186
    /**
187
     * Only way to generate an array ReflectionNamedType (Internal use).
188
     *
189
     * @return \ReflectionNamedType
190
     */
191
    public static function getArrayReflectionNamedType(): \ReflectionNamedType
192
    {
193
        if (!isset(static::$rnt['array'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
194
            static::$rnt['array'] = static::getReflectionNamedType(
195
                new \ReflectionFunction(static fn(array $param): array => $param)
196
            );
197
        }
198
199
        return static::$rnt['array'];
200
    }
201
202
    /**
203
     * Only way to generate an object ReflectionNamedType (Internal use).
204
     *
205
     * @return \ReflectionNamedType
206
     */
207
    public static function getObjectReflectionNamedType(): \ReflectionNamedType
208
    {
209
        if (!isset(static::$rnt['object'])) {
0 ignored issues
show
Bug introduced by
Since $rnt is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $rnt to at least protected.
Loading history...
210
            static::$rnt['object'] = static::getReflectionNamedType(
211
                new \ReflectionFunction(static fn(object $param): object => $param)
212
            );
213
        }
214
215
        return static::$rnt['object'];
216
    }
217
}
218