GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NodeWalker::walk()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace Inspector\Analysis;
4
5
use PhpParser\Node;
6
use Inspector\Analysis\Exception\AnalysisException;
7
8
/**
9
 * Walks/Traverses through each Node in a Abstract Syntax Tree recursively
10
 * triggering a callback for each node.
11
 */
12
class NodeWalker
13
{
14
15
    /**
16
     * @param Node[]|null $ast
17
     * @param callable $callback
18
     * @return array
19
     */
20
    public function walk($ast, callable $callback)
21
    {
22
        if (empty($ast)) {
23
            return [];
24
        }
25
26
        // Ensure that it is an array of nodes
27
        $ast = $this->harmonizeNodes($ast);
28
29
        $errors = [];
30
31
        $this->foreachNode($ast, function (Node $node) use (&$errors, $callback) {
32
33
            $errors = array_merge($errors, $this->triggerCallback($callback, $node));
34
35
            $this->foreachSubNodes($node, function (array $subNodes) use (&$errors, $callback) {
36
                $moreErrors = $this->walk($subNodes, $callback);
37
                $errors = array_merge($errors, $moreErrors);
38
            });
39
        });
40
41
        return $errors;
42
    }
43
44
    /**
45
     * Triggers the callback for the walker and catches errors
46
     *
47
     * @param callable $callback
48
     * @param $node
49
     * @return array
50
     */
51
    protected function triggerCallback(callable $callback, Node $node)
52
    {
53
        $errors = [];
54
        try {
55
            $callback($node);
56
        } catch (AnalysisException $e) {
57
            $errors[] = $e;
58
        }
59
60
        return $errors;
61
    }
62
63
    /**
64
     * Makes it a consistent array of node even if it is just a single instance of Node.
65
     *
66
     * @param mixed $ast
67
     * @return array
68
     */
69
    protected function harmonizeNodes($ast)
70
    {
71
        return ($ast instanceof Node) ? [$ast] : $ast;
72
    }
73
74
    /**
75
     * Loops through the AST for each Node (linearly) and triggers the callback for it.
76
     *
77
     * @param Node[]|null $ast
78
     * @param callable $callback
79
     */
80
    protected function foreachNode($ast, callable $callback)
81
    {
82
        foreach ($ast as $node) {
0 ignored issues
show
Bug introduced by
The expression $ast of type array<integer,object<PhpParser\Node>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
83
            if (!($node instanceof Node)) {
84
                continue;
85
            }
86
87
            $callback($node);
88
        }
89
    }
90
91
    /**
92
     * Checks and goes through each list of sub nodes a given Node has,
93
     * and triggers the callback for each of those sub nodes (Node[])
94
     *
95
     * @param Node $node
96
     * @param callable $callback
97
     */
98
    protected function foreachSubNodes(Node $node, callable $callback)
99
    {
100
        foreach ($node->getSubNodeNames() as $subNodeName) {
101
            $subNodeList = $node->{$subNodeName};
102
103
            $subNodeList = $this->harmonizeNodes($subNodeList);
104
105
            if (!is_array($subNodeList)) {
106
                continue;
107
            }
108
109
            // Node[] $subNodeList
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
110
            $callback($subNodeList);
111
        }
112
    }
113
}
114