|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource\Input; |
|
6
|
|
|
|
|
7
|
|
|
use BackedEnum; |
|
8
|
|
|
use BEAR\Resource\Exception\NotBackedEnumException; |
|
9
|
|
|
use BEAR\Resource\Exception\ParameterEnumTypeException; |
|
10
|
|
|
use BEAR\Resource\Exception\ParameterException; |
|
11
|
|
|
use BEAR\Resource\Exception\ParameterInvalidEnumException; |
|
12
|
|
|
use ReflectionEnum; |
|
|
|
|
|
|
13
|
|
|
use ReflectionNamedType; |
|
14
|
|
|
use UnitEnum; |
|
15
|
|
|
|
|
16
|
|
|
use function assert; |
|
17
|
|
|
use function enum_exists; |
|
|
|
|
|
|
18
|
|
|
use function is_a; |
|
19
|
|
|
use function is_int; |
|
20
|
|
|
use function is_string; |
|
21
|
|
|
use function ltrim; |
|
22
|
|
|
use function preg_replace; |
|
23
|
|
|
use function strtolower; |
|
24
|
|
|
|
|
25
|
|
|
final class InputParamEnumHandler |
|
26
|
|
|
{ |
|
27
|
|
|
/** @param array<string, mixed> $query */ |
|
28
|
|
|
public function createEnum( |
|
29
|
|
|
string $type, |
|
30
|
|
|
string $varName, |
|
31
|
|
|
array $query, |
|
32
|
|
|
bool $isDefaultAvailable, |
|
33
|
|
|
mixed $defaultValue, |
|
34
|
|
|
): mixed { |
|
35
|
|
|
$props = $this->getPropsForClassParam($varName, $query, $isDefaultAvailable, $defaultValue); |
|
36
|
|
|
|
|
37
|
|
|
/** @var class-string<UnitEnum> $type */ |
|
38
|
|
|
$refEnum = new ReflectionEnum($type); |
|
39
|
|
|
assert(enum_exists($type)); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (! $refEnum->isBacked()) { |
|
42
|
|
|
throw new NotBackedEnumException($type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
assert(is_a($type, BackedEnum::class, true)); |
|
46
|
|
|
if (! (is_int($props) || is_string($props))) { |
|
47
|
|
|
if ($isDefaultAvailable) { |
|
|
|
|
|
|
48
|
|
|
return $defaultValue; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new ParameterEnumTypeException($varName); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Get the backing type of the enum |
|
55
|
|
|
$backingType = $refEnum->getBackingType(); |
|
56
|
|
|
if ($backingType instanceof ReflectionNamedType && $backingType->getName() === 'int' && is_string($props)) { |
|
57
|
|
|
$props = (int) $props; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @psalm-suppress MixedAssignment */ |
|
61
|
|
|
$value = $type::tryFrom($props); |
|
62
|
|
|
if ($value === null) { |
|
63
|
|
|
throw new ParameterInvalidEnumException($varName); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $value; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @param array<string, mixed> $query */ |
|
70
|
|
|
private function getPropsForClassParam( |
|
71
|
|
|
string $varName, |
|
72
|
|
|
array $query, |
|
73
|
|
|
bool $isDefaultAvailable, |
|
74
|
|
|
mixed $defaultValue, |
|
75
|
|
|
): mixed { |
|
76
|
|
|
if (isset($query[$varName])) { |
|
77
|
|
|
return $query[$varName]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$snakeName = ltrim(strtolower((string) preg_replace('/[A-Z]/', '_\0', $varName)), '_'); |
|
81
|
|
|
if (isset($query[$snakeName])) { |
|
82
|
|
|
return $query[$snakeName]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($isDefaultAvailable) { |
|
86
|
|
|
return $defaultValue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
throw new ParameterException($varName); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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