Completed
Push — develop ( 346f28...6e1df5 )
by Jaap
8s
created

ElementNameResolver   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 0
loc 109
ccs 54
cts 63
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeTraverse() 0 4 1
B leaveNode() 0 16 10
C enterNode() 0 39 11
A resetState() 0 5 1
A buildName() 0 8 2
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
14
namespace phpDocumentor\Reflection\NodeVisitor;
15
16
use phpDocumentor\Reflection\Fqsen;
17
use PhpParser\Node;
18
use PhpParser\Node\Const_;
19
use PhpParser\Node\Name;
20
use PhpParser\Node\Stmt\Class_;
21
use PhpParser\Node\Stmt\ClassConst;
22
use PhpParser\Node\Stmt\ClassMethod;
23
use PhpParser\Node\Stmt\Function_;
24
use PhpParser\Node\Stmt\Interface_;
25
use PhpParser\Node\Stmt\Namespace_;
26
use PhpParser\Node\Stmt\PropertyProperty;
27
use PhpParser\Node\Stmt\Trait_;
28
use PhpParser\NodeTraverser;
29
use PhpParser\NodeVisitorAbstract;
30
31
final class ElementNameResolver extends NodeVisitorAbstract
0 ignored issues
show
Complexity introduced by
The class ElementNameResolver has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
32
{
33
    /**
34
     * @var \SplDoublyLinkedList
35
     */
36
    private $parts = null;
37
38
    /**
39
     * Resets the object to a known state before start processing.
40
     *
41
     * @param array $nodes
42
     */
43
    public function beforeTraverse(array $nodes)
44
    {
45
        $this->resetState('\\');
46
    }
47
48
    /**
49
     * Performs a reset of the added element when needed.
50
     *
51
     * @param Node $node
52
     */
53 1
    public function leaveNode(Node $node)
54
    {
55 1
        switch (get_class($node)) {
56 1
            case Namespace_::class:
57 1
            case Class_::class:
58 1
            case ClassMethod::class:
59 1
            case Trait_::class:
60 1
            case PropertyProperty::class:
61 1
            case ClassConst::class:
62 1
            case Const_::class:
63 1
            case Interface_::class:
64 1
            case Function_::class:
65 1
                $this->parts->pop();
66 1
                break;
67 1
        }
68 1
    }
69
70
    /**
71
     * Adds fqsen property to a node when applicable.
72
     *
73
     * @param Node $node
74
     */
75 6
    public function enterNode(Node $node)
76
    {
77 6
        switch (get_class($node)) {
78 6
            case Namespace_::class:
79 2
                $this->resetState('\\' . $node->name . '\\');
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...
80 2
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
81 2
                break;
82 6
            case Class_::class:
83 6
            case Trait_::class:
84 6
            case Interface_::class:
85 4
                $this->parts->push((string)$node->name);
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...
86
87 4
                if (is_null($node->name)) {
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...
88 2
                    return NodeTraverser::DONT_TRAVERSE_CHILDREN;
89
                }
90
91 2
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
92 2
                break;
93 4
            case Function_::class:
94 1
                $this->parts->push($node->name . '()');
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...
95 1
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
96 1
                break;
97 3
            case ClassMethod::class:
98
                $this->parts->push('::' . $node->name . '()');
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...
99
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
100
                break;
101 3
            case ClassConst::class:
102 1
                $this->parts->push('::');
103 1
                break;
104 3
            case Const_::class:
105 2
                $this->parts->push($node->name);
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...
106 2
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
107 2
                break;
108 1
            case PropertyProperty::class:
109
                $this->parts->push('::$' . $node->name);
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...
110
                $node->fqsen = new Fqsen($this->buildName());
0 ignored issues
show
Bug introduced by
Accessing fqsen 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...
111
                break;
112 5
        }
113 5
    }
114
115
    /**
116
     * Resets the state of the object to an empty state.
117
     *
118
     * @param string $namespace
119
     */
120 6
    private function resetState($namespace = null)
121
    {
122 6
        $this->parts = new \SplDoublyLinkedList();
123 6
        $this->parts->push($namespace);
124 6
    }
125
126
    /**
127
     * Builds the name of the current node using the parts that are pushed to the parts list.
128
     *
129
     * @return null|string
130
     */
131 5
    private function buildName()
132
    {
133 5
        $name = null;
134 5
        foreach ($this->parts as $part) {
135 5
            $name .= $part;
136 5
        }
137 5
        return rtrim($name, '\\');
138
    }
139
}
140