Completed
Push — master ( 439122...918b18 )
by Anton
04:45
created

Dumper::dump()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 4.8196
cc 10
eloc 26
nc 12
nop 2

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Debug;
9
10
use Psr\Log\LoggerAwareInterface;
11
use Psr\Log\LoggerInterface;
12
use Spiral\Core\Component;
13
use Spiral\Core\Container\SingletonInterface;
14
use Spiral\Debug\Dumper\Style;
15
use Spiral\Debug\Traits\BenchmarkTrait;
16
use Spiral\Debug\Traits\LoggerTrait;
17
18
/**
19
 * One of the oldest spiral parts, used to dump variables content in user friendly way.
20
 */
21
class Dumper extends Component implements SingletonInterface, LoggerAwareInterface
22
{
23
    use LoggerTrait, BenchmarkTrait;
24
25
    /**
26
     * Options for dump() function to specify output.
27
     */
28
    const OUTPUT_ECHO     = 0;
29
    const OUTPUT_RETURN   = 1;
30
    const OUTPUT_LOG      = 2;
31
    const OUTPUT_LOG_NICE = 3;
32
33
    /**
34
     * Deepest level to be dumped.
35
     *
36
     * @var int
37
     */
38
    private $maxLevel = 10;
39
40
    /**
41
     * @invisible
42
     * @var Style
43
     */
44
    private $style = null;
45
46
    /**
47
     * @param int             $maxLevel
48
     * @param Style           $styler Light styler to be used by default.
49
     * @param LoggerInterface $logger
50
     */
51
    public function __construct(
52
        $maxLevel = 10,
53
        Style $styler = null,
54
        LoggerInterface $logger = null
55
    ) {
56
        $this->maxLevel = $maxLevel;
57
        $this->style = !empty($styler) ? $styler : new Style();
58
        $this->logger = $logger;
59
    }
60
61
    /**
62
     * Set dump styler.
63
     *
64
     * @param Style $style
65
     * @return $this
66
     */
67
    public function setStyle(Style $style)
68
    {
69
        $this->style = $style;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Dump specified value.
76
     *
77
     * @param mixed $value
78
     * @param int   $output
79
     * @return null|string
80
     */
81
    public function dump($value, $output = self::OUTPUT_ECHO)
82
    {
83
        if (php_sapi_name() === 'cli' && $output == self::OUTPUT_ECHO) {
84
            if (is_object($value) && method_exists($value, '__debugInfo')) {
85
                $value = $value->__debugInfo();
86
            }
87
88
            print_r($value);
89
            if (is_scalar($value)) {
90
                echo "\n";
91
            }
92
93
            return null;
94
        }
95
96
        //Dumping is pretty slow operation, let's record it so we can exclude dump time from application
97
        //timeline
98
        $benchmark = $this->benchmark('dump');
99
        try {
100
            switch ($output) {
101
                case self::OUTPUT_ECHO:
102
                    echo $this->style->mountContainer($this->dumpValue($value, '', 0));
103
                    break;
104
105
                case self::OUTPUT_RETURN:
106
                    return $this->style->mountContainer($this->dumpValue($value, '', 0));
107
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
108
109
                case self::OUTPUT_LOG:
110
                    $this->logger()->debug(print_r($value, true));
111
                    break;
112
113
                case self::OUTPUT_LOG_NICE:
114
                    $this->logger()->debug($this->dump($value, self::OUTPUT_RETURN));
115
                    break;
116
            }
117
118
            return null;
119
        } finally {
120
            $this->benchmark($benchmark);
121
        }
122
    }
123
124
    /**
125
     * Variable dumper. This is the oldest spiral function originally written in 2007. :)
126
     *
127
     * @param mixed  $value
128
     * @param string $name       Variable name, internal.
129
     * @param int    $level      Dumping level, internal.
130
     * @param bool   $hideHeader Hide array/object header, internal.
131
     * @return string
132
     */
133
    private function dumpValue($value, $name = '', $level = 0, $hideHeader = false)
134
    {
135
        //Any dump starts with initial indent (level based)
136
        $indent = $this->style->indent($level);
137
138
        if (!$hideHeader && !empty($name)) {
139
            //Showing element name (if any provided)
140
            $header = $indent . $this->style->style($name, "name");
141
142
            //Showing equal sing
143
            $header .= $this->style->style(" = ", "syntax", "=");
144
        } else {
145
            $header = $indent;
146
        }
147
148
        if ($level > $this->maxLevel) {
149
            //Dumper is not reference based, we can't dump too deep values
150
            return $indent . $this->style->style('-too deep-', 'maxLevel') . "\n";
151
        }
152
153
        $type = strtolower(gettype($value));
154
155
        if ($type == 'array') {
156
            return $header . $this->dumpArray($value, $level, $hideHeader);
157
        }
158
159
        if ($type == 'object') {
160
            return $header . $this->dumpObject($value, $level, $hideHeader);
161
        }
162
163
        if ($type == 'resource') {
164
            //No need to dump resource value
165
            $element = get_resource_type($value) . " resource ";
166
167
            return $header . $this->style->style($element, "type", "resource") . "\n";
168
        }
169
170
        //Value length
171
        $length = strlen($value);
172
173
        //Including type size
174
        $header .= $this->style->style("{$type}({$length})", "type", $type);
175
176
        $element = null;
177
        switch ($type) {
178
            case "string":
179
                $element = htmlspecialchars($value);
180
                break;
181
182
            case "boolean":
183
                $element = ($value ? "true" : "false");
184
                break;
185
186
            default:
187
                if ($value !== null) {
188
                    //Not showing null value, type is enough
189
                    $element = var_export($value, true);
190
                }
191
        }
192
193
        //Including value
194
        return $header . " " . $this->style->style($element, "value", $type) . "\n";
195
    }
196
197
    /**
198
     * @param array $array
199
     * @param int   $level
200
     * @param bool  $hideHeader
201
     * @return string
202
     */
203
    private function dumpArray(array $array, $level, $hideHeader = false)
204
    {
205
        $indent = $this->style->indent($level);
206
207 View Code Duplication
        if (!$hideHeader) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
            $count = count($array);
209
210
            //Array size and scope
211
            $output = $this->style->style("array({$count})", "type", "array") . "\n";
212
            $output .= $indent . $this->style->style("[", "syntax", "[") . "\n";
213
        } else {
214
            $output = '';
215
        }
216
217
        foreach ($array as $key => $value) {
218
            if (!is_numeric($key)) {
219
                if (is_string($key)) {
220
                    $key = htmlspecialchars($key);
221
                }
222
223
                $key = "'{$key}'";
224
            }
225
226
            $output .= $this->dumpValue($value, "[{$key}]", $level + 1);
227
        }
228
229
        if (!$hideHeader) {
230
            //Closing array scope
231
            $output .= $indent . $this->style->style("]", "syntax", "]") . "\n";
232
        }
233
234
        return $output;
235
    }
236
237
    /**
238
     * @param object $object
239
     * @param int    $level
240
     * @param bool   $hideHeader
241
     * @param string $class
242
     * @return string
243
     */
244
    private function dumpObject($object, $level, $hideHeader = false, $class = '')
245
    {
246
        $indent = $this->style->indent($level);
247
248 View Code Duplication
        if (!$hideHeader) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
            $type = ($class ?: get_class($object)) . " object ";
250
251
            $header = $this->style->style($type, "type", "object") . "\n";
252
            $header .= $indent . $this->style->style("(", "syntax", "(") . "\n";
253
        } else {
254
            $header = '';
255
        }
256
257
        //Let's use method specifically created for dumping
258
        if (method_exists($object, '__debugInfo')) {
259
            $debugInfo = $object->__debugInfo();
260
261
            if (is_array($debugInfo)) {
262
                //Pretty view
263
                $debugInfo = (object)$debugInfo;
264
            }
265
266
            if (is_object($debugInfo)) {
267
                //We are not including syntax elements here
268
                return $this->dumpObject($debugInfo, $level, false, get_class($object));
269
            }
270
271
            return $header
272
            . $this->dumpValue($debugInfo, '', $level + (is_scalar($object)), true)
273
            . $indent . $this->style->style(")", "syntax", ")") . "\n";
274
        }
275
276
        if ($object instanceof \Closure) {
277
            //todo: dump source code of closure
278
            $output = '';
279
        } else {
280
            $refection = new \ReflectionObject($object);
281
282
            $output = '';
283
            foreach ($refection->getProperties() as $property) {
284
                $output .= $this->dumpProperty($object, $property, $level);
285
            }
286
        }
287
288
        //Header, content, footer
289
        return $header . $output . $indent . $this->style->style(")", "syntax", ")") . "\n";
290
    }
291
292
    /**
293
     * @param object              $object
294
     * @param \ReflectionProperty $property
295
     * @param int                 $level
296
     * @return string
297
     */
298
    private function dumpProperty($object, \ReflectionProperty $property, $level)
299
    {
300
        if ($property->isStatic()) {
301
            return '';
302
        }
303
304
        if (
305
            !($object instanceof \stdClass)
306
            && strpos($property->getDocComment(), '@invisible') !== false
307
        ) {
308
            //Memory loop while reading doc comment for stdClass variables?
309
            //Report a PHP bug about treating comment INSIDE property declaration as doc comment.
310
            return '';
311
        }
312
313
        //Property access level
314
        $access = $this->getAccess($property);
315
316
        //To read private and protected properties
317
        $property->setAccessible(true);
318
319
        if ($object instanceof \stdClass) {
320
            $access = 'dynamic';
321
        }
322
323
        //Property name includes access level
324
        $name = $property->getName() . $this->style->style(":" . $access, "access", $access);
325
326
        return $this->dumpValue($property->getValue($object), $name, $level + 1);
327
    }
328
329
    /**
330
     * Property access level label.
331
     *
332
     * @param \ReflectionProperty $property
333
     * @return string
334
     */
335
    private function getAccess(\ReflectionProperty $property)
336
    {
337
        if ($property->isPrivate()) {
338
            return 'private';
339
        } elseif ($property->isProtected()) {
340
            return 'protected';
341
        }
342
343
        return 'public';
344
    }
345
}