NodeDumper::dump()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 44
Code Lines 33

Duplication

Lines 31
Ratio 70.45 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 31
loc 44
rs 5.1234
cc 13
eloc 33
nc 13
nop 1

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 PhpParser;
4
5
class NodeDumper
6
{
7
    /**
8
     * Dumps a node or array.
9
     *
10
     * @param array|Node $node Node or array to dump
11
     *
12
     * @return string Dumped value
13
     */
14
    public function dump($node) {
15
        if ($node instanceof Node) {
16
            $r = $node->getType() . '(';
17
18 View Code Duplication
            foreach ($node->getSubNodeNames() as $key) {
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...
19
                $r .= "\n    " . $key . ': ';
20
21
                $value = $node->$key;
22
                if (null === $value) {
23
                    $r .= 'null';
24
                } elseif (false === $value) {
25
                    $r .= 'false';
26
                } elseif (true === $value) {
27
                    $r .= 'true';
28
                } elseif (is_scalar($value)) {
29
                    $r .= $value;
30
                } else {
31
                    $r .= str_replace("\n", "\n    ", $this->dump($value));
32
                }
33
            }
34
        } elseif (is_array($node)) {
35
            $r = 'array(';
36
37 View Code Duplication
            foreach ($node as $key => $value) {
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...
38
                $r .= "\n    " . $key . ': ';
39
40
                if (null === $value) {
41
                    $r .= 'null';
42
                } elseif (false === $value) {
43
                    $r .= 'false';
44
                } elseif (true === $value) {
45
                    $r .= 'true';
46
                } elseif (is_scalar($value)) {
47
                    $r .= $value;
48
                } else {
49
                    $r .= str_replace("\n", "\n    ", $this->dump($value));
50
                }
51
            }
52
        } else {
53
            throw new \InvalidArgumentException('Can only dump nodes and arrays.');
54
        }
55
56
        return $r . "\n)";
57
    }
58
}
59