Passed
Pull Request — main (#20)
by Anatoly
03:33
created

Enum::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
namespace Sunrise\Hydrator;
13
14
/**
15
 * Import classes
16
 */
17
use JsonSerializable;
18
use ReflectionClass;
19
use RuntimeException;
20
21
/**
22
 * Import functions
23
 */
24
use function is_int;
25
use function is_string;
26
use function sprintf;
27
28
/**
29
 * Abstract enum
30
 *
31
 * @since 2.6.0
32
 */
33
abstract class Enum implements JsonSerializable
34
{
35
36
    /**
37
     * Cached cases of the enum
38
     *
39
     * @var array<class-string<static>, list<static>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<static>, list<static>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<static>, list<static>>.
Loading history...
40
     */
41
    private static array $cases = [];
42
43
    /**
44
     * The name of the enum's case
45
     *
46
     * @var string
47
     *
48
     * @readonly
49
     */
50
    private string $name;
51
52
    /**
53
     * The value of the enum's case
54
     *
55
     * @var int|string
56
     *
57
     * @readonly
58
     */
59
    private $value;
60
61
    /**
62
     * Constructor of the class
63
     *
64
     * @param string $name
65
     * @param int|string $value
66
     */
67
    final protected function __construct(string $name, $value)
68
    {
69
        $this->name = $name;
70
        $this->value = $value;
71
    }
72
73
    /**
74
     * Gets the name of the enum's case
75
     *
76
     * @return string
77
     */
78 1
    final public function name(): string
79
    {
80 1
        return $this->name;
81
    }
82
83
    /**
84
     * Gets the value of the enum's case
85
     *
86
     * @return int|string
87
     */
88 3
    final public function value()
89
    {
90 3
        return $this->value;
91
    }
92
93
    /**
94
     * Gets all cases of the enum
95
     *
96
     * @return list<static>
0 ignored issues
show
Bug introduced by
The type Sunrise\Hydrator\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
     */
98 14
    final public static function cases(): array
99
    {
100 14
        if (isset(self::$cases[static::class])) {
101 14
            return self::$cases[static::class];
102
        }
103
104
        $class = new ReflectionClass(static::class);
105
        $constants = $class->getReflectionConstants();
106
        foreach ($constants as $constant) {
107
            $owner = $constant->getDeclaringClass();
108
            if ($owner->getName() === self::class) {
109
                continue;
110
            }
111
112
            $name = $constant->getName();
113
            $value = $constant->getValue();
114
115
            if (!is_int($value) && !is_string($value)) {
116
                continue;
117
            }
118
119
            self::$cases[static::class][] = new static($name, $value);
120
        }
121
122
        return self::$cases[static::class];
123
    }
124
125
    /**
126
     * Tries to initialize the enum from the given case's value
127
     *
128
     * @param int|string $value
129
     *
130
     * @return static|null
131
     */
132 9
    final public static function tryFrom($value)
133
    {
134 9
        foreach (self::cases() as $case) {
135 9
            if ($case->value == $value) {
136 7
                return $case;
137
            }
138
        }
139
140 2
        return null;
141
    }
142
143
    /**
144
     * Tries to initialize the enum from the given case's name
145
     *
146
     * @param string $name
147
     *
148
     * @return static
149
     *
150
     * @throws RuntimeException
151
     */
152 6
    final public static function __callStatic(string $name, array $arguments = [])
153
    {
154 6
        foreach (self::cases() as $case) {
155 6
            if ($case->name === $name) {
156 5
                return $case;
157
            }
158
        }
159
160 1
        throw new RuntimeException(sprintf('Enum case %1$s::%2$s not found', static::class, $name));
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 1
    #[\ReturnTypeWillChange]
167
    public function jsonSerialize()
168
    {
169 1
        return $this->value;
170
    }
171
}
172