Issues (201)

src/Tools/Dumper.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Tools;
6
7
use ArrayIterator;
8
use ArrayObject;
9
use DateTimeInterface;
10
use Doctrine\Common\Collections\Collection;
0 ignored issues
show
The type Doctrine\Common\Collections\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\Common\Persistence\Proxy;
0 ignored issues
show
The type Doctrine\Common\Persistence\Proxy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use stdClass;
13
use function array_keys;
14
use function assert;
15
use function class_exists;
16
use function count;
17
use function end;
18
use function explode;
19
use function extension_loaded;
20
use function get_class;
21
use function html_entity_decode;
22
use function ini_set;
23
use function is_array;
24
use function is_object;
25
use function is_string;
26
use function ob_get_clean;
27
use function ob_start;
28
use function strip_tags;
29
use function strlen;
30
use function strrpos;
31
use function substr;
32
use function var_dump;
33
34
/**
35
 * Static class used to dump the variable to be used on output.
36
 * Simplified port of Util\Debug from doctrine/common.
37
 *
38
 * @internal
39
 */
40
final class Dumper
41
{
42
    /**
43
     * Private constructor (prevents instantiation).
44
     */
45
    private function __construct()
46
    {
47
    }
48
49
    /**
50
     * Returns a dump of the public, protected and private properties of $var.
51
     *
52
     * @link https://xdebug.org/
53
     *
54
     * @param mixed $var      The variable to dump.
55
     * @param int   $maxDepth The maximum nesting level for object properties.
56
     */
57 66
    public static function dump($var, int $maxDepth = 2) : string
58
    {
59 66
        $html = ini_set('html_errors', '1');
60 66
        assert(is_string($html));
61
62 66
        if (extension_loaded('xdebug')) {
63 3
            ini_set('xdebug.var_display_max_depth', (string) $maxDepth);
64
        }
65
66 66
        $var = self::export($var, $maxDepth);
67
68 66
        ob_start();
69 66
        var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var) looks like debug code. Are you sure you do not want to remove it?
Loading history...
70
71
        try {
72 66
            $output = ob_get_clean();
73 66
            assert(is_string($output));
74
75 66
            return strip_tags(html_entity_decode($output));
76
        } finally {
77 66
            ini_set('html_errors', $html);
78
        }
79
    }
80
81
    /**
82
     * @param mixed $var
83
     *
84
     * @return mixed
85
     */
86 264
    public static function export($var, int $maxDepth)
87
    {
88 264
        $return = null;
89 264
        $isObj  = is_object($var);
90
91 264
        if ($var instanceof Collection) {
92
            $var = $var->toArray();
93
        }
94
95 264
        if ($maxDepth === 0) {
96 44
            return is_object($var) ? get_class($var)
97 44
                : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
98
        }
99
100 264
        if (is_array($var)) {
101 88
            $return = [];
102
103 88
            foreach ($var as $k => $v) {
104 88
                $return[$k] = self::export($v, $maxDepth - 1);
105
            }
106
107 88
            return $return;
108
        }
109
110 264
        if (! $isObj) {
111 198
            return $var;
112
        }
113
114 176
        $return = new stdClass();
115 176
        if ($var instanceof DateTimeInterface) {
116 66
            $return->__CLASS__ = get_class($var);
117 66
            $return->date      = $var->format('c');
118 66
            $return->timezone  = $var->getTimezone()->getName();
119
120 66
            return $return;
121
        }
122
123 110
        $return->__CLASS__ = self::getClass($var);
124
125 110
        if ($var instanceof Proxy) {
126
            $return->__IS_PROXY__          = true;
127
            $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
128
        }
129
130 110
        if ($var instanceof ArrayObject || $var instanceof ArrayIterator) {
131 22
            $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
132
        }
133
134 110
        return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
135
    }
136
137
    /**
138
     * Fill the $return variable with class attributes
139
     * Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075}
140
     *
141
     * @return mixed
142
     */
143 110
    private static function fillReturnWithClassAttributes(object $var, stdClass $return, int $maxDepth)
144
    {
145 110
        $clone = (array) $var;
146
147 110
        foreach (array_keys($clone) as $key) {
148 110
            $aux  = explode("\0", (string) $key);
149 110
            $name = end($aux);
150 110
            if ($aux[0] === '') {
151 44
                $name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private');
152
            }
153
154 110
            $return->$name = self::export($clone[$key], $maxDepth - 1);
155
        }
156
157 110
        return $return;
158
    }
159
160 110
    private static function getClass(object $object) : string
161
    {
162 110
        $class = get_class($object);
163
164 110
        if (! class_exists(Proxy::class)) {
165 110
            return $class;
166
        }
167
168
        $pos = strrpos($class, '\\' . Proxy::MARKER . '\\');
169
170
        if ($pos === false) {
171
            return $class;
172
        }
173
174
        return substr($class, $pos + strlen(Proxy::MARKER) + 2);
175
    }
176
}
177