1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\NestedAccessor\Helpers; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
|
7
|
|
|
class KeyAccessHelper |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @template T |
11
|
|
|
* @param array<string, T>|ArrayAccess<string, T>|object|mixed $container |
12
|
|
|
* @param string $key |
13
|
|
|
* @param T|null $defaultValue |
|
|
|
|
14
|
|
|
* @return T|null |
15
|
|
|
*/ |
16
|
|
|
public static function get($container, string $key, $defaultValue = null) |
17
|
|
|
{ |
18
|
|
|
switch(true) { |
19
|
|
|
case is_array($container): |
20
|
|
|
return static::getFromArray($container, $key, $defaultValue); |
21
|
|
|
case $container instanceof ArrayAccess: |
22
|
|
|
return static::getFromArrayAccess($container, $key, $defaultValue); |
23
|
|
|
case is_object($container): |
24
|
|
|
return static::getFromObject($container, $key, $defaultValue); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $defaultValue; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array<string, mixed>|ArrayAccess<string, mixed>|object|mixed $container |
32
|
|
|
* @param string $key |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public static function exists($container, string $key): bool |
36
|
|
|
{ |
37
|
|
|
switch(true) { |
38
|
|
|
case is_array($container): |
39
|
|
|
return static::existsInArray($container, $key); |
40
|
|
|
case $container instanceof ArrayAccess: |
41
|
|
|
return static::existsInArrayAccess($container, $key); |
42
|
|
|
case is_object($container): |
43
|
|
|
return static::existsInObject($container, $key); |
44
|
|
|
} |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @template T |
50
|
|
|
* @param array<string, T> $container |
51
|
|
|
* @param string $key |
52
|
|
|
* @param T|null $defaultValue |
53
|
|
|
* @return T|null |
54
|
|
|
*/ |
55
|
|
|
protected static function getFromArray(array $container, string $key, $defaultValue) |
56
|
|
|
{ |
57
|
|
|
if(static::existsInArray($container, $key)) { |
58
|
|
|
return $container[$key]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $defaultValue ?? null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @template T |
66
|
|
|
* @param array<string, T> $container |
67
|
|
|
* @param string $key |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected static function existsInArray(array $container, string $key): bool |
71
|
|
|
{ |
72
|
|
|
return array_key_exists($key, $container); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @template T |
77
|
|
|
* @param ArrayAccess<string, T> $container |
78
|
|
|
* @param string $key |
79
|
|
|
* @param T|null $defaultValue |
80
|
|
|
* @return T|null |
81
|
|
|
*/ |
82
|
|
|
protected static function getFromArrayAccess(ArrayAccess $container, string $key, $defaultValue) |
83
|
|
|
{ |
84
|
|
|
if(static::existsInArrayAccess($container, $key)) { |
85
|
|
|
return $container[$key]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $defaultValue ?? null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @template T |
93
|
|
|
* @param ArrayAccess<string, T> $container |
94
|
|
|
* @param string $key |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
protected static function existsInArrayAccess(ArrayAccess $container, string $key): bool |
98
|
|
|
{ |
99
|
|
|
return $container->offsetExists($key); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param object $container |
104
|
|
|
* @param string $key |
105
|
|
|
* @param mixed|null $defaultValue |
106
|
|
|
* @return mixed|null |
107
|
|
|
*/ |
108
|
|
|
protected static function getFromObject(object $container, string $key, $defaultValue) |
109
|
|
|
{ |
110
|
|
|
if(ObjectHelper::hasPublicProperty($container, $key)) { |
111
|
|
|
return $container->{$key} ?? $defaultValue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if(ObjectHelper::hasPropertyAccessibleByGetter($container, $key)) { |
115
|
|
|
return ObjectHelper::getPropertyByGetter($container, $key); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $defaultValue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param object $container |
123
|
|
|
* @param string $key |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected static function existsInObject(object $container, string $key): bool |
127
|
|
|
{ |
128
|
|
|
return ObjectHelper::hasPublicProperty($container, $key) |
129
|
|
|
|| ObjectHelper::hasPropertyAccessibleByGetter($container, $key); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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