Completed
Push — master ( bdaaad...4f4372 )
by personal
04:35 queued 45s
created

functions.php ➔ getNameOfNode()   C

Complexity

Conditions 17
Paths 13

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 26
nc 13
nop 1
dl 0
loc 51
rs 5.6079
c 0
b 0
f 0

How to fix   Long Method    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
/**
4
 * Class MyVisitor
5
 */
6
class MyVisitor extends \PhpParser\NodeVisitorAbstract
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    /**
10
     * @var
11
     */
12
    private $callback;
13
14
    /**
15
     * MyVisitor constructor.
16
     * @param $callback
17
     */
18
    public function __construct($callback)
19
    {
20
        $this->callback = $callback;
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function leaveNode(\PhpParser\Node $node)
27
    {
28
        call_user_func($this->callback, $node);
29
    }
30
}
31
32
/**
33
 * @param $node
34
 * @param $callback
35
 */
36
function iterate_over_node($node, $callback)
37
{
38
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
         // way 2
40
         foreach ($node->getSubNodeNames() as $name) {
41
             $subNode = $node->$name;
42
43
44
45
             if ($subNode instanceof \PhpParser\Node) {
46
47
                 return iterate_over_node($node, $callback);
48
             }
49
50
             if (is_array($subNode)) {
51
                 foreach ($subNode as $sub) {
52
                     return iterate_over_node($sub, $callback);
53
                 }
54
             }
55
56
                 return $callback($node);
57
         }
58
59
         return $callback($node);
60
    */
61
62
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
        // way 1
64
        static $traverser;
65
        if (!isset($traverser)) {
66
            $myVisitor = new MyVisitor($callback);
67
            $traverser = new \PhpParser\NodeTraverser();
68
            $traverser->addVisitor($myVisitor);
69
        }
70
        $traverser->traverse(array($node));
71
        return;
72
    */
73
74
    // way 1
75
    $myVisitor = new MyVisitor($callback);
76
    $traverser = new \PhpParser\NodeTraverser();
77
    $traverser->addVisitor($myVisitor);
78
    $traverser->traverse(array($node));
79
    return;
80
}
81
82
/**
83
 * @param $node
84
 * @return string
85
 */
86
function getNameOfNode($node)
87
{
88
    if (is_string($node)) {
89
        return $node;
90
    }
91
92
    if ($node instanceof \PhpParser\Node\Name\FullyQualified) {
93
        return (string)$node;
94
    }
95
    if ($node instanceof \PhpParser\Node\Expr\New_) {
96
        return getNameOfNode($node->class);
97
    }
98
99
    if (isset($node->class)) {
100
        return getNameOfNode($node->class);
101
    }
102
103
    if ($node instanceof \PhpParser\Node\Name) {
104
        return (string)implode($node->parts);
105
    }
106
107
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\Variable) {
108
        return getNameOfNode($node->name);
109
    }
110
111
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\MethodCall) {
112
        return getNameOfNode($node->name);
113
    }
114
115
    if ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
116
        return getNameOfNode($node->var);
117
    }
118
119
    if (isset($node->name) && $node->name instanceof \PhpParser\Node\Expr\BinaryOp) {
120
        return get_class($node->name);
121
    }
122
123
    if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) {
124
        return getNameOfNode($node->var);
125
    }
126
127
    if (isset($node->name) && !is_string($node->name)) {
128
        return getNameOfNode($node->name);
129
    }
130
131
    if (isset($node->name)) {
132
        return (string)$node->name;
133
    }
134
135
    return null;
136
}
137
138
/**
139
 * @param $src
140
 * @param $dst
141
 */
142
function recurse_copy($src, $dst)
143
{
144
    $dir = opendir($src);
145
    if (!file_exists($dst)) {
146
        mkdir($dst);
147
    }
148
    while (false !== ($file = readdir($dir))) {
149
        if (($file != '.') && ($file != '..')) {
150
            if (is_dir($src . '/' . $file)) {
151
                recurse_copy($src . '/' . $file, $dst . '/' . $file);
152
            } else {
153
                copy($src . '/' . $file, $dst . '/' . $file);
154
            }
155
        }
156
    }
157
    closedir($dir);
158
}
159
160
/**
161
 * @return string
162
 */
163
function getVersion()
164
{
165
    return 'v2.0.0';
166
}