NodeDumper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 57.41 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 0
cbo 1
dl 31
loc 54
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C dump() 31 44 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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