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 | namespace Ducks\Component\SplTypes; |
||
13 | |||
14 | /** |
||
15 | * Trait used for enum emulation |
||
16 | * |
||
17 | * @phpstan-require-extends SplEnum |
||
18 | * |
||
19 | * @psalm-api |
||
20 | */ |
||
21 | trait SplEnumTrait |
||
22 | { |
||
23 | /** |
||
24 | * Return a new instance of enum |
||
25 | * |
||
26 | * @param string $name |
||
27 | * @param mixed[] $arguments |
||
28 | * |
||
29 | * @return static |
||
30 | * |
||
31 | * @throws \Error if $name is not a case |
||
32 | * |
||
33 | * @phpstan-param string $name |
||
34 | * @phpstan-param list<mixed> $arguments |
||
35 | * @phpstan-return static |
||
36 | * @phpstan-ignore-next-line |
||
37 | * |
||
38 | * @psalm-suppress UnsafeInstantiation |
||
39 | */ |
||
40 | #[\ReturnTypeWillChange] |
||
41 | public static function __callStatic(string $name, array $arguments) |
||
42 | { |
||
43 | try { |
||
44 | $class = new \ReflectionClassConstant(static::class, $name); |
||
45 | } catch (\ReflectionException $th) { |
||
46 | throw new \Error('Undefined constant ' . static::class . '::' . $name); |
||
47 | } |
||
48 | |||
49 | // @phpstan-ignore-next-line |
||
50 | return new static($class->getValue()); |
||
51 | } |
||
52 | } |
||
53 |