@@ -49,298 +49,298 @@ |
||
49 | 49 | */ |
50 | 50 | class Exporter |
51 | 51 | { |
52 | - /** |
|
53 | - * Exports a value as a string. |
|
54 | - * |
|
55 | - * The output of this method is similar to the output of print_r(), but |
|
56 | - * improved in various aspects: |
|
57 | - * |
|
58 | - * - NULL is rendered as "null" (instead of "") |
|
59 | - * - TRUE is rendered as "true" (instead of "1") |
|
60 | - * - FALSE is rendered as "false" (instead of "") |
|
61 | - * - Strings are always quoted with single quotes |
|
62 | - * - Carriage returns and newlines are normalized to \n |
|
63 | - * - Recursion and repeated rendering is treated properly |
|
64 | - * |
|
65 | - * @param int $indentation The indentation level of the 2nd+ line |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function export($value, $indentation = 0) |
|
70 | - { |
|
71 | - return $this->recursiveExport($value, $indentation); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param array<mixed> $data |
|
76 | - * @param Context $context |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function shortenedRecursiveExport(&$data, Context $context = null) |
|
81 | - { |
|
82 | - $result = []; |
|
83 | - $exporter = new self(); |
|
84 | - |
|
85 | - if (!$context) { |
|
86 | - $context = new Context; |
|
87 | - } |
|
88 | - |
|
89 | - $array = $data; |
|
90 | - $context->add($data); |
|
91 | - |
|
92 | - foreach ($array as $key => $value) { |
|
93 | - if (is_array($value)) { |
|
94 | - if ($context->contains($data[$key]) !== false) { |
|
95 | - $result[] = '*RECURSION*'; |
|
96 | - } else { |
|
97 | - $result[] = sprintf( |
|
98 | - 'array(%s)', |
|
99 | - $this->shortenedRecursiveExport($data[$key], $context) |
|
100 | - ); |
|
101 | - } |
|
102 | - } else { |
|
103 | - $result[] = $exporter->shortenedExport($value); |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - return implode(', ', $result); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Exports a value into a single-line string. |
|
112 | - * |
|
113 | - * The output of this method is similar to the output of |
|
114 | - * SebastianBergmann\Exporter\Exporter::export(). |
|
115 | - * |
|
116 | - * Newlines are replaced by the visible string '\n'. |
|
117 | - * Contents of arrays and objects (if any) are replaced by '...'. |
|
118 | - * |
|
119 | - * @return string |
|
120 | - * |
|
121 | - * @see SebastianBergmann\Exporter\Exporter::export |
|
122 | - */ |
|
123 | - public function shortenedExport($value) |
|
124 | - { |
|
125 | - if (is_string($value)) { |
|
126 | - $string = str_replace("\n", '', $this->export($value)); |
|
127 | - |
|
128 | - if (function_exists('mb_strlen')) { |
|
129 | - if (mb_strlen($string) > 40) { |
|
130 | - $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7); |
|
131 | - } |
|
132 | - } else { |
|
133 | - if (strlen($string) > 40) { |
|
134 | - $string = substr($string, 0, 30) . '...' . substr($string, -7); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return $string; |
|
139 | - } |
|
140 | - |
|
141 | - if (is_object($value)) { |
|
142 | - return sprintf( |
|
143 | - '%s Object (%s)', |
|
144 | - get_class($value), |
|
145 | - count($this->toArray($value)) > 0 ? '...' : '' |
|
146 | - ); |
|
147 | - } |
|
148 | - |
|
149 | - if (is_array($value)) { |
|
150 | - return sprintf( |
|
151 | - 'Array (%s)', |
|
152 | - count($value) > 0 ? '...' : '' |
|
153 | - ); |
|
154 | - } |
|
155 | - |
|
156 | - return $this->export($value); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Converts an object to an array containing all of its private, protected |
|
161 | - * and public properties. |
|
162 | - * |
|
163 | - * @return array |
|
164 | - */ |
|
165 | - public function toArray($value) |
|
166 | - { |
|
167 | - if (!is_object($value)) { |
|
168 | - return (array) $value; |
|
169 | - } |
|
170 | - |
|
171 | - $array = []; |
|
172 | - |
|
173 | - foreach ((array) $value as $key => $val) { |
|
174 | - // Exception traces commonly reference hundreds to thousands of |
|
175 | - // objects currently loaded in memory. Including them in the result |
|
176 | - // has a severe negative performance impact. |
|
177 | - if ("\0Error\0trace" === $key || "\0Exception\0trace" === $key) { |
|
178 | - continue; |
|
179 | - } |
|
180 | - |
|
181 | - // properties are transformed to keys in the following way: |
|
182 | - // private $property => "\0Classname\0property" |
|
183 | - // protected $property => "\0*\0property" |
|
184 | - // public $property => "property" |
|
185 | - if (preg_match('/^\0.+\0(.+)$/', (string) $key, $matches)) { |
|
186 | - $key = $matches[1]; |
|
187 | - } |
|
188 | - |
|
189 | - // See https://github.com/php/php-src/commit/5721132 |
|
190 | - if ($key === "\0gcdata") { |
|
191 | - continue; |
|
192 | - } |
|
193 | - |
|
194 | - $array[$key] = $val; |
|
195 | - } |
|
196 | - |
|
197 | - // Some internal classes like SplObjectStorage don't work with the |
|
198 | - // above (fast) mechanism nor with reflection in Zend. |
|
199 | - // Format the output similarly to print_r() in this case |
|
200 | - if ($value instanceof SplObjectStorage) { |
|
201 | - foreach ($value as $key => $val) { |
|
202 | - $array[spl_object_hash($val)] = [ |
|
203 | - 'obj' => $val, |
|
204 | - 'inf' => $value->getInfo(), |
|
205 | - ]; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - return $array; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Recursive implementation of export. |
|
214 | - * |
|
215 | - * @param mixed $value The value to export |
|
216 | - * @param int $indentation The indentation level of the 2nd+ line |
|
217 | - * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects |
|
218 | - * |
|
219 | - * @return string |
|
220 | - * |
|
221 | - * @see SebastianBergmann\Exporter\Exporter::export |
|
222 | - */ |
|
223 | - protected function recursiveExport(&$value, $indentation, $processed = null) |
|
224 | - { |
|
225 | - if ($value === null) { |
|
226 | - return 'null'; |
|
227 | - } |
|
228 | - |
|
229 | - if ($value === true) { |
|
230 | - return 'true'; |
|
231 | - } |
|
232 | - |
|
233 | - if ($value === false) { |
|
234 | - return 'false'; |
|
235 | - } |
|
236 | - |
|
237 | - if (is_float($value)) { |
|
238 | - $precisionBackup = ini_get('precision'); |
|
239 | - |
|
240 | - ini_set('precision', '-1'); |
|
241 | - |
|
242 | - try { |
|
243 | - $valueStr = (string) $value; |
|
244 | - |
|
245 | - if ((string) (int) $value === $valueStr) { |
|
246 | - return $valueStr . '.0'; |
|
247 | - } |
|
248 | - |
|
249 | - return $valueStr; |
|
250 | - } finally { |
|
251 | - ini_set('precision', $precisionBackup); |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - if (gettype($value) === 'resource (closed)') { |
|
256 | - return 'resource (closed)'; |
|
257 | - } |
|
258 | - |
|
259 | - if (is_resource($value)) { |
|
260 | - return sprintf( |
|
261 | - 'resource(%d) of type (%s)', |
|
262 | - $value, |
|
263 | - get_resource_type($value) |
|
264 | - ); |
|
265 | - } |
|
266 | - |
|
267 | - if (is_string($value)) { |
|
268 | - // Match for most non printable chars somewhat taking multibyte chars into account |
|
269 | - if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) { |
|
270 | - return 'Binary String: 0x' . bin2hex($value); |
|
271 | - } |
|
272 | - |
|
273 | - return "'" . |
|
274 | - str_replace( |
|
275 | - '<lf>', |
|
276 | - "\n", |
|
277 | - str_replace( |
|
278 | - ["\r\n", "\n\r", "\r", "\n"], |
|
279 | - ['\r\n<lf>', '\n\r<lf>', '\r<lf>', '\n<lf>'], |
|
280 | - $value |
|
281 | - ) |
|
282 | - ) . |
|
283 | - "'"; |
|
284 | - } |
|
285 | - |
|
286 | - $whitespace = str_repeat(' ', 4 * $indentation); |
|
287 | - |
|
288 | - if (!$processed) { |
|
289 | - $processed = new Context; |
|
290 | - } |
|
291 | - |
|
292 | - if (is_array($value)) { |
|
293 | - if (($key = $processed->contains($value)) !== false) { |
|
294 | - return 'Array &' . $key; |
|
295 | - } |
|
296 | - |
|
297 | - $array = $value; |
|
298 | - $key = $processed->add($value); |
|
299 | - $values = ''; |
|
300 | - |
|
301 | - if (count($array) > 0) { |
|
302 | - foreach ($array as $k => $v) { |
|
303 | - $values .= sprintf( |
|
304 | - '%s %s => %s' . "\n", |
|
305 | - $whitespace, |
|
306 | - $this->recursiveExport($k, $indentation), |
|
307 | - $this->recursiveExport($value[$k], $indentation + 1, $processed) |
|
308 | - ); |
|
309 | - } |
|
310 | - |
|
311 | - $values = "\n" . $values . $whitespace; |
|
312 | - } |
|
313 | - |
|
314 | - return sprintf('Array &%s (%s)', $key, $values); |
|
315 | - } |
|
316 | - |
|
317 | - if (is_object($value)) { |
|
318 | - $class = get_class($value); |
|
319 | - |
|
320 | - if ($hash = $processed->contains($value)) { |
|
321 | - return sprintf('%s Object &%s', $class, $hash); |
|
322 | - } |
|
323 | - |
|
324 | - $hash = $processed->add($value); |
|
325 | - $values = ''; |
|
326 | - $array = $this->toArray($value); |
|
327 | - |
|
328 | - if (count($array) > 0) { |
|
329 | - foreach ($array as $k => $v) { |
|
330 | - $values .= sprintf( |
|
331 | - '%s %s => %s' . "\n", |
|
332 | - $whitespace, |
|
333 | - $this->recursiveExport($k, $indentation), |
|
334 | - $this->recursiveExport($v, $indentation + 1, $processed) |
|
335 | - ); |
|
336 | - } |
|
337 | - |
|
338 | - $values = "\n" . $values . $whitespace; |
|
339 | - } |
|
340 | - |
|
341 | - return sprintf('%s Object &%s (%s)', $class, $hash, $values); |
|
342 | - } |
|
343 | - |
|
344 | - return var_export($value, true); |
|
345 | - } |
|
52 | + /** |
|
53 | + * Exports a value as a string. |
|
54 | + * |
|
55 | + * The output of this method is similar to the output of print_r(), but |
|
56 | + * improved in various aspects: |
|
57 | + * |
|
58 | + * - NULL is rendered as "null" (instead of "") |
|
59 | + * - TRUE is rendered as "true" (instead of "1") |
|
60 | + * - FALSE is rendered as "false" (instead of "") |
|
61 | + * - Strings are always quoted with single quotes |
|
62 | + * - Carriage returns and newlines are normalized to \n |
|
63 | + * - Recursion and repeated rendering is treated properly |
|
64 | + * |
|
65 | + * @param int $indentation The indentation level of the 2nd+ line |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function export($value, $indentation = 0) |
|
70 | + { |
|
71 | + return $this->recursiveExport($value, $indentation); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param array<mixed> $data |
|
76 | + * @param Context $context |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function shortenedRecursiveExport(&$data, Context $context = null) |
|
81 | + { |
|
82 | + $result = []; |
|
83 | + $exporter = new self(); |
|
84 | + |
|
85 | + if (!$context) { |
|
86 | + $context = new Context; |
|
87 | + } |
|
88 | + |
|
89 | + $array = $data; |
|
90 | + $context->add($data); |
|
91 | + |
|
92 | + foreach ($array as $key => $value) { |
|
93 | + if (is_array($value)) { |
|
94 | + if ($context->contains($data[$key]) !== false) { |
|
95 | + $result[] = '*RECURSION*'; |
|
96 | + } else { |
|
97 | + $result[] = sprintf( |
|
98 | + 'array(%s)', |
|
99 | + $this->shortenedRecursiveExport($data[$key], $context) |
|
100 | + ); |
|
101 | + } |
|
102 | + } else { |
|
103 | + $result[] = $exporter->shortenedExport($value); |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + return implode(', ', $result); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Exports a value into a single-line string. |
|
112 | + * |
|
113 | + * The output of this method is similar to the output of |
|
114 | + * SebastianBergmann\Exporter\Exporter::export(). |
|
115 | + * |
|
116 | + * Newlines are replaced by the visible string '\n'. |
|
117 | + * Contents of arrays and objects (if any) are replaced by '...'. |
|
118 | + * |
|
119 | + * @return string |
|
120 | + * |
|
121 | + * @see SebastianBergmann\Exporter\Exporter::export |
|
122 | + */ |
|
123 | + public function shortenedExport($value) |
|
124 | + { |
|
125 | + if (is_string($value)) { |
|
126 | + $string = str_replace("\n", '', $this->export($value)); |
|
127 | + |
|
128 | + if (function_exists('mb_strlen')) { |
|
129 | + if (mb_strlen($string) > 40) { |
|
130 | + $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7); |
|
131 | + } |
|
132 | + } else { |
|
133 | + if (strlen($string) > 40) { |
|
134 | + $string = substr($string, 0, 30) . '...' . substr($string, -7); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return $string; |
|
139 | + } |
|
140 | + |
|
141 | + if (is_object($value)) { |
|
142 | + return sprintf( |
|
143 | + '%s Object (%s)', |
|
144 | + get_class($value), |
|
145 | + count($this->toArray($value)) > 0 ? '...' : '' |
|
146 | + ); |
|
147 | + } |
|
148 | + |
|
149 | + if (is_array($value)) { |
|
150 | + return sprintf( |
|
151 | + 'Array (%s)', |
|
152 | + count($value) > 0 ? '...' : '' |
|
153 | + ); |
|
154 | + } |
|
155 | + |
|
156 | + return $this->export($value); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Converts an object to an array containing all of its private, protected |
|
161 | + * and public properties. |
|
162 | + * |
|
163 | + * @return array |
|
164 | + */ |
|
165 | + public function toArray($value) |
|
166 | + { |
|
167 | + if (!is_object($value)) { |
|
168 | + return (array) $value; |
|
169 | + } |
|
170 | + |
|
171 | + $array = []; |
|
172 | + |
|
173 | + foreach ((array) $value as $key => $val) { |
|
174 | + // Exception traces commonly reference hundreds to thousands of |
|
175 | + // objects currently loaded in memory. Including them in the result |
|
176 | + // has a severe negative performance impact. |
|
177 | + if ("\0Error\0trace" === $key || "\0Exception\0trace" === $key) { |
|
178 | + continue; |
|
179 | + } |
|
180 | + |
|
181 | + // properties are transformed to keys in the following way: |
|
182 | + // private $property => "\0Classname\0property" |
|
183 | + // protected $property => "\0*\0property" |
|
184 | + // public $property => "property" |
|
185 | + if (preg_match('/^\0.+\0(.+)$/', (string) $key, $matches)) { |
|
186 | + $key = $matches[1]; |
|
187 | + } |
|
188 | + |
|
189 | + // See https://github.com/php/php-src/commit/5721132 |
|
190 | + if ($key === "\0gcdata") { |
|
191 | + continue; |
|
192 | + } |
|
193 | + |
|
194 | + $array[$key] = $val; |
|
195 | + } |
|
196 | + |
|
197 | + // Some internal classes like SplObjectStorage don't work with the |
|
198 | + // above (fast) mechanism nor with reflection in Zend. |
|
199 | + // Format the output similarly to print_r() in this case |
|
200 | + if ($value instanceof SplObjectStorage) { |
|
201 | + foreach ($value as $key => $val) { |
|
202 | + $array[spl_object_hash($val)] = [ |
|
203 | + 'obj' => $val, |
|
204 | + 'inf' => $value->getInfo(), |
|
205 | + ]; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + return $array; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Recursive implementation of export. |
|
214 | + * |
|
215 | + * @param mixed $value The value to export |
|
216 | + * @param int $indentation The indentation level of the 2nd+ line |
|
217 | + * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects |
|
218 | + * |
|
219 | + * @return string |
|
220 | + * |
|
221 | + * @see SebastianBergmann\Exporter\Exporter::export |
|
222 | + */ |
|
223 | + protected function recursiveExport(&$value, $indentation, $processed = null) |
|
224 | + { |
|
225 | + if ($value === null) { |
|
226 | + return 'null'; |
|
227 | + } |
|
228 | + |
|
229 | + if ($value === true) { |
|
230 | + return 'true'; |
|
231 | + } |
|
232 | + |
|
233 | + if ($value === false) { |
|
234 | + return 'false'; |
|
235 | + } |
|
236 | + |
|
237 | + if (is_float($value)) { |
|
238 | + $precisionBackup = ini_get('precision'); |
|
239 | + |
|
240 | + ini_set('precision', '-1'); |
|
241 | + |
|
242 | + try { |
|
243 | + $valueStr = (string) $value; |
|
244 | + |
|
245 | + if ((string) (int) $value === $valueStr) { |
|
246 | + return $valueStr . '.0'; |
|
247 | + } |
|
248 | + |
|
249 | + return $valueStr; |
|
250 | + } finally { |
|
251 | + ini_set('precision', $precisionBackup); |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + if (gettype($value) === 'resource (closed)') { |
|
256 | + return 'resource (closed)'; |
|
257 | + } |
|
258 | + |
|
259 | + if (is_resource($value)) { |
|
260 | + return sprintf( |
|
261 | + 'resource(%d) of type (%s)', |
|
262 | + $value, |
|
263 | + get_resource_type($value) |
|
264 | + ); |
|
265 | + } |
|
266 | + |
|
267 | + if (is_string($value)) { |
|
268 | + // Match for most non printable chars somewhat taking multibyte chars into account |
|
269 | + if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) { |
|
270 | + return 'Binary String: 0x' . bin2hex($value); |
|
271 | + } |
|
272 | + |
|
273 | + return "'" . |
|
274 | + str_replace( |
|
275 | + '<lf>', |
|
276 | + "\n", |
|
277 | + str_replace( |
|
278 | + ["\r\n", "\n\r", "\r", "\n"], |
|
279 | + ['\r\n<lf>', '\n\r<lf>', '\r<lf>', '\n<lf>'], |
|
280 | + $value |
|
281 | + ) |
|
282 | + ) . |
|
283 | + "'"; |
|
284 | + } |
|
285 | + |
|
286 | + $whitespace = str_repeat(' ', 4 * $indentation); |
|
287 | + |
|
288 | + if (!$processed) { |
|
289 | + $processed = new Context; |
|
290 | + } |
|
291 | + |
|
292 | + if (is_array($value)) { |
|
293 | + if (($key = $processed->contains($value)) !== false) { |
|
294 | + return 'Array &' . $key; |
|
295 | + } |
|
296 | + |
|
297 | + $array = $value; |
|
298 | + $key = $processed->add($value); |
|
299 | + $values = ''; |
|
300 | + |
|
301 | + if (count($array) > 0) { |
|
302 | + foreach ($array as $k => $v) { |
|
303 | + $values .= sprintf( |
|
304 | + '%s %s => %s' . "\n", |
|
305 | + $whitespace, |
|
306 | + $this->recursiveExport($k, $indentation), |
|
307 | + $this->recursiveExport($value[$k], $indentation + 1, $processed) |
|
308 | + ); |
|
309 | + } |
|
310 | + |
|
311 | + $values = "\n" . $values . $whitespace; |
|
312 | + } |
|
313 | + |
|
314 | + return sprintf('Array &%s (%s)', $key, $values); |
|
315 | + } |
|
316 | + |
|
317 | + if (is_object($value)) { |
|
318 | + $class = get_class($value); |
|
319 | + |
|
320 | + if ($hash = $processed->contains($value)) { |
|
321 | + return sprintf('%s Object &%s', $class, $hash); |
|
322 | + } |
|
323 | + |
|
324 | + $hash = $processed->add($value); |
|
325 | + $values = ''; |
|
326 | + $array = $this->toArray($value); |
|
327 | + |
|
328 | + if (count($array) > 0) { |
|
329 | + foreach ($array as $k => $v) { |
|
330 | + $values .= sprintf( |
|
331 | + '%s %s => %s' . "\n", |
|
332 | + $whitespace, |
|
333 | + $this->recursiveExport($k, $indentation), |
|
334 | + $this->recursiveExport($v, $indentation + 1, $processed) |
|
335 | + ); |
|
336 | + } |
|
337 | + |
|
338 | + $values = "\n" . $values . $whitespace; |
|
339 | + } |
|
340 | + |
|
341 | + return sprintf('%s Object &%s (%s)', $class, $hash, $values); |
|
342 | + } |
|
343 | + |
|
344 | + return var_export($value, true); |
|
345 | + } |
|
346 | 346 | } |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $result = []; |
83 | 83 | $exporter = new self(); |
84 | 84 | |
85 | - if (!$context) { |
|
85 | + if ( ! $context) { |
|
86 | 86 | $context = new Context; |
87 | 87 | } |
88 | 88 | |
@@ -127,11 +127,11 @@ discard block |
||
127 | 127 | |
128 | 128 | if (function_exists('mb_strlen')) { |
129 | 129 | if (mb_strlen($string) > 40) { |
130 | - $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7); |
|
130 | + $string = mb_substr($string, 0, 30).'...'.mb_substr($string, -7); |
|
131 | 131 | } |
132 | 132 | } else { |
133 | 133 | if (strlen($string) > 40) { |
134 | - $string = substr($string, 0, 30) . '...' . substr($string, -7); |
|
134 | + $string = substr($string, 0, 30).'...'.substr($string, -7); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | public function toArray($value) |
166 | 166 | { |
167 | - if (!is_object($value)) { |
|
167 | + if ( ! is_object($value)) { |
|
168 | 168 | return (array) $value; |
169 | 169 | } |
170 | 170 | |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | $valueStr = (string) $value; |
244 | 244 | |
245 | 245 | if ((string) (int) $value === $valueStr) { |
246 | - return $valueStr . '.0'; |
|
246 | + return $valueStr.'.0'; |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | return $valueStr; |
@@ -267,10 +267,10 @@ discard block |
||
267 | 267 | if (is_string($value)) { |
268 | 268 | // Match for most non printable chars somewhat taking multibyte chars into account |
269 | 269 | if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) { |
270 | - return 'Binary String: 0x' . bin2hex($value); |
|
270 | + return 'Binary String: 0x'.bin2hex($value); |
|
271 | 271 | } |
272 | 272 | |
273 | - return "'" . |
|
273 | + return "'". |
|
274 | 274 | str_replace( |
275 | 275 | '<lf>', |
276 | 276 | "\n", |
@@ -279,19 +279,19 @@ discard block |
||
279 | 279 | ['\r\n<lf>', '\n\r<lf>', '\r<lf>', '\n<lf>'], |
280 | 280 | $value |
281 | 281 | ) |
282 | - ) . |
|
282 | + ). |
|
283 | 283 | "'"; |
284 | 284 | } |
285 | 285 | |
286 | 286 | $whitespace = str_repeat(' ', 4 * $indentation); |
287 | 287 | |
288 | - if (!$processed) { |
|
288 | + if ( ! $processed) { |
|
289 | 289 | $processed = new Context; |
290 | 290 | } |
291 | 291 | |
292 | 292 | if (is_array($value)) { |
293 | 293 | if (($key = $processed->contains($value)) !== false) { |
294 | - return 'Array &' . $key; |
|
294 | + return 'Array &'.$key; |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | $array = $value; |
@@ -301,14 +301,14 @@ discard block |
||
301 | 301 | if (count($array) > 0) { |
302 | 302 | foreach ($array as $k => $v) { |
303 | 303 | $values .= sprintf( |
304 | - '%s %s => %s' . "\n", |
|
304 | + '%s %s => %s'."\n", |
|
305 | 305 | $whitespace, |
306 | 306 | $this->recursiveExport($k, $indentation), |
307 | 307 | $this->recursiveExport($value[$k], $indentation + 1, $processed) |
308 | 308 | ); |
309 | 309 | } |
310 | 310 | |
311 | - $values = "\n" . $values . $whitespace; |
|
311 | + $values = "\n".$values.$whitespace; |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | return sprintf('Array &%s (%s)', $key, $values); |
@@ -328,14 +328,14 @@ discard block |
||
328 | 328 | if (count($array) > 0) { |
329 | 329 | foreach ($array as $k => $v) { |
330 | 330 | $values .= sprintf( |
331 | - '%s %s => %s' . "\n", |
|
331 | + '%s %s => %s'."\n", |
|
332 | 332 | $whitespace, |
333 | 333 | $this->recursiveExport($k, $indentation), |
334 | 334 | $this->recursiveExport($v, $indentation + 1, $processed) |
335 | 335 | ); |
336 | 336 | } |
337 | 337 | |
338 | - $values = "\n" . $values . $whitespace; |
|
338 | + $values = "\n".$values.$whitespace; |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | return sprintf('%s Object &%s (%s)', $class, $hash, $values); |
@@ -22,63 +22,63 @@ |
||
22 | 22 | */ |
23 | 23 | class NumericComparator extends ScalarComparator |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns whether the comparator can compare two values. |
|
27 | - * |
|
28 | - * @param mixed $expected The first value to compare |
|
29 | - * @param mixed $actual The second value to compare |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function accepts($expected, $actual) |
|
34 | - { |
|
35 | - // all numerical values, but not if both of them are strings |
|
36 | - return is_numeric($expected) && is_numeric($actual) && |
|
37 | - !(is_string($expected) && is_string($actual)); |
|
38 | - } |
|
25 | + /** |
|
26 | + * Returns whether the comparator can compare two values. |
|
27 | + * |
|
28 | + * @param mixed $expected The first value to compare |
|
29 | + * @param mixed $actual The second value to compare |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function accepts($expected, $actual) |
|
34 | + { |
|
35 | + // all numerical values, but not if both of them are strings |
|
36 | + return is_numeric($expected) && is_numeric($actual) && |
|
37 | + !(is_string($expected) && is_string($actual)); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Asserts that two values are equal. |
|
42 | - * |
|
43 | - * @param mixed $expected First value to compare |
|
44 | - * @param mixed $actual Second value to compare |
|
45 | - * @param float $delta Allowed numerical distance between two values to consider them equal |
|
46 | - * @param bool $canonicalize Arrays are sorted before comparison when set to true |
|
47 | - * @param bool $ignoreCase Case is ignored when set to true |
|
48 | - * |
|
49 | - * @throws ComparisonFailure |
|
50 | - */ |
|
51 | - public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/ |
|
52 | - { |
|
53 | - if ($this->isInfinite($actual) && $this->isInfinite($expected)) { |
|
54 | - return; |
|
55 | - } |
|
40 | + /** |
|
41 | + * Asserts that two values are equal. |
|
42 | + * |
|
43 | + * @param mixed $expected First value to compare |
|
44 | + * @param mixed $actual Second value to compare |
|
45 | + * @param float $delta Allowed numerical distance between two values to consider them equal |
|
46 | + * @param bool $canonicalize Arrays are sorted before comparison when set to true |
|
47 | + * @param bool $ignoreCase Case is ignored when set to true |
|
48 | + * |
|
49 | + * @throws ComparisonFailure |
|
50 | + */ |
|
51 | + public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/ |
|
52 | + { |
|
53 | + if ($this->isInfinite($actual) && $this->isInfinite($expected)) { |
|
54 | + return; |
|
55 | + } |
|
56 | 56 | |
57 | - if (($this->isInfinite($actual) xor $this->isInfinite($expected)) || |
|
58 | - ($this->isNan($actual) || $this->isNan($expected)) || |
|
59 | - abs($actual - $expected) > $delta) { |
|
60 | - throw new ComparisonFailure( |
|
61 | - $expected, |
|
62 | - $actual, |
|
63 | - '', |
|
64 | - '', |
|
65 | - false, |
|
66 | - sprintf( |
|
67 | - 'Failed asserting that %s matches expected %s.', |
|
68 | - $this->exporter->export($actual), |
|
69 | - $this->exporter->export($expected) |
|
70 | - ) |
|
71 | - ); |
|
72 | - } |
|
73 | - } |
|
57 | + if (($this->isInfinite($actual) xor $this->isInfinite($expected)) || |
|
58 | + ($this->isNan($actual) || $this->isNan($expected)) || |
|
59 | + abs($actual - $expected) > $delta) { |
|
60 | + throw new ComparisonFailure( |
|
61 | + $expected, |
|
62 | + $actual, |
|
63 | + '', |
|
64 | + '', |
|
65 | + false, |
|
66 | + sprintf( |
|
67 | + 'Failed asserting that %s matches expected %s.', |
|
68 | + $this->exporter->export($actual), |
|
69 | + $this->exporter->export($expected) |
|
70 | + ) |
|
71 | + ); |
|
72 | + } |
|
73 | + } |
|
74 | 74 | |
75 | - private function isInfinite($value): bool |
|
76 | - { |
|
77 | - return is_float($value) && is_infinite($value); |
|
78 | - } |
|
75 | + private function isInfinite($value): bool |
|
76 | + { |
|
77 | + return is_float($value) && is_infinite($value); |
|
78 | + } |
|
79 | 79 | |
80 | - private function isNan($value): bool |
|
81 | - { |
|
82 | - return is_float($value) && is_nan($value); |
|
83 | - } |
|
80 | + private function isNan($value): bool |
|
81 | + { |
|
82 | + return is_float($value) && is_nan($value); |
|
83 | + } |
|
84 | 84 | } |
@@ -34,7 +34,7 @@ |
||
34 | 34 | { |
35 | 35 | // all numerical values, but not if both of them are strings |
36 | 36 | return is_numeric($expected) && is_numeric($actual) && |
37 | - !(is_string($expected) && is_string($actual)); |
|
37 | + ! (is_string($expected) && is_string($actual)); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -16,126 +16,126 @@ |
||
16 | 16 | */ |
17 | 17 | class Factory |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Factory |
|
21 | - */ |
|
22 | - private static $instance; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var Comparator[] |
|
26 | - */ |
|
27 | - private $customComparators = []; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Comparator[] |
|
31 | - */ |
|
32 | - private $defaultComparators = []; |
|
33 | - |
|
34 | - /** |
|
35 | - * @return Factory |
|
36 | - */ |
|
37 | - public static function getInstance() |
|
38 | - { |
|
39 | - if (self::$instance === null) { |
|
40 | - self::$instance = new self; // @codeCoverageIgnore |
|
41 | - } |
|
42 | - |
|
43 | - return self::$instance; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Constructs a new factory. |
|
48 | - */ |
|
49 | - public function __construct() |
|
50 | - { |
|
51 | - $this->registerDefaultComparators(); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the correct comparator for comparing two values. |
|
56 | - * |
|
57 | - * @param mixed $expected The first value to compare |
|
58 | - * @param mixed $actual The second value to compare |
|
59 | - * |
|
60 | - * @return Comparator |
|
61 | - */ |
|
62 | - public function getComparatorFor($expected, $actual) |
|
63 | - { |
|
64 | - foreach ($this->customComparators as $comparator) { |
|
65 | - if ($comparator->accepts($expected, $actual)) { |
|
66 | - return $comparator; |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - foreach ($this->defaultComparators as $comparator) { |
|
71 | - if ($comparator->accepts($expected, $actual)) { |
|
72 | - return $comparator; |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - throw new RuntimeException('No suitable Comparator implementation found'); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Registers a new comparator. |
|
81 | - * |
|
82 | - * This comparator will be returned by getComparatorFor() if its accept() method |
|
83 | - * returns TRUE for the compared values. It has higher priority than the |
|
84 | - * existing comparators, meaning that its accept() method will be invoked |
|
85 | - * before those of the other comparators. |
|
86 | - * |
|
87 | - * @param Comparator $comparator The comparator to be registered |
|
88 | - */ |
|
89 | - public function register(Comparator $comparator)/*: void*/ |
|
90 | - { |
|
91 | - array_unshift($this->customComparators, $comparator); |
|
92 | - |
|
93 | - $comparator->setFactory($this); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Unregisters a comparator. |
|
98 | - * |
|
99 | - * This comparator will no longer be considered by getComparatorFor(). |
|
100 | - * |
|
101 | - * @param Comparator $comparator The comparator to be unregistered |
|
102 | - */ |
|
103 | - public function unregister(Comparator $comparator)/*: void*/ |
|
104 | - { |
|
105 | - foreach ($this->customComparators as $key => $_comparator) { |
|
106 | - if ($comparator === $_comparator) { |
|
107 | - unset($this->customComparators[$key]); |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Unregisters all non-default comparators. |
|
114 | - */ |
|
115 | - public function reset()/*: void*/ |
|
116 | - { |
|
117 | - $this->customComparators = []; |
|
118 | - } |
|
119 | - |
|
120 | - private function registerDefaultComparators(): void |
|
121 | - { |
|
122 | - $this->registerDefaultComparator(new MockObjectComparator); |
|
123 | - $this->registerDefaultComparator(new DateTimeComparator); |
|
124 | - $this->registerDefaultComparator(new DOMNodeComparator); |
|
125 | - $this->registerDefaultComparator(new SplObjectStorageComparator); |
|
126 | - $this->registerDefaultComparator(new ExceptionComparator); |
|
127 | - $this->registerDefaultComparator(new ObjectComparator); |
|
128 | - $this->registerDefaultComparator(new ResourceComparator); |
|
129 | - $this->registerDefaultComparator(new ArrayComparator); |
|
130 | - $this->registerDefaultComparator(new NumericComparator); |
|
131 | - $this->registerDefaultComparator(new ScalarComparator); |
|
132 | - $this->registerDefaultComparator(new TypeComparator); |
|
133 | - } |
|
134 | - |
|
135 | - private function registerDefaultComparator(Comparator $comparator): void |
|
136 | - { |
|
137 | - $this->defaultComparators[] = $comparator; |
|
138 | - |
|
139 | - $comparator->setFactory($this); |
|
140 | - } |
|
19 | + /** |
|
20 | + * @var Factory |
|
21 | + */ |
|
22 | + private static $instance; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var Comparator[] |
|
26 | + */ |
|
27 | + private $customComparators = []; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Comparator[] |
|
31 | + */ |
|
32 | + private $defaultComparators = []; |
|
33 | + |
|
34 | + /** |
|
35 | + * @return Factory |
|
36 | + */ |
|
37 | + public static function getInstance() |
|
38 | + { |
|
39 | + if (self::$instance === null) { |
|
40 | + self::$instance = new self; // @codeCoverageIgnore |
|
41 | + } |
|
42 | + |
|
43 | + return self::$instance; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Constructs a new factory. |
|
48 | + */ |
|
49 | + public function __construct() |
|
50 | + { |
|
51 | + $this->registerDefaultComparators(); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the correct comparator for comparing two values. |
|
56 | + * |
|
57 | + * @param mixed $expected The first value to compare |
|
58 | + * @param mixed $actual The second value to compare |
|
59 | + * |
|
60 | + * @return Comparator |
|
61 | + */ |
|
62 | + public function getComparatorFor($expected, $actual) |
|
63 | + { |
|
64 | + foreach ($this->customComparators as $comparator) { |
|
65 | + if ($comparator->accepts($expected, $actual)) { |
|
66 | + return $comparator; |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + foreach ($this->defaultComparators as $comparator) { |
|
71 | + if ($comparator->accepts($expected, $actual)) { |
|
72 | + return $comparator; |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + throw new RuntimeException('No suitable Comparator implementation found'); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Registers a new comparator. |
|
81 | + * |
|
82 | + * This comparator will be returned by getComparatorFor() if its accept() method |
|
83 | + * returns TRUE for the compared values. It has higher priority than the |
|
84 | + * existing comparators, meaning that its accept() method will be invoked |
|
85 | + * before those of the other comparators. |
|
86 | + * |
|
87 | + * @param Comparator $comparator The comparator to be registered |
|
88 | + */ |
|
89 | + public function register(Comparator $comparator)/*: void*/ |
|
90 | + { |
|
91 | + array_unshift($this->customComparators, $comparator); |
|
92 | + |
|
93 | + $comparator->setFactory($this); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Unregisters a comparator. |
|
98 | + * |
|
99 | + * This comparator will no longer be considered by getComparatorFor(). |
|
100 | + * |
|
101 | + * @param Comparator $comparator The comparator to be unregistered |
|
102 | + */ |
|
103 | + public function unregister(Comparator $comparator)/*: void*/ |
|
104 | + { |
|
105 | + foreach ($this->customComparators as $key => $_comparator) { |
|
106 | + if ($comparator === $_comparator) { |
|
107 | + unset($this->customComparators[$key]); |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Unregisters all non-default comparators. |
|
114 | + */ |
|
115 | + public function reset()/*: void*/ |
|
116 | + { |
|
117 | + $this->customComparators = []; |
|
118 | + } |
|
119 | + |
|
120 | + private function registerDefaultComparators(): void |
|
121 | + { |
|
122 | + $this->registerDefaultComparator(new MockObjectComparator); |
|
123 | + $this->registerDefaultComparator(new DateTimeComparator); |
|
124 | + $this->registerDefaultComparator(new DOMNodeComparator); |
|
125 | + $this->registerDefaultComparator(new SplObjectStorageComparator); |
|
126 | + $this->registerDefaultComparator(new ExceptionComparator); |
|
127 | + $this->registerDefaultComparator(new ObjectComparator); |
|
128 | + $this->registerDefaultComparator(new ResourceComparator); |
|
129 | + $this->registerDefaultComparator(new ArrayComparator); |
|
130 | + $this->registerDefaultComparator(new NumericComparator); |
|
131 | + $this->registerDefaultComparator(new ScalarComparator); |
|
132 | + $this->registerDefaultComparator(new TypeComparator); |
|
133 | + } |
|
134 | + |
|
135 | + private function registerDefaultComparator(Comparator $comparator): void |
|
136 | + { |
|
137 | + $this->defaultComparators[] = $comparator; |
|
138 | + |
|
139 | + $comparator->setFactory($this); |
|
140 | + } |
|
141 | 141 | } |
@@ -22,78 +22,78 @@ |
||
22 | 22 | */ |
23 | 23 | class ScalarComparator extends Comparator |
24 | 24 | { |
25 | - /** |
|
26 | - * Returns whether the comparator can compare two values. |
|
27 | - * |
|
28 | - * @param mixed $expected The first value to compare |
|
29 | - * @param mixed $actual The second value to compare |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - * |
|
33 | - * @since Method available since Release 3.6.0 |
|
34 | - */ |
|
35 | - public function accepts($expected, $actual) |
|
36 | - { |
|
37 | - return ((is_scalar($expected) xor null === $expected) && |
|
38 | - (is_scalar($actual) xor null === $actual)) |
|
39 | - // allow comparison between strings and objects featuring __toString() |
|
40 | - || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString')) |
|
41 | - || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual)); |
|
42 | - } |
|
25 | + /** |
|
26 | + * Returns whether the comparator can compare two values. |
|
27 | + * |
|
28 | + * @param mixed $expected The first value to compare |
|
29 | + * @param mixed $actual The second value to compare |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + * |
|
33 | + * @since Method available since Release 3.6.0 |
|
34 | + */ |
|
35 | + public function accepts($expected, $actual) |
|
36 | + { |
|
37 | + return ((is_scalar($expected) xor null === $expected) && |
|
38 | + (is_scalar($actual) xor null === $actual)) |
|
39 | + // allow comparison between strings and objects featuring __toString() |
|
40 | + || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString')) |
|
41 | + || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual)); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Asserts that two values are equal. |
|
46 | - * |
|
47 | - * @param mixed $expected First value to compare |
|
48 | - * @param mixed $actual Second value to compare |
|
49 | - * @param float $delta Allowed numerical distance between two values to consider them equal |
|
50 | - * @param bool $canonicalize Arrays are sorted before comparison when set to true |
|
51 | - * @param bool $ignoreCase Case is ignored when set to true |
|
52 | - * |
|
53 | - * @throws ComparisonFailure |
|
54 | - */ |
|
55 | - public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/ |
|
56 | - { |
|
57 | - $expectedToCompare = $expected; |
|
58 | - $actualToCompare = $actual; |
|
44 | + /** |
|
45 | + * Asserts that two values are equal. |
|
46 | + * |
|
47 | + * @param mixed $expected First value to compare |
|
48 | + * @param mixed $actual Second value to compare |
|
49 | + * @param float $delta Allowed numerical distance between two values to consider them equal |
|
50 | + * @param bool $canonicalize Arrays are sorted before comparison when set to true |
|
51 | + * @param bool $ignoreCase Case is ignored when set to true |
|
52 | + * |
|
53 | + * @throws ComparisonFailure |
|
54 | + */ |
|
55 | + public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/ |
|
56 | + { |
|
57 | + $expectedToCompare = $expected; |
|
58 | + $actualToCompare = $actual; |
|
59 | 59 | |
60 | - // always compare as strings to avoid strange behaviour |
|
61 | - // otherwise 0 == 'Foobar' |
|
62 | - if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) { |
|
63 | - $expectedToCompare = (string) $expectedToCompare; |
|
64 | - $actualToCompare = (string) $actualToCompare; |
|
60 | + // always compare as strings to avoid strange behaviour |
|
61 | + // otherwise 0 == 'Foobar' |
|
62 | + if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) { |
|
63 | + $expectedToCompare = (string) $expectedToCompare; |
|
64 | + $actualToCompare = (string) $actualToCompare; |
|
65 | 65 | |
66 | - if ($ignoreCase) { |
|
67 | - $expectedToCompare = strtolower($expectedToCompare); |
|
68 | - $actualToCompare = strtolower($actualToCompare); |
|
69 | - } |
|
70 | - } |
|
66 | + if ($ignoreCase) { |
|
67 | + $expectedToCompare = strtolower($expectedToCompare); |
|
68 | + $actualToCompare = strtolower($actualToCompare); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - if ($expectedToCompare !== $actualToCompare && is_string($expected) && is_string($actual)) { |
|
73 | - throw new ComparisonFailure( |
|
74 | - $expected, |
|
75 | - $actual, |
|
76 | - $this->exporter->export($expected), |
|
77 | - $this->exporter->export($actual), |
|
78 | - false, |
|
79 | - 'Failed asserting that two strings are equal.' |
|
80 | - ); |
|
81 | - } |
|
72 | + if ($expectedToCompare !== $actualToCompare && is_string($expected) && is_string($actual)) { |
|
73 | + throw new ComparisonFailure( |
|
74 | + $expected, |
|
75 | + $actual, |
|
76 | + $this->exporter->export($expected), |
|
77 | + $this->exporter->export($actual), |
|
78 | + false, |
|
79 | + 'Failed asserting that two strings are equal.' |
|
80 | + ); |
|
81 | + } |
|
82 | 82 | |
83 | - if ($expectedToCompare != $actualToCompare) { |
|
84 | - throw new ComparisonFailure( |
|
85 | - $expected, |
|
86 | - $actual, |
|
87 | - // no diff is required |
|
88 | - '', |
|
89 | - '', |
|
90 | - false, |
|
91 | - sprintf( |
|
92 | - 'Failed asserting that %s matches expected %s.', |
|
93 | - $this->exporter->export($actual), |
|
94 | - $this->exporter->export($expected) |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
98 | - } |
|
83 | + if ($expectedToCompare != $actualToCompare) { |
|
84 | + throw new ComparisonFailure( |
|
85 | + $expected, |
|
86 | + $actual, |
|
87 | + // no diff is required |
|
88 | + '', |
|
89 | + '', |
|
90 | + false, |
|
91 | + sprintf( |
|
92 | + 'Failed asserting that %s matches expected %s.', |
|
93 | + $this->exporter->export($actual), |
|
94 | + $this->exporter->export($expected) |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | + } |
|
99 | 99 | } |
@@ -59,7 +59,7 @@ |
||
59 | 59 | |
60 | 60 | // always compare as strings to avoid strange behaviour |
61 | 61 | // otherwise 0 == 'Foobar' |
62 | - if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) { |
|
62 | + if ((is_string($expected) && ! is_bool($actual)) || (is_string($actual) && ! is_bool($expected))) { |
|
63 | 63 | $expectedToCompare = (string) $expectedToCompare; |
64 | 64 | $actualToCompare = (string) $actualToCompare; |
65 | 65 |
@@ -11,32 +11,32 @@ |
||
11 | 11 | |
12 | 12 | final class Parameter |
13 | 13 | { |
14 | - /** |
|
15 | - * @psalm-var non-empty-string |
|
16 | - */ |
|
17 | - private $name; |
|
14 | + /** |
|
15 | + * @psalm-var non-empty-string |
|
16 | + */ |
|
17 | + private $name; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @var Type |
|
21 | - */ |
|
22 | - private $type; |
|
19 | + /** |
|
20 | + * @var Type |
|
21 | + */ |
|
22 | + private $type; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @psalm-param non-empty-string $name |
|
26 | - */ |
|
27 | - public function __construct(string $name, Type $type) |
|
28 | - { |
|
29 | - $this->name = $name; |
|
30 | - $this->type = $type; |
|
31 | - } |
|
24 | + /** |
|
25 | + * @psalm-param non-empty-string $name |
|
26 | + */ |
|
27 | + public function __construct(string $name, Type $type) |
|
28 | + { |
|
29 | + $this->name = $name; |
|
30 | + $this->type = $type; |
|
31 | + } |
|
32 | 32 | |
33 | - public function name(): string |
|
34 | - { |
|
35 | - return $this->name; |
|
36 | - } |
|
33 | + public function name(): string |
|
34 | + { |
|
35 | + return $this->name; |
|
36 | + } |
|
37 | 37 | |
38 | - public function type(): Type |
|
39 | - { |
|
40 | - return $this->type; |
|
41 | - } |
|
38 | + public function type(): Type |
|
39 | + { |
|
40 | + return $this->type; |
|
41 | + } |
|
42 | 42 | } |
@@ -19,166 +19,166 @@ |
||
19 | 19 | |
20 | 20 | final class ReflectionMapper |
21 | 21 | { |
22 | - /** |
|
23 | - * @psalm-return list<Parameter> |
|
24 | - */ |
|
25 | - public function fromParameterTypes(ReflectionFunctionAbstract $functionOrMethod): array |
|
26 | - { |
|
27 | - $parameters = []; |
|
28 | - |
|
29 | - foreach ($functionOrMethod->getParameters() as $parameter) { |
|
30 | - $name = $parameter->getName(); |
|
31 | - |
|
32 | - assert($name !== ''); |
|
33 | - |
|
34 | - if (!$parameter->hasType()) { |
|
35 | - $parameters[] = new Parameter($name, new UnknownType); |
|
36 | - |
|
37 | - continue; |
|
38 | - } |
|
39 | - |
|
40 | - $type = $parameter->getType(); |
|
41 | - |
|
42 | - if ($type instanceof ReflectionNamedType) { |
|
43 | - $parameters[] = new Parameter( |
|
44 | - $name, |
|
45 | - $this->mapNamedType($type, $functionOrMethod) |
|
46 | - ); |
|
47 | - |
|
48 | - continue; |
|
49 | - } |
|
50 | - |
|
51 | - if ($type instanceof ReflectionUnionType) { |
|
52 | - $parameters[] = new Parameter( |
|
53 | - $name, |
|
54 | - $this->mapUnionType($type, $functionOrMethod) |
|
55 | - ); |
|
56 | - |
|
57 | - continue; |
|
58 | - } |
|
59 | - |
|
60 | - if ($type instanceof ReflectionIntersectionType) { |
|
61 | - $parameters[] = new Parameter( |
|
62 | - $name, |
|
63 | - $this->mapIntersectionType($type, $functionOrMethod) |
|
64 | - ); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - return $parameters; |
|
69 | - } |
|
70 | - |
|
71 | - public function fromReturnType(ReflectionFunctionAbstract $functionOrMethod): Type |
|
72 | - { |
|
73 | - if (!$this->hasReturnType($functionOrMethod)) { |
|
74 | - return new UnknownType; |
|
75 | - } |
|
76 | - |
|
77 | - $returnType = $this->returnType($functionOrMethod); |
|
78 | - |
|
79 | - assert($returnType instanceof ReflectionNamedType || $returnType instanceof ReflectionUnionType || $returnType instanceof ReflectionIntersectionType); |
|
80 | - |
|
81 | - if ($returnType instanceof ReflectionNamedType) { |
|
82 | - return $this->mapNamedType($returnType, $functionOrMethod); |
|
83 | - } |
|
84 | - |
|
85 | - if ($returnType instanceof ReflectionUnionType) { |
|
86 | - return $this->mapUnionType($returnType, $functionOrMethod); |
|
87 | - } |
|
88 | - |
|
89 | - if ($returnType instanceof ReflectionIntersectionType) { |
|
90 | - return $this->mapIntersectionType($returnType, $functionOrMethod); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - private function mapNamedType(ReflectionNamedType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
95 | - { |
|
96 | - if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'self') { |
|
97 | - return ObjectType::fromName( |
|
98 | - $functionOrMethod->getDeclaringClass()->getName(), |
|
99 | - $type->allowsNull() |
|
100 | - ); |
|
101 | - } |
|
102 | - |
|
103 | - if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'static') { |
|
104 | - return new StaticType( |
|
105 | - TypeName::fromReflection($functionOrMethod->getDeclaringClass()), |
|
106 | - $type->allowsNull() |
|
107 | - ); |
|
108 | - } |
|
109 | - |
|
110 | - if ($type->getName() === 'mixed') { |
|
111 | - return new MixedType; |
|
112 | - } |
|
113 | - |
|
114 | - if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'parent') { |
|
115 | - return ObjectType::fromName( |
|
116 | - $functionOrMethod->getDeclaringClass()->getParentClass()->getName(), |
|
117 | - $type->allowsNull() |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - return Type::fromName( |
|
122 | - $type->getName(), |
|
123 | - $type->allowsNull() |
|
124 | - ); |
|
125 | - } |
|
126 | - |
|
127 | - private function mapUnionType(ReflectionUnionType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
128 | - { |
|
129 | - $types = []; |
|
130 | - |
|
131 | - foreach ($type->getTypes() as $_type) { |
|
132 | - assert($_type instanceof ReflectionNamedType || $_type instanceof ReflectionIntersectionType); |
|
133 | - |
|
134 | - if ($_type instanceof ReflectionNamedType) { |
|
135 | - $types[] = $this->mapNamedType($_type, $functionOrMethod); |
|
136 | - |
|
137 | - continue; |
|
138 | - } |
|
139 | - |
|
140 | - $types[] = $this->mapIntersectionType($_type, $functionOrMethod); |
|
141 | - } |
|
142 | - |
|
143 | - return new UnionType(...$types); |
|
144 | - } |
|
145 | - |
|
146 | - private function mapIntersectionType(ReflectionIntersectionType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
147 | - { |
|
148 | - $types = []; |
|
149 | - |
|
150 | - foreach ($type->getTypes() as $_type) { |
|
151 | - assert($_type instanceof ReflectionNamedType); |
|
152 | - |
|
153 | - $types[] = $this->mapNamedType($_type, $functionOrMethod); |
|
154 | - } |
|
155 | - |
|
156 | - return new IntersectionType(...$types); |
|
157 | - } |
|
158 | - |
|
159 | - private function hasReturnType(ReflectionFunctionAbstract $functionOrMethod): bool |
|
160 | - { |
|
161 | - if ($functionOrMethod->hasReturnType()) { |
|
162 | - return true; |
|
163 | - } |
|
164 | - |
|
165 | - if (!method_exists($functionOrMethod, 'hasTentativeReturnType')) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - return $functionOrMethod->hasTentativeReturnType(); |
|
170 | - } |
|
171 | - |
|
172 | - private function returnType(ReflectionFunctionAbstract $functionOrMethod): ?ReflectionType |
|
173 | - { |
|
174 | - if ($functionOrMethod->hasReturnType()) { |
|
175 | - return $functionOrMethod->getReturnType(); |
|
176 | - } |
|
177 | - |
|
178 | - if (!method_exists($functionOrMethod, 'getTentativeReturnType')) { |
|
179 | - return null; |
|
180 | - } |
|
181 | - |
|
182 | - return $functionOrMethod->getTentativeReturnType(); |
|
183 | - } |
|
22 | + /** |
|
23 | + * @psalm-return list<Parameter> |
|
24 | + */ |
|
25 | + public function fromParameterTypes(ReflectionFunctionAbstract $functionOrMethod): array |
|
26 | + { |
|
27 | + $parameters = []; |
|
28 | + |
|
29 | + foreach ($functionOrMethod->getParameters() as $parameter) { |
|
30 | + $name = $parameter->getName(); |
|
31 | + |
|
32 | + assert($name !== ''); |
|
33 | + |
|
34 | + if (!$parameter->hasType()) { |
|
35 | + $parameters[] = new Parameter($name, new UnknownType); |
|
36 | + |
|
37 | + continue; |
|
38 | + } |
|
39 | + |
|
40 | + $type = $parameter->getType(); |
|
41 | + |
|
42 | + if ($type instanceof ReflectionNamedType) { |
|
43 | + $parameters[] = new Parameter( |
|
44 | + $name, |
|
45 | + $this->mapNamedType($type, $functionOrMethod) |
|
46 | + ); |
|
47 | + |
|
48 | + continue; |
|
49 | + } |
|
50 | + |
|
51 | + if ($type instanceof ReflectionUnionType) { |
|
52 | + $parameters[] = new Parameter( |
|
53 | + $name, |
|
54 | + $this->mapUnionType($type, $functionOrMethod) |
|
55 | + ); |
|
56 | + |
|
57 | + continue; |
|
58 | + } |
|
59 | + |
|
60 | + if ($type instanceof ReflectionIntersectionType) { |
|
61 | + $parameters[] = new Parameter( |
|
62 | + $name, |
|
63 | + $this->mapIntersectionType($type, $functionOrMethod) |
|
64 | + ); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + return $parameters; |
|
69 | + } |
|
70 | + |
|
71 | + public function fromReturnType(ReflectionFunctionAbstract $functionOrMethod): Type |
|
72 | + { |
|
73 | + if (!$this->hasReturnType($functionOrMethod)) { |
|
74 | + return new UnknownType; |
|
75 | + } |
|
76 | + |
|
77 | + $returnType = $this->returnType($functionOrMethod); |
|
78 | + |
|
79 | + assert($returnType instanceof ReflectionNamedType || $returnType instanceof ReflectionUnionType || $returnType instanceof ReflectionIntersectionType); |
|
80 | + |
|
81 | + if ($returnType instanceof ReflectionNamedType) { |
|
82 | + return $this->mapNamedType($returnType, $functionOrMethod); |
|
83 | + } |
|
84 | + |
|
85 | + if ($returnType instanceof ReflectionUnionType) { |
|
86 | + return $this->mapUnionType($returnType, $functionOrMethod); |
|
87 | + } |
|
88 | + |
|
89 | + if ($returnType instanceof ReflectionIntersectionType) { |
|
90 | + return $this->mapIntersectionType($returnType, $functionOrMethod); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + private function mapNamedType(ReflectionNamedType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
95 | + { |
|
96 | + if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'self') { |
|
97 | + return ObjectType::fromName( |
|
98 | + $functionOrMethod->getDeclaringClass()->getName(), |
|
99 | + $type->allowsNull() |
|
100 | + ); |
|
101 | + } |
|
102 | + |
|
103 | + if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'static') { |
|
104 | + return new StaticType( |
|
105 | + TypeName::fromReflection($functionOrMethod->getDeclaringClass()), |
|
106 | + $type->allowsNull() |
|
107 | + ); |
|
108 | + } |
|
109 | + |
|
110 | + if ($type->getName() === 'mixed') { |
|
111 | + return new MixedType; |
|
112 | + } |
|
113 | + |
|
114 | + if ($functionOrMethod instanceof ReflectionMethod && $type->getName() === 'parent') { |
|
115 | + return ObjectType::fromName( |
|
116 | + $functionOrMethod->getDeclaringClass()->getParentClass()->getName(), |
|
117 | + $type->allowsNull() |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + return Type::fromName( |
|
122 | + $type->getName(), |
|
123 | + $type->allowsNull() |
|
124 | + ); |
|
125 | + } |
|
126 | + |
|
127 | + private function mapUnionType(ReflectionUnionType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
128 | + { |
|
129 | + $types = []; |
|
130 | + |
|
131 | + foreach ($type->getTypes() as $_type) { |
|
132 | + assert($_type instanceof ReflectionNamedType || $_type instanceof ReflectionIntersectionType); |
|
133 | + |
|
134 | + if ($_type instanceof ReflectionNamedType) { |
|
135 | + $types[] = $this->mapNamedType($_type, $functionOrMethod); |
|
136 | + |
|
137 | + continue; |
|
138 | + } |
|
139 | + |
|
140 | + $types[] = $this->mapIntersectionType($_type, $functionOrMethod); |
|
141 | + } |
|
142 | + |
|
143 | + return new UnionType(...$types); |
|
144 | + } |
|
145 | + |
|
146 | + private function mapIntersectionType(ReflectionIntersectionType $type, ReflectionFunctionAbstract $functionOrMethod): Type |
|
147 | + { |
|
148 | + $types = []; |
|
149 | + |
|
150 | + foreach ($type->getTypes() as $_type) { |
|
151 | + assert($_type instanceof ReflectionNamedType); |
|
152 | + |
|
153 | + $types[] = $this->mapNamedType($_type, $functionOrMethod); |
|
154 | + } |
|
155 | + |
|
156 | + return new IntersectionType(...$types); |
|
157 | + } |
|
158 | + |
|
159 | + private function hasReturnType(ReflectionFunctionAbstract $functionOrMethod): bool |
|
160 | + { |
|
161 | + if ($functionOrMethod->hasReturnType()) { |
|
162 | + return true; |
|
163 | + } |
|
164 | + |
|
165 | + if (!method_exists($functionOrMethod, 'hasTentativeReturnType')) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + return $functionOrMethod->hasTentativeReturnType(); |
|
170 | + } |
|
171 | + |
|
172 | + private function returnType(ReflectionFunctionAbstract $functionOrMethod): ?ReflectionType |
|
173 | + { |
|
174 | + if ($functionOrMethod->hasReturnType()) { |
|
175 | + return $functionOrMethod->getReturnType(); |
|
176 | + } |
|
177 | + |
|
178 | + if (!method_exists($functionOrMethod, 'getTentativeReturnType')) { |
|
179 | + return null; |
|
180 | + } |
|
181 | + |
|
182 | + return $functionOrMethod->getTentativeReturnType(); |
|
183 | + } |
|
184 | 184 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | |
32 | 32 | assert($name !== ''); |
33 | 33 | |
34 | - if (!$parameter->hasType()) { |
|
34 | + if ( ! $parameter->hasType()) { |
|
35 | 35 | $parameters[] = new Parameter($name, new UnknownType); |
36 | 36 | |
37 | 37 | continue; |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | |
71 | 71 | public function fromReturnType(ReflectionFunctionAbstract $functionOrMethod): Type |
72 | 72 | { |
73 | - if (!$this->hasReturnType($functionOrMethod)) { |
|
73 | + if ( ! $this->hasReturnType($functionOrMethod)) { |
|
74 | 74 | return new UnknownType; |
75 | 75 | } |
76 | 76 | |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | return true; |
163 | 163 | } |
164 | 164 | |
165 | - if (!method_exists($functionOrMethod, 'hasTentativeReturnType')) { |
|
165 | + if ( ! method_exists($functionOrMethod, 'hasTentativeReturnType')) { |
|
166 | 166 | return false; |
167 | 167 | } |
168 | 168 | |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | return $functionOrMethod->getReturnType(); |
176 | 176 | } |
177 | 177 | |
178 | - if (!method_exists($functionOrMethod, 'getTentativeReturnType')) { |
|
178 | + if ( ! method_exists($functionOrMethod, 'getTentativeReturnType')) { |
|
179 | 179 | return null; |
180 | 180 | } |
181 | 181 |
@@ -13,92 +13,92 @@ |
||
13 | 13 | |
14 | 14 | final class SimpleType extends Type |
15 | 15 | { |
16 | - /** |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - private $name; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var bool |
|
23 | - */ |
|
24 | - private $allowsNull; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var mixed |
|
28 | - */ |
|
29 | - private $value; |
|
30 | - |
|
31 | - public function __construct(string $name, bool $nullable, $value = null) |
|
32 | - { |
|
33 | - $this->name = $this->normalize($name); |
|
34 | - $this->allowsNull = $nullable; |
|
35 | - $this->value = $value; |
|
36 | - } |
|
37 | - |
|
38 | - public function isAssignable(Type $other): bool |
|
39 | - { |
|
40 | - if ($this->allowsNull && $other instanceof NullType) { |
|
41 | - return true; |
|
42 | - } |
|
43 | - |
|
44 | - if ($this->name === 'bool' && $other->name() === 'true') { |
|
45 | - return true; |
|
46 | - } |
|
47 | - |
|
48 | - if ($this->name === 'bool' && $other->name() === 'false') { |
|
49 | - return true; |
|
50 | - } |
|
51 | - |
|
52 | - if ($other instanceof self) { |
|
53 | - return $this->name === $other->name; |
|
54 | - } |
|
55 | - |
|
56 | - return false; |
|
57 | - } |
|
58 | - |
|
59 | - public function name(): string |
|
60 | - { |
|
61 | - return $this->name; |
|
62 | - } |
|
63 | - |
|
64 | - public function allowsNull(): bool |
|
65 | - { |
|
66 | - return $this->allowsNull; |
|
67 | - } |
|
68 | - |
|
69 | - public function value() |
|
70 | - { |
|
71 | - return $this->value; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @psalm-assert-if-true SimpleType $this |
|
76 | - */ |
|
77 | - public function isSimple(): bool |
|
78 | - { |
|
79 | - return true; |
|
80 | - } |
|
81 | - |
|
82 | - private function normalize(string $name): string |
|
83 | - { |
|
84 | - $name = strtolower($name); |
|
85 | - |
|
86 | - switch ($name) { |
|
87 | - case 'boolean': |
|
88 | - return 'bool'; |
|
89 | - |
|
90 | - case 'real': |
|
91 | - case 'double': |
|
92 | - return 'float'; |
|
93 | - |
|
94 | - case 'integer': |
|
95 | - return 'int'; |
|
96 | - |
|
97 | - case '[]': |
|
98 | - return 'array'; |
|
99 | - |
|
100 | - default: |
|
101 | - return $name; |
|
102 | - } |
|
103 | - } |
|
16 | + /** |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + private $name; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var bool |
|
23 | + */ |
|
24 | + private $allowsNull; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var mixed |
|
28 | + */ |
|
29 | + private $value; |
|
30 | + |
|
31 | + public function __construct(string $name, bool $nullable, $value = null) |
|
32 | + { |
|
33 | + $this->name = $this->normalize($name); |
|
34 | + $this->allowsNull = $nullable; |
|
35 | + $this->value = $value; |
|
36 | + } |
|
37 | + |
|
38 | + public function isAssignable(Type $other): bool |
|
39 | + { |
|
40 | + if ($this->allowsNull && $other instanceof NullType) { |
|
41 | + return true; |
|
42 | + } |
|
43 | + |
|
44 | + if ($this->name === 'bool' && $other->name() === 'true') { |
|
45 | + return true; |
|
46 | + } |
|
47 | + |
|
48 | + if ($this->name === 'bool' && $other->name() === 'false') { |
|
49 | + return true; |
|
50 | + } |
|
51 | + |
|
52 | + if ($other instanceof self) { |
|
53 | + return $this->name === $other->name; |
|
54 | + } |
|
55 | + |
|
56 | + return false; |
|
57 | + } |
|
58 | + |
|
59 | + public function name(): string |
|
60 | + { |
|
61 | + return $this->name; |
|
62 | + } |
|
63 | + |
|
64 | + public function allowsNull(): bool |
|
65 | + { |
|
66 | + return $this->allowsNull; |
|
67 | + } |
|
68 | + |
|
69 | + public function value() |
|
70 | + { |
|
71 | + return $this->value; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @psalm-assert-if-true SimpleType $this |
|
76 | + */ |
|
77 | + public function isSimple(): bool |
|
78 | + { |
|
79 | + return true; |
|
80 | + } |
|
81 | + |
|
82 | + private function normalize(string $name): string |
|
83 | + { |
|
84 | + $name = strtolower($name); |
|
85 | + |
|
86 | + switch ($name) { |
|
87 | + case 'boolean': |
|
88 | + return 'bool'; |
|
89 | + |
|
90 | + case 'real': |
|
91 | + case 'double': |
|
92 | + return 'float'; |
|
93 | + |
|
94 | + case 'integer': |
|
95 | + return 'int'; |
|
96 | + |
|
97 | + case '[]': |
|
98 | + return 'array'; |
|
99 | + |
|
100 | + default: |
|
101 | + return $name; |
|
102 | + } |
|
103 | + } |
|
104 | 104 | } |
@@ -17,210 +17,210 @@ |
||
17 | 17 | |
18 | 18 | abstract class Type |
19 | 19 | { |
20 | - public static function fromValue($value, bool $allowsNull): self |
|
21 | - { |
|
22 | - if ($allowsNull === false) { |
|
23 | - if ($value === true) { |
|
24 | - return new TrueType; |
|
25 | - } |
|
26 | - |
|
27 | - if ($value === false) { |
|
28 | - return new FalseType; |
|
29 | - } |
|
30 | - } |
|
31 | - |
|
32 | - $typeName = gettype($value); |
|
33 | - |
|
34 | - if ($typeName === 'object') { |
|
35 | - return new ObjectType(TypeName::fromQualifiedName(get_class($value)), $allowsNull); |
|
36 | - } |
|
37 | - |
|
38 | - $type = self::fromName($typeName, $allowsNull); |
|
39 | - |
|
40 | - if ($type instanceof SimpleType) { |
|
41 | - $type = new SimpleType($typeName, $allowsNull, $value); |
|
42 | - } |
|
43 | - |
|
44 | - return $type; |
|
45 | - } |
|
46 | - |
|
47 | - public static function fromName(string $typeName, bool $allowsNull): self |
|
48 | - { |
|
49 | - if (version_compare(PHP_VERSION, '8.1.0-dev', '>=') && strtolower($typeName) === 'never') { |
|
50 | - return new NeverType; |
|
51 | - } |
|
52 | - |
|
53 | - switch (strtolower($typeName)) { |
|
54 | - case 'callable': |
|
55 | - return new CallableType($allowsNull); |
|
56 | - |
|
57 | - case 'true': |
|
58 | - return new TrueType; |
|
59 | - |
|
60 | - case 'false': |
|
61 | - return new FalseType; |
|
62 | - |
|
63 | - case 'iterable': |
|
64 | - return new IterableType($allowsNull); |
|
65 | - |
|
66 | - case 'null': |
|
67 | - return new NullType; |
|
68 | - |
|
69 | - case 'object': |
|
70 | - return new GenericObjectType($allowsNull); |
|
71 | - |
|
72 | - case 'unknown type': |
|
73 | - return new UnknownType; |
|
74 | - |
|
75 | - case 'void': |
|
76 | - return new VoidType; |
|
77 | - |
|
78 | - case 'array': |
|
79 | - case 'bool': |
|
80 | - case 'boolean': |
|
81 | - case 'double': |
|
82 | - case 'float': |
|
83 | - case 'int': |
|
84 | - case 'integer': |
|
85 | - case 'real': |
|
86 | - case 'resource': |
|
87 | - case 'resource (closed)': |
|
88 | - case 'string': |
|
89 | - return new SimpleType($typeName, $allowsNull); |
|
90 | - |
|
91 | - default: |
|
92 | - return new ObjectType(TypeName::fromQualifiedName($typeName), $allowsNull); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - public function asString(): string |
|
97 | - { |
|
98 | - return ($this->allowsNull() ? '?' : '') . $this->name(); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @psalm-assert-if-true CallableType $this |
|
103 | - */ |
|
104 | - public function isCallable(): bool |
|
105 | - { |
|
106 | - return false; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @psalm-assert-if-true TrueType $this |
|
111 | - */ |
|
112 | - public function isTrue(): bool |
|
113 | - { |
|
114 | - return false; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @psalm-assert-if-true FalseType $this |
|
119 | - */ |
|
120 | - public function isFalse(): bool |
|
121 | - { |
|
122 | - return false; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @psalm-assert-if-true GenericObjectType $this |
|
127 | - */ |
|
128 | - public function isGenericObject(): bool |
|
129 | - { |
|
130 | - return false; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @psalm-assert-if-true IntersectionType $this |
|
135 | - */ |
|
136 | - public function isIntersection(): bool |
|
137 | - { |
|
138 | - return false; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @psalm-assert-if-true IterableType $this |
|
143 | - */ |
|
144 | - public function isIterable(): bool |
|
145 | - { |
|
146 | - return false; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @psalm-assert-if-true MixedType $this |
|
151 | - */ |
|
152 | - public function isMixed(): bool |
|
153 | - { |
|
154 | - return false; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @psalm-assert-if-true NeverType $this |
|
159 | - */ |
|
160 | - public function isNever(): bool |
|
161 | - { |
|
162 | - return false; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @psalm-assert-if-true NullType $this |
|
167 | - */ |
|
168 | - public function isNull(): bool |
|
169 | - { |
|
170 | - return false; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @psalm-assert-if-true ObjectType $this |
|
175 | - */ |
|
176 | - public function isObject(): bool |
|
177 | - { |
|
178 | - return false; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @psalm-assert-if-true SimpleType $this |
|
183 | - */ |
|
184 | - public function isSimple(): bool |
|
185 | - { |
|
186 | - return false; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @psalm-assert-if-true StaticType $this |
|
191 | - */ |
|
192 | - public function isStatic(): bool |
|
193 | - { |
|
194 | - return false; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @psalm-assert-if-true UnionType $this |
|
199 | - */ |
|
200 | - public function isUnion(): bool |
|
201 | - { |
|
202 | - return false; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @psalm-assert-if-true UnknownType $this |
|
207 | - */ |
|
208 | - public function isUnknown(): bool |
|
209 | - { |
|
210 | - return false; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @psalm-assert-if-true VoidType $this |
|
215 | - */ |
|
216 | - public function isVoid(): bool |
|
217 | - { |
|
218 | - return false; |
|
219 | - } |
|
220 | - |
|
221 | - abstract public function isAssignable(self $other): bool; |
|
222 | - |
|
223 | - abstract public function name(): string; |
|
224 | - |
|
225 | - abstract public function allowsNull(): bool; |
|
20 | + public static function fromValue($value, bool $allowsNull): self |
|
21 | + { |
|
22 | + if ($allowsNull === false) { |
|
23 | + if ($value === true) { |
|
24 | + return new TrueType; |
|
25 | + } |
|
26 | + |
|
27 | + if ($value === false) { |
|
28 | + return new FalseType; |
|
29 | + } |
|
30 | + } |
|
31 | + |
|
32 | + $typeName = gettype($value); |
|
33 | + |
|
34 | + if ($typeName === 'object') { |
|
35 | + return new ObjectType(TypeName::fromQualifiedName(get_class($value)), $allowsNull); |
|
36 | + } |
|
37 | + |
|
38 | + $type = self::fromName($typeName, $allowsNull); |
|
39 | + |
|
40 | + if ($type instanceof SimpleType) { |
|
41 | + $type = new SimpleType($typeName, $allowsNull, $value); |
|
42 | + } |
|
43 | + |
|
44 | + return $type; |
|
45 | + } |
|
46 | + |
|
47 | + public static function fromName(string $typeName, bool $allowsNull): self |
|
48 | + { |
|
49 | + if (version_compare(PHP_VERSION, '8.1.0-dev', '>=') && strtolower($typeName) === 'never') { |
|
50 | + return new NeverType; |
|
51 | + } |
|
52 | + |
|
53 | + switch (strtolower($typeName)) { |
|
54 | + case 'callable': |
|
55 | + return new CallableType($allowsNull); |
|
56 | + |
|
57 | + case 'true': |
|
58 | + return new TrueType; |
|
59 | + |
|
60 | + case 'false': |
|
61 | + return new FalseType; |
|
62 | + |
|
63 | + case 'iterable': |
|
64 | + return new IterableType($allowsNull); |
|
65 | + |
|
66 | + case 'null': |
|
67 | + return new NullType; |
|
68 | + |
|
69 | + case 'object': |
|
70 | + return new GenericObjectType($allowsNull); |
|
71 | + |
|
72 | + case 'unknown type': |
|
73 | + return new UnknownType; |
|
74 | + |
|
75 | + case 'void': |
|
76 | + return new VoidType; |
|
77 | + |
|
78 | + case 'array': |
|
79 | + case 'bool': |
|
80 | + case 'boolean': |
|
81 | + case 'double': |
|
82 | + case 'float': |
|
83 | + case 'int': |
|
84 | + case 'integer': |
|
85 | + case 'real': |
|
86 | + case 'resource': |
|
87 | + case 'resource (closed)': |
|
88 | + case 'string': |
|
89 | + return new SimpleType($typeName, $allowsNull); |
|
90 | + |
|
91 | + default: |
|
92 | + return new ObjectType(TypeName::fromQualifiedName($typeName), $allowsNull); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + public function asString(): string |
|
97 | + { |
|
98 | + return ($this->allowsNull() ? '?' : '') . $this->name(); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @psalm-assert-if-true CallableType $this |
|
103 | + */ |
|
104 | + public function isCallable(): bool |
|
105 | + { |
|
106 | + return false; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @psalm-assert-if-true TrueType $this |
|
111 | + */ |
|
112 | + public function isTrue(): bool |
|
113 | + { |
|
114 | + return false; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @psalm-assert-if-true FalseType $this |
|
119 | + */ |
|
120 | + public function isFalse(): bool |
|
121 | + { |
|
122 | + return false; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @psalm-assert-if-true GenericObjectType $this |
|
127 | + */ |
|
128 | + public function isGenericObject(): bool |
|
129 | + { |
|
130 | + return false; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @psalm-assert-if-true IntersectionType $this |
|
135 | + */ |
|
136 | + public function isIntersection(): bool |
|
137 | + { |
|
138 | + return false; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @psalm-assert-if-true IterableType $this |
|
143 | + */ |
|
144 | + public function isIterable(): bool |
|
145 | + { |
|
146 | + return false; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @psalm-assert-if-true MixedType $this |
|
151 | + */ |
|
152 | + public function isMixed(): bool |
|
153 | + { |
|
154 | + return false; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @psalm-assert-if-true NeverType $this |
|
159 | + */ |
|
160 | + public function isNever(): bool |
|
161 | + { |
|
162 | + return false; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @psalm-assert-if-true NullType $this |
|
167 | + */ |
|
168 | + public function isNull(): bool |
|
169 | + { |
|
170 | + return false; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @psalm-assert-if-true ObjectType $this |
|
175 | + */ |
|
176 | + public function isObject(): bool |
|
177 | + { |
|
178 | + return false; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @psalm-assert-if-true SimpleType $this |
|
183 | + */ |
|
184 | + public function isSimple(): bool |
|
185 | + { |
|
186 | + return false; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @psalm-assert-if-true StaticType $this |
|
191 | + */ |
|
192 | + public function isStatic(): bool |
|
193 | + { |
|
194 | + return false; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @psalm-assert-if-true UnionType $this |
|
199 | + */ |
|
200 | + public function isUnion(): bool |
|
201 | + { |
|
202 | + return false; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @psalm-assert-if-true UnknownType $this |
|
207 | + */ |
|
208 | + public function isUnknown(): bool |
|
209 | + { |
|
210 | + return false; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @psalm-assert-if-true VoidType $this |
|
215 | + */ |
|
216 | + public function isVoid(): bool |
|
217 | + { |
|
218 | + return false; |
|
219 | + } |
|
220 | + |
|
221 | + abstract public function isAssignable(self $other): bool; |
|
222 | + |
|
223 | + abstract public function name(): string; |
|
224 | + |
|
225 | + abstract public function allowsNull(): bool; |
|
226 | 226 | } |
@@ -95,7 +95,7 @@ |
||
95 | 95 | |
96 | 96 | public function asString(): string |
97 | 97 | { |
98 | - return ($this->allowsNull() ? '?' : '') . $this->name(); |
|
98 | + return ($this->allowsNull() ? '?' : '').$this->name(); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -14,61 +14,61 @@ |
||
14 | 14 | |
15 | 15 | final class ObjectType extends Type |
16 | 16 | { |
17 | - /** |
|
18 | - * @var TypeName |
|
19 | - */ |
|
20 | - private $className; |
|
17 | + /** |
|
18 | + * @var TypeName |
|
19 | + */ |
|
20 | + private $className; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @var bool |
|
24 | - */ |
|
25 | - private $allowsNull; |
|
22 | + /** |
|
23 | + * @var bool |
|
24 | + */ |
|
25 | + private $allowsNull; |
|
26 | 26 | |
27 | - public function __construct(TypeName $className, bool $allowsNull) |
|
28 | - { |
|
29 | - $this->className = $className; |
|
30 | - $this->allowsNull = $allowsNull; |
|
31 | - } |
|
27 | + public function __construct(TypeName $className, bool $allowsNull) |
|
28 | + { |
|
29 | + $this->className = $className; |
|
30 | + $this->allowsNull = $allowsNull; |
|
31 | + } |
|
32 | 32 | |
33 | - public function isAssignable(Type $other): bool |
|
34 | - { |
|
35 | - if ($this->allowsNull && $other instanceof NullType) { |
|
36 | - return true; |
|
37 | - } |
|
33 | + public function isAssignable(Type $other): bool |
|
34 | + { |
|
35 | + if ($this->allowsNull && $other instanceof NullType) { |
|
36 | + return true; |
|
37 | + } |
|
38 | 38 | |
39 | - if ($other instanceof self) { |
|
40 | - if (0 === strcasecmp($this->className->qualifiedName(), $other->className->qualifiedName())) { |
|
41 | - return true; |
|
42 | - } |
|
39 | + if ($other instanceof self) { |
|
40 | + if (0 === strcasecmp($this->className->qualifiedName(), $other->className->qualifiedName())) { |
|
41 | + return true; |
|
42 | + } |
|
43 | 43 | |
44 | - if (is_subclass_of($other->className->qualifiedName(), $this->className->qualifiedName(), true)) { |
|
45 | - return true; |
|
46 | - } |
|
47 | - } |
|
44 | + if (is_subclass_of($other->className->qualifiedName(), $this->className->qualifiedName(), true)) { |
|
45 | + return true; |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - return false; |
|
50 | - } |
|
49 | + return false; |
|
50 | + } |
|
51 | 51 | |
52 | - public function name(): string |
|
53 | - { |
|
54 | - return $this->className->qualifiedName(); |
|
55 | - } |
|
52 | + public function name(): string |
|
53 | + { |
|
54 | + return $this->className->qualifiedName(); |
|
55 | + } |
|
56 | 56 | |
57 | - public function allowsNull(): bool |
|
58 | - { |
|
59 | - return $this->allowsNull; |
|
60 | - } |
|
57 | + public function allowsNull(): bool |
|
58 | + { |
|
59 | + return $this->allowsNull; |
|
60 | + } |
|
61 | 61 | |
62 | - public function className(): TypeName |
|
63 | - { |
|
64 | - return $this->className; |
|
65 | - } |
|
62 | + public function className(): TypeName |
|
63 | + { |
|
64 | + return $this->className; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @psalm-assert-if-true ObjectType $this |
|
69 | - */ |
|
70 | - public function isObject(): bool |
|
71 | - { |
|
72 | - return true; |
|
73 | - } |
|
67 | + /** |
|
68 | + * @psalm-assert-if-true ObjectType $this |
|
69 | + */ |
|
70 | + public function isObject(): bool |
|
71 | + { |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | } |