This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Concat\Config\Container; |
||
4 | |||
5 | use UnexpectedValueException; |
||
6 | |||
7 | /** |
||
8 | * Used to evaluate a value according to a list of expected types. |
||
9 | */ |
||
10 | class Evaluator |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Evaluates a value according to a list of expected types. |
||
15 | * |
||
16 | * @param mixed $value The value to evaluate. |
||
17 | * @param array $types The expected types of the evaluated value. |
||
18 | * |
||
19 | * @return mixed |
||
20 | * |
||
21 | * @throws UnexpectedValueException if the value could not be evaluated. |
||
22 | */ |
||
23 | 54 | public static function evaluate($value, array $types) |
|
24 | { |
||
25 | 54 | $valuetype = gettype($value); |
|
26 | |||
27 | 54 | if (in_array($valuetype, $types)) { |
|
28 | 17 | return $value; |
|
29 | } |
||
30 | |||
31 | 38 | foreach ($types as $type) { |
|
32 | |||
33 | // No direct match, delegate by type |
||
34 | 37 | $result = self::delegate($value, $type, $valuetype, $types); |
|
35 | |||
36 | 37 | if ($result !== null) { |
|
37 | 25 | return $result; |
|
38 | } |
||
39 | 13 | } |
|
40 | |||
41 | // Value could not be evaluated to any of the expected types. |
||
42 | 13 | throw new UnexpectedValueException(sprintf( |
|
43 | 13 | "Could not evaluate value for expected type: %s", |
|
44 | 13 | implode(',', $types) |
|
45 | 13 | )); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Calls an evaluation function based on the type of the value. |
||
50 | * |
||
51 | * @param mixed $value The value to evaluate. |
||
52 | * @param string $type The expected type of the evaluated value. |
||
53 | * @param string $valuetype The raw type of the value to evaluate. |
||
54 | * @param array $types Acceptable value types. |
||
55 | * |
||
56 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
57 | */ |
||
58 | 37 | private static function delegate($value, $type, $valuetype, $types) |
|
59 | { |
||
60 | switch ($valuetype) { |
||
61 | 37 | case Value::TYPE_ARRAY: |
|
62 | 37 | case Value::TYPE_OBJECT: |
|
63 | 37 | case Value::TYPE_STRING: |
|
64 | 37 | case Value::TYPE_INTEGER: |
|
65 | 37 | case Value::TYPE_FLOAT: |
|
66 | 36 | return self::{"evaluate".$valuetype}($value, $type, $types); |
|
67 | } |
||
68 | 1 | } |
|
69 | |||
70 | /** |
||
71 | * Attempts to evaluate a string according to an expected type. |
||
72 | * |
||
73 | * @param string $value The value to evaluate. |
||
74 | * @param string $type The expected type of the evaluated value. |
||
75 | * @param array $types Acceptable value types. |
||
76 | * |
||
77 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
78 | */ |
||
79 | 10 | private static function evaluateString($value, $type, $types) |
|
80 | { |
||
81 | 10 | if (class_exists($value)) { |
|
82 | 2 | return self::evaluateObject(new $value, $type, $types); |
|
83 | } |
||
84 | |||
85 | 8 | return self::evaluateUnmatchedString($value, $type); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Attempts to evaluate a string according to an expected type, knowing |
||
90 | * that the expected type was not a string. |
||
91 | * |
||
92 | * @param string $value The value to evaluate. |
||
93 | * @param string $type The expected type of the evaluated value. |
||
94 | * |
||
95 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
96 | */ |
||
97 | 8 | private static function evaluateUnmatchedString($value, $type) |
|
98 | { |
||
99 | 8 | if ($type === Value::TYPE_BOOLEAN) { |
|
100 | |||
101 | // This is better than a boolean cast, as (bool)"false" is true. |
||
102 | 1 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
103 | } |
||
104 | |||
105 | // Check if numeric first otherwise anything evaluates to 0. |
||
106 | 7 | if (is_numeric($value)) { |
|
107 | |||
108 | 3 | if ($type === Value::TYPE_INTEGER) { |
|
109 | 1 | return intval($value); |
|
110 | } |
||
111 | |||
112 | 2 | if ($type === Value::TYPE_FLOAT) { |
|
113 | 1 | return floatval($value); |
|
114 | } |
||
115 | 1 | } |
|
116 | 5 | } |
|
117 | |||
118 | /** |
||
119 | * Attempts to evaluate an integer according to an expected type, knowing |
||
120 | * that the expected type was not an integer. |
||
121 | * |
||
122 | * @param string $value The value to evaluate. |
||
123 | * @param string $type The expected type of the evaluated value. |
||
124 | * |
||
125 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
126 | */ |
||
127 | 6 | View Code Duplication | private static function evaluateInteger($value, $type) |
0 ignored issues
–
show
|
|||
128 | { |
||
129 | switch ($type) { |
||
130 | 6 | case Value::TYPE_BOOLEAN: |
|
131 | 1 | return (bool) $value; |
|
132 | 5 | case Value::TYPE_FLOAT: |
|
133 | 1 | return floatval($value); |
|
134 | 4 | case Value::TYPE_STRING: |
|
135 | 1 | return "$value"; |
|
136 | } |
||
137 | 3 | } |
|
138 | |||
139 | /** |
||
140 | * Attempts to evaluate a float according to an expected type, knowing |
||
141 | * that the expected type was not an float. |
||
142 | * |
||
143 | * @param string $value The value to evaluate. |
||
144 | * @param string $type The expected type of the evaluated value. |
||
145 | * |
||
146 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
147 | */ |
||
148 | 4 | View Code Duplication | private static function evaluateDouble($value, $type) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
149 | { |
||
150 | switch ($type) { |
||
151 | 4 | case Value::TYPE_BOOLEAN: |
|
152 | 1 | return (bool) $value; |
|
153 | 3 | case Value::TYPE_INTEGER: |
|
154 | 1 | return intval($value); |
|
155 | 2 | case Value::TYPE_STRING: |
|
156 | 1 | return "$value"; |
|
157 | } |
||
158 | 1 | } |
|
159 | |||
160 | /** |
||
161 | * Attempts to evaluate an object according to an expected type. |
||
162 | * |
||
163 | * @param object $value The value to evaluate. |
||
164 | * @param string $type The expected type of the evaluated value. |
||
165 | * @param array $types Acceptable value types. |
||
166 | * |
||
167 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
168 | */ |
||
169 | 15 | private static function evaluateObject($value, $type, $types) |
|
170 | { |
||
171 | 15 | if (is_a($value, $type)) { |
|
172 | 11 | return $value; |
|
173 | } |
||
174 | |||
175 | 4 | View Code Duplication | if (is_a($value, Value::TYPE_CLOSURE)) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
176 | |||
177 | // Check if a closure is at all acceptable before evaluating. |
||
178 | 3 | if (in_array(Value::TYPE_CLOSURE, $types)) { |
|
179 | 2 | return $value; |
|
180 | } |
||
181 | |||
182 | 1 | return self::evaluate($value(), [$type]); |
|
183 | } |
||
184 | 1 | } |
|
185 | |||
186 | /** |
||
187 | * Attempts to evaluate an object according to an expected type. |
||
188 | * |
||
189 | * @param array $value The value to evaluate. |
||
190 | * @param string $type The expected type of the evaluated value. |
||
191 | * @param array $types Acceptable value types. |
||
192 | * |
||
193 | * @return mixed|null The evaluated value or null if failed to evaluate. |
||
194 | */ |
||
195 | 5 | private static function evaluateArray($value, $type, $types) |
|
196 | { |
||
197 | 5 | View Code Duplication | if (is_callable($value)) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
198 | |||
199 | // Check if a callable is at all acceptable before evaluating. |
||
200 | 4 | if (in_array(Value::TYPE_CALLABLE, $types)) { |
|
201 | 2 | return $value; |
|
202 | } |
||
203 | |||
204 | 2 | return self::evaluate($value(), [$type]); |
|
205 | } |
||
206 | 1 | } |
|
207 | } |
||
208 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.