Passed
Pull Request — master (#54)
by Alexander
02:17
created

VarDumper::getPropertyName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\VarDumper;
6
7
use __PHP_Incomplete_Class;
8
use Closure;
9
use Exception;
10
use IteratorAggregate;
11
use JsonSerializable;
12
use ReflectionObject;
13
use ReflectionException;
14
use Yiisoft\Arrays\ArrayableInterface;
15
16
use function array_keys;
17
use function count;
18
use function get_class;
19
use function gettype;
20
use function highlight_string;
21
use function method_exists;
22
use function next;
23
use function preg_replace;
24
use function range;
25
use function spl_object_id;
26
use function str_repeat;
27
use function strtr;
28
use function trim;
29
use function var_export;
30
31
/**
32
 * VarDumper provides enhanced versions of the PHP functions {@see var_dump()} and {@see var_export()}.
33
 * It can:
34
 *
35
 * - Correctly identify the recursively referenced objects in a complex object structure.
36
 * - Recursively control depth to avoid indefinite recursive display of some peculiar variables.
37
 * - Export closures and objects.
38
 * - Highlight output.
39
 * - Format output.
40
 */
41
final class VarDumper
42
{
43
    /**
44
     * @var mixed Variable to dump.
45
     */
46
    private $variable;
47
    private array $useVarInClosures = [];
48
    private bool $serializeObjects = true;
49
    private static ?ClosureExporter $closureExporter = null;
50
51
    /**
52
     * @param mixed $variable Variable to dump.
53
     */
54 83
    private function __construct($variable)
55
    {
56 83
        $this->variable = $variable;
57 83
    }
58
59
    /**
60
     * @param mixed $variable Variable to dump.
61
     *
62
     * @return static An instance containing variable to dump.
63
     */
64 83
    public static function create($variable): self
65
    {
66 83
        return new self($variable);
67
    }
68
69
    /**
70
     * Prints a variable.
71
     *
72
     * This method achieves the similar functionality as {@see var_dump()} and {@see print_r()}
73
     * but is more robust when handling complex objects.
74
     *
75
     * @param mixed $variable Variable to be dumped.
76
     * @param int $depth Maximum depth that the dumper should go into the variable. Defaults to 10.
77
     * @param bool $highlight Whether the result should be syntax-highlighted.
78
     *
79
     * @throws ReflectionException
80
     */
81 6
    public static function dump($variable, int $depth = 10, bool $highlight = true): void
82
    {
83 6
        echo self::create($variable)->asString($depth, $highlight);
84 6
    }
85
86
    /**
87
     * Dumps a variable in terms of a string.
88
     *
89
     * This method achieves the similar functionality as {@see var_dump()} and {@see print_r()}
90
     * but is more robust when handling complex objects.
91
     *
92
     * @param int $depth Maximum depth that the dumper should go into the variable. Defaults to 10.
93
     * @param bool $highlight Whether the result should be syntax-highlighted.
94
     *
95
     * @throws ReflectionException
96
     *
97
     * @return string The string representation of the variable.* @return string The string representation of the variable.
98
     */
99 32
    public function asString(int $depth = 10, bool $highlight = false): string
100
    {
101 32
        $output = $this->dumpInternal($this->variable, true, $depth, 0);
102
103 32
        if ($highlight) {
104 1
            $result = highlight_string("<?php\n" . $output, true);
105 1
            $output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
106
        }
107
108 32
        return $output;
109
    }
110
111
    /**
112
     * Exports a variable as a string containing PHP code.
113
     *
114
     * The string is a valid PHP expression that can be evaluated by PHP parser
115
     * and the evaluation result will give back the variable value.
116
     *
117
     * This method is similar to {@see var_export()}. The main difference is that
118
     * it generates more compact string representation using short array syntax.
119
     *
120
     * It also handles closures with {@see ClosureExporter} and objects
121
     * by using the PHP functions {@see serialize()} and {@see unserialize()}.
122
     *
123
     * @param bool $format Whatever to format code.
124
     * @param array $useVariables Array of variables used in `use` statement (['$params', '$config'])
125
     * @param bool $serializeObjects If it is true all objects will be serialized except objects with closure(s). If it
126
     * is false only objects of internal classes will be serialized.
127
     *
128
     * @throws ReflectionException
129
     *
130
     * @return string A PHP code representation of the variable.
131
     */
132 53
    public function export(bool $format = true, array $useVariables = [], bool $serializeObjects = true): string
133
    {
134 53
        $this->useVarInClosures = $useVariables;
135 53
        $this->serializeObjects = $serializeObjects;
136 53
        return $this->exportInternal($this->variable, $format, 0);
137
    }
138
139
    /**
140
     * @param mixed $var Variable to be dumped.
141
     * @param bool $format Whatever to format code.
142
     * @param int $depth Maximum depth.
143
     * @param int $level Current depth.
144
     *
145
     * @throws ReflectionException
146
     *
147
     * @return string
148
     */
149 32
    private function dumpInternal($var, bool $format, int $depth, int $level): string
150
    {
151 32
        switch (gettype($var)) {
152 32
            case 'resource':
153 31
            case 'resource (closed)':
154 1
                return '{resource}';
155 31
            case 'NULL':
156 1
                return 'null';
157 30
            case 'array':
158 6
                if ($depth <= $level) {
159 1
                    return '[...]';
160
                }
161
162 5
                if (empty($var)) {
163 2
                    return '[]';
164
                }
165
166 3
                $output = '';
167 3
                $keys = array_keys($var);
168 3
                $spaces = str_repeat(' ', $level * 4);
169 3
                $output .= '[';
170
171 3
                foreach ($keys as $name) {
172 3
                    if ($format) {
173 3
                        $output .= "\n" . $spaces . '    ';
174
                    }
175 3
                    $output .= $this->exportVariable($name);
176 3
                    $output .= ' => ';
177 3
                    $output .= $this->dumpInternal($var[$name], $format, $depth, $level + 1);
178
                }
179
180 3
                return $format
181 3
                    ? $output . "\n" . $spaces . ']'
182 3
                    : $output . ']';
183 28
            case 'object':
184 16
                if ($var instanceof Closure) {
185 11
                    return $this->exportClosure($var);
186
                }
187
188 7
                if ($depth <= $level) {
189 1
                    return $this->getObjectDescription($var) . ' (...)';
190
                }
191
192 6
                $spaces = str_repeat(' ', $level * 4);
193 6
                $output = $this->getObjectDescription($var) . "\n" . $spaces . '(';
194 6
                $objectProperties = $this->getObjectProperties($var);
195
196
                /** @psalm-var mixed $value */
197 6
                foreach ($objectProperties as $name => $value) {
198 4
                    $propertyName = strtr(trim((string) $name), "\0", '::');
199 4
                    $output .= "\n" . $spaces . "    [$propertyName] => ";
200 4
                    $output .= $this->dumpInternal($value, $format, $depth, $level + 1);
201
                }
202 6
                return $output . "\n" . $spaces . ')';
203
            default:
204 14
                return $this->exportVariable($var);
205
        }
206
    }
207
208
    /**
209
     * @param mixed $variable Variable to be exported.
210
     * @param bool $format Whatever to format code.
211
     * @param int $level Current depth.
212
     *
213
     * @throws ReflectionException
214
     *
215
     * @return string
216
     */
217 53
    private function exportInternal($variable, bool $format, int $level): string
218
    {
219 53
        $spaces = str_repeat(' ', $level * 4);
220 53
        switch (gettype($variable)) {
221 53
            case 'NULL':
222 2
                return 'null';
223 51
            case 'array':
224 9
                if (empty($variable)) {
225 2
                    return '[]';
226
                }
227
228 7
                $keys = array_keys($variable);
229 7
                $outputKeys = ($keys !== range(0, count($variable) - 1));
230 7
                $output = '[';
231
232 7
                foreach ($keys as $key) {
233 7
                    if ($format) {
234 4
                        $output .= "\n" . $spaces . '    ';
235
                    }
236 7
                    if ($outputKeys) {
237 3
                        $output .= $this->exportVariable($key);
238 3
                        $output .= ' => ';
239
                    }
240 7
                    $output .= $this->exportInternal($variable[$key], $format, $level + 1);
241 7
                    if ($format || next($keys) !== false) {
242 6
                        $output .= ',';
243
                    }
244
                }
245
246 7
                return $format
247 4
                    ? $output . "\n" . $spaces . ']'
248 7
                    : $output . ']';
249 49
            case 'object':
250 32
                if ($variable instanceof Closure) {
251 25
                    return $this->exportClosure($variable, $level);
252
                }
253
254 12
                $reflectionObject = new ReflectionObject($variable);
255
                try {
256 12
                    if ($this->serializeObjects || $reflectionObject->isInternal() || $reflectionObject->isAnonymous()) {
257 10
                        return "unserialize({$this->exportVariable(serialize($variable))})";
258
                    }
259
260 2
                    return $this->exportObject($variable, $reflectionObject, $format, $level);
261 6
                } catch (Exception $e) {
262
                    // Serialize may fail, for example: if object contains a `\Closure` instance so we use a fallback.
263 6
                    if ($this->serializeObjects && !$reflectionObject->isInternal() && !$reflectionObject->isAnonymous()) {
264
                        try {
265 4
                            return $this->exportObject($variable, $reflectionObject, $format, $level);
266
                        } catch (Exception $e) {
267
                            return $this->exportObjectFallback($variable, $format, $level);
268
                        }
269
                    }
270
271 2
                    return $this->exportObjectFallback($variable, $format, $level);
272
                }
273
            default:
274 19
                return $this->exportVariable($variable);
275
        }
276
    }
277
278 6
    private function getPropertyName(string $property): string
279
    {
280 6
        $property = str_replace("\0", '::', trim($property));
281
282 6
        if (strpos($property, '*::') === 0) {
283
            return substr($property, 3);
284
        }
285
286 6
        if (($pos = strpos($property, '::')) !== false) {
287 4
            return substr($property, $pos + 2);
288
        }
289
290 2
        return $property;
291
    }
292
293
    /**
294
     * @param object $variable
295
     * @param bool $format
296
     * @param int $level
297
     *
298
     * @throws ReflectionException
299
     *
300
     * @return string
301
     */
302 2
    private function exportObjectFallback(object $variable, bool $format, int $level): string
303
    {
304 2
        if ($variable instanceof ArrayableInterface) {
305
            return $this->exportInternal($variable->toArray(), $format, $level);
306
        }
307
308 2
        if ($variable instanceof JsonSerializable) {
309
            return $this->exportInternal($variable->jsonSerialize(), $format, $level);
310
        }
311
312 2
        if ($variable instanceof IteratorAggregate) {
313
            return $this->exportInternal(iterator_to_array($variable), $format, $level);
314
        }
315
316
        /** @psalm-suppress RedundantCondition */
317 2
        if ('__PHP_Incomplete_Class' !== get_class($variable) && method_exists($variable, '__toString')) {
318
            return $this->exportVariable($variable->__toString());
319
        }
320
321 2
        return $this->exportVariable(self::create($variable)->asString());
322
    }
323
324 6
    private function exportObject(object $variable, ReflectionObject $reflectionObject, bool $format, int $level): string
325
    {
326 6
        $spaces = str_repeat(' ', $level * 4);
327 6
        $objectProperties = $this->getObjectProperties($variable);
328 6
        $class = get_class($variable);
329 6
        $use = $this->useVarInClosures === [] ? '' : ' use (' . implode(', ', $this->useVarInClosures) . ')';
330 6
        $lines = ['(static function ()' . $use . ' {',];
331 6
        if ($reflectionObject->getConstructor() === null) {
332 2
            $lines = array_merge($lines, [
333 2
                '    $object = new ' . $class . '();',
334 2
                '    (function ()' . $use . ' {',
335
            ]);
336
        } else {
337 4
            $lines = array_merge($lines, [
338 4
                '    $class = new \ReflectionClass(\'' . $class . '\');',
339 4
                '    $object = $class->newInstanceWithoutConstructor();',
340 4
                '    (function ()' . $use . ' {',
341
            ]);
342
        }
343
        $endLines = [
344 6
            '    })->bindTo($object, \'' . $class . '\')();',
345 6
            '',
346 6
            '    return $object;',
347 6
            '})()',
348
        ];
349
350
        /**
351
         * @psalm-var mixed $value
352
         * @psalm-var string $name
353
         */
354 6
        foreach ($objectProperties as $name => $value) {
355 6
            $propertyName = $this->getPropertyName($name);
356 6
            $lines[] = '        $this->' . $propertyName . ' = ' .
357 6
                $this->exportInternal($value, $format, $level + 2) . ';';
358
        }
359
360 6
        return implode("\n" . ($format ? $spaces : ''), array_merge($lines, $endLines));
361
    }
362
363
    /**
364
     * Exports a {@see \Closure} instance.
365
     *
366
     * @param Closure $closure Closure instance.
367
     *
368
     * @throws ReflectionException
369
     *
370
     * @return string
371
     */
372 36
    private function exportClosure(Closure $closure, int $level = 0): string
373
    {
374 36
        if (self::$closureExporter === null) {
375 1
            self::$closureExporter = new ClosureExporter();
376
        }
377
378 36
        return self::$closureExporter->export($closure, $level);
0 ignored issues
show
Bug introduced by
The method export() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

378
        return self::$closureExporter->/** @scrutinizer ignore-call */ export($closure, $level);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
379
    }
380
381
    /**
382
     * @param mixed $variable
383
     *
384
     * @return string
385
     */
386 41
    private function exportVariable($variable): string
387
    {
388 41
        return var_export($variable, true);
389
    }
390
391 7
    private function getObjectDescription(object $object): string
392
    {
393 7
        return get_class($object) . '#' . spl_object_id($object);
394
    }
395
396 12
    private function getObjectProperties(object $var): array
397
    {
398 12
        if (!$var instanceof __PHP_Incomplete_Class && method_exists($var, '__debugInfo')) {
399
            /** @var array $var */
400 1
            $var = $var->__debugInfo();
401
        }
402
403 12
        return (array) $var;
404
    }
405
}
406