|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Type\BuiltIn\Enum\Trait; |
|
15
|
|
|
|
|
16
|
|
|
use BackedEnum; |
|
|
|
|
|
|
17
|
|
|
use UnitEnum; |
|
|
|
|
|
|
18
|
|
|
use Valkyrja\Type\Throwable\Exception\InvalidArgumentException; |
|
19
|
|
|
use Valkyrja\Type\Throwable\Exception\RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
use function is_int; |
|
22
|
|
|
use function is_string; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Trait Enum. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Melech Mizrachi |
|
28
|
|
|
*/ |
|
29
|
|
|
trait Enum |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Get a new Type given a value. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function fromValue(mixed $value): static |
|
35
|
|
|
{ |
|
36
|
|
|
if ($value instanceof static) { |
|
37
|
|
|
return $value; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @var class-string<UnitEnum>|class-string<BackedEnum> $class */ |
|
41
|
|
|
$class = static::class; |
|
42
|
|
|
|
|
43
|
|
|
if (! is_string($value) && ! is_int($value)) { |
|
44
|
|
|
throw new InvalidArgumentException("Invalid value provided for enum $class"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Need to check BackedEnum first because all Enums are UnitEnum |
|
48
|
|
|
if (is_a($class, BackedEnum::class, true)) { |
|
49
|
|
|
/** @var static $case Get Psalm working and understanding that the static is what gets returned here */ |
|
50
|
|
|
$case = $class::from($value); |
|
51
|
|
|
|
|
52
|
|
|
return $case; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Fallback to iterating over all the cases and find a match |
|
56
|
|
|
foreach ($class::cases() as $case) { |
|
57
|
|
|
if ($case->name === $value) { |
|
58
|
|
|
/** @var static $case Get Psalm working and understanding that the static is what gets returned here */ |
|
59
|
|
|
return $case; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new InvalidArgumentException("Invalid value provided for enum $class"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
* |
|
69
|
|
|
* @psalm-suppress ImplementedReturnTypeMismatch The inherited return type 'static' for Valkyrja\Type\Type::asValue is different to the implemented return type for Valkyrja\Type\Types\Enum::asvalue 'Valkyrja\Enum&static' |
|
70
|
|
|
*/ |
|
71
|
|
|
public function asValue(): static |
|
72
|
|
|
{ |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function asFlatValue(): string|int |
|
80
|
|
|
{ |
|
81
|
|
|
// Need to check BackedEnum first because all Enums are UnitEnum |
|
82
|
|
|
if ($this instanceof BackedEnum) { |
|
83
|
|
|
return $this->value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Fallback to UnitEnum name property |
|
87
|
|
|
/** @var string $name */ |
|
88
|
|
|
$name = $this->name; |
|
89
|
|
|
|
|
90
|
|
|
return $name; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @inheritDoc |
|
95
|
|
|
*/ |
|
96
|
|
|
public function modify(callable $closure): static |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
throw new RuntimeException('Cannot modify an enum.'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritDoc |
|
103
|
|
|
*/ |
|
104
|
|
|
public function jsonSerialize(): string|int |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->asFlatValue(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths