DebugNodeVisitor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 34
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 4 4
A leaveNode() 0 4 1
A beforeTraverse() 0 5 1
A afterTraverse() 0 5 1
A debug() 0 4 1
1
<?php
2
3
namespace Benoth\StaticReflection\Parser;
4
5
use PhpParser\Node as NodeInterface;
6
use PhpParser\NodeVisitorAbstract;
7
8
/**
9
 * @codeCoverageIgnore
10
 */
11
class DebugNodeVisitor extends NodeVisitorAbstract
12
{
13
    protected $level = 0;
14
15
    public function enterNode(NodeInterface $node)
16
    {
17
        $this->debug(str_repeat('  ', ++$this->level).$node->getType().(property_exists($node, 'name') ? ' '.(method_exists($node->name, 'toString') ? $node->name->toString() : $node->name) : '').(method_exists($node, 'getLine') ? ' L:'.$node->getLine() : '').PHP_EOL);
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
18
    }
19
20
    public function leaveNode(NodeInterface $node)
21
    {
22
        --$this->level;
23
    }
24
25
    public function beforeTraverse(array $nodes)
26
    {
27
        $this->level = 0;
28
        $this->debug('BeforeTraverse'.PHP_EOL);
29
    }
30
31
    public function afterTraverse(array $nodes)
32
    {
33
        $this->level = 0;
34
        $this->debug('AfterTraverse'.PHP_EOL);
35
    }
36
37
    /**
38
     * @param string $text
39
     */
40
    protected function debug($text)
41
    {
42
        echo $text;
43
    }
44
}
45