Failed Conditions
Push — replace-common-debug ( c44207 )
by Michael
30:13
created

Dumper::export()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12.1769

Importance

Changes 0
Metric Value
dl 0
loc 49
c 0
b 0
f 0
ccs 25
cts 28
cp 0.8929
rs 5.1474
cc 12
eloc 27
nc 24
nop 2
crap 12.1769

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\DBAL\Tools;
4
5
use ArrayIterator;
6
use ArrayObject;
7
use DateTimeInterface;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\Common\Persistence\Proxy;
10
use stdClass;
11
use function array_keys;
12
use function class_exists;
13
use function count;
14
use function end;
15
use function explode;
16
use function extension_loaded;
17
use function get_class;
18
use function html_entity_decode;
19
use function ini_get;
20
use function ini_set;
21
use function is_array;
22
use function is_object;
23
use function ob_get_clean;
24
use function ob_start;
25
use function strip_tags;
26
use function strrpos;
27
use function substr;
28
use function var_dump;
29
30
/**
31
 * Static class used to dump the variable to be used on output.
32
 * Simplified port of Util\Debug from doctrine/common.
33
 *
34
 * @internal
35
 */
36
final class Dumper
37
{
38
    /**
39
     * Private constructor (prevents instantiation).
40
     */
41
    private function __construct()
42
    {
43
    }
44
45
    /**
46
     * Returns a dump of the public, protected and private properties of $var.
47
     *
48
     * @link https://xdebug.org/
49
     *
50
     * @param mixed $var      The variable to dump.
51
     * @param int   $maxDepth The maximum nesting level for object properties.
52
     */
53 51
    public static function dump($var, int $maxDepth = 2) : string
54
    {
55 51
        $html = ini_get('html_errors');
56
57 51
        if ($html !== true) {
58 51
            ini_set('html_errors', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $newvalue of ini_set(). ( Ignorable by Annotation )

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

58
            ini_set('html_errors', /** @scrutinizer ignore-type */ true);
Loading history...
59
        }
60
61 51
        if (extension_loaded('xdebug')) {
62 51
            ini_set('xdebug.var_display_max_depth', $maxDepth);
63
        }
64
65 51
        $var = self::export($var, $maxDepth);
66
67 51
        ob_start();
68 51
        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...
69
70
        try {
71 51
            return strip_tags(html_entity_decode(ob_get_clean()));
72
        } finally {
73 51
            ini_set('html_errors', $html);
74
        }
75
    }
76
77
    /**
78
     * @param mixed $var
79
     *
80
     * @return mixed
81
     */
82 204
    public static function export($var, int $maxDepth)
83
    {
84 204
        $return = null;
85 204
        $isObj  = is_object($var);
86
87 204
        if ($var instanceof Collection) {
88
            $var = $var->toArray();
89
        }
90
91 204
        if ($maxDepth === 0) {
92 34
            return is_object($var) ? get_class($var)
93 34
                : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
94
        }
95
96 204
        if (is_array($var)) {
97 68
            $return = [];
98
99 68
            foreach ($var as $k => $v) {
100 68
                $return[$k] = self::export($v, $maxDepth - 1);
101
            }
102
103 68
            return $return;
104
        }
105
106 204
        if (! $isObj) {
107 153
            return $var;
108
        }
109
110 136
        $return = new stdClass();
111 136
        if ($var instanceof DateTimeInterface) {
112 51
            $return->__CLASS__ = get_class($var);
113 51
            $return->date      = $var->format('c');
114 51
            $return->timezone  = $var->getTimezone()->getName();
115
116 51
            return $return;
117
        }
118
119 85
        $return->__CLASS__ = self::getClass($var);
120
121 85
        if ($var instanceof Proxy) {
122
            $return->__IS_PROXY__          = true;
123
            $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
124
        }
125
126 85
        if ($var instanceof ArrayObject || $var instanceof ArrayIterator) {
127 17
            $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
128
        }
129
130 85
        return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
0 ignored issues
show
Bug introduced by
It seems like $var can also be of type array; however, parameter $var of Doctrine\DBAL\Tools\Dump...rnWithClassAttributes() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

130
        return self::fillReturnWithClassAttributes(/** @scrutinizer ignore-type */ $var, $return, $maxDepth);
Loading history...
131
    }
132
133
    /**
134
     * Fill the $return variable with class attributes
135
     * Based on obj2array function from {@see https://secure.php.net/manual/en/function.get-object-vars.php#47075}
136
     *
137
     * @param object $var
138
     *
139
     * @return mixed
140
     */
141 85
    private static function fillReturnWithClassAttributes($var, stdClass $return, int $maxDepth)
142
    {
143 85
        $clone = (array) $var;
144
145 85
        foreach (array_keys($clone) as $key) {
146 85
            $aux  = explode("\0", $key);
147 85
            $name = end($aux);
148 85
            if ($aux[0] === '') {
149 34
                $name .= ':' . ($aux[1] === '*' ? 'protected' : $aux[1] . ':private');
150
            }
151 85
            $return->$name = self::export($clone[$key], $maxDepth - 1);
152
            ;
153
        }
154
155 85
        return $return;
156
    }
157
158
    /**
159
     * @param object $object
160
     */
161 85
    private static function getClass($object) : string
162
    {
163 85
        $class = get_class($object);
164
165 85
        if (! class_exists(Proxy::class)) {
166 85
            return $class;
167
        }
168
169
        $pos = strrpos($class, '\\' . Proxy::MARKER . '\\');
170
171
        if ($pos === false) {
172
            return $class;
173
        }
174
175
        return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
176
    }
177
}
178