1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
6
|
|
|
|
7
|
|
|
use BackedEnum; |
8
|
|
|
use BEAR\Resource\Exception\ParameterEnumTypeException; |
9
|
|
|
use BEAR\Resource\Exception\ParameterException; |
10
|
|
|
use BEAR\Resource\Exception\ParameterInvalidEnumException; |
11
|
|
|
use Ray\Di\InjectorInterface; |
12
|
|
|
use ReflectionEnum; |
|
|
|
|
13
|
|
|
use ReflectionNamedType; |
14
|
|
|
use ReflectionParameter; |
15
|
|
|
use UnitEnum; |
16
|
|
|
|
17
|
|
|
use function assert; |
18
|
|
|
use function class_exists; |
19
|
|
|
use function enum_exists; |
|
|
|
|
20
|
|
|
use function is_a; |
21
|
|
|
use function is_int; |
22
|
|
|
use function is_string; |
23
|
|
|
use function ltrim; |
24
|
|
|
use function preg_replace; |
25
|
|
|
use function strtolower; |
26
|
|
|
|
27
|
|
|
final class ScalarParam implements ParamInterface |
28
|
|
|
{ |
29
|
|
|
private readonly string $type; |
30
|
|
|
private readonly bool $isDefaultAvailable; |
31
|
|
|
private readonly mixed $defaultValue; // @phpstan-ignore-line |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
ReflectionNamedType $type, |
35
|
|
|
ReflectionParameter $parameter, |
36
|
|
|
) { |
37
|
|
|
$this->type = $type->getName(); |
|
|
|
|
38
|
|
|
$this->isDefaultAvailable = $parameter->isDefaultValueAvailable(); |
|
|
|
|
39
|
|
|
if (! $this->isDefaultAvailable) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->defaultValue = $parameter->getDefaultValue(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function __invoke(string $varName, array $query, InjectorInterface $injector) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
/** @psalm-suppress MixedAssignment */ |
53
|
|
|
$props = $this->getProps($varName, $query, $injector); |
54
|
|
|
} catch (ParameterException $e) { |
55
|
|
|
if ($this->isDefaultAvailable) { |
56
|
|
|
return $this->defaultValue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw $e; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
assert(class_exists($this->type)); |
63
|
|
|
|
64
|
|
|
return new $this->type($props); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @param array<string, mixed> $query */ |
68
|
|
|
private function getProps(string $varName, array $query, InjectorInterface $injector): mixed |
69
|
|
|
{ |
70
|
|
|
if (isset($query[$varName])) { |
71
|
|
|
return $query[$varName]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// try camelCase variable name |
75
|
|
|
$snakeName = ltrim(strtolower((string) preg_replace('/[A-Z]/', '_\0', $varName)), '_'); |
76
|
|
|
if (isset($query[$snakeName])) { |
77
|
|
|
return $query[$snakeName]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
unset($injector); |
81
|
|
|
|
82
|
|
|
throw new ParameterException($varName); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param class-string $type |
|
|
|
|
87
|
|
|
* |
88
|
|
|
* @psalm-suppress MixedArgument |
89
|
|
|
*/ |
90
|
|
|
private function enum(string $type, mixed $props, string $varName): mixed |
91
|
|
|
{ |
92
|
|
|
/** @var class-string<UnitEnum> $type */ |
93
|
|
|
$refEnum = new ReflectionEnum($type); |
94
|
|
|
assert(enum_exists($type)); |
|
|
|
|
95
|
|
|
|
96
|
|
|
if (! $refEnum->isBacked()) { |
97
|
|
|
throw new NotBackedEnumException($type); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
assert(is_a($type, BackedEnum::class, true)); |
101
|
|
|
if (! (is_int($props) || is_string($props))) { |
102
|
|
|
throw new ParameterEnumTypeException($varName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @psalm-suppress MixedAssignment */ |
106
|
|
|
$value = $type::tryFrom($props); |
107
|
|
|
if ($value === null) { |
108
|
|
|
throw new ParameterInvalidEnumException($varName); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $value; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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