Completed
Push — master ( 03a881...28de70 )
by Damian
09:16
created

ClassManifestVisitor::getClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Manifest;
4
5
use PhpParser\Node;
6
use PhpParser\NodeTraverser;
7
use PhpParser\NodeVisitorAbstract;
8
9
class ClassManifestVisitor extends NodeVisitorAbstract
10
{
11
12
    private $classes = [];
13
14
    private $traits = [];
15
16
    private $interfaces = [];
17
18
    public function resetState()
19
    {
20
        $this->classes = [];
21
        $this->traits = [];
22
        $this->interfaces = [];
23
    }
24
25
    public function beforeTraverse(array $nodes)
26
    {
27
        $this->resetState();
28
    }
29
30
    public function enterNode(Node $node)
31
    {
32
        if ($node instanceof Node\Stmt\Class_) {
33
            $extends = '';
34
            $interfaces = [];
35
36
            if ($node->extends) {
37
                $extends = array((string)$node->extends);
38
            }
39
40
            if ($node->implements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $node->implements of type PhpParser\Node\Name[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
                foreach ($node->implements as $interface) {
42
                    $interfaces[] = (string)$interface;
43
                }
44
            }
45
46
            $this->classes[(string)$node->namespacedName] = [
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Class_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
                'extends' => $extends,
48
                'interfaces' => $interfaces,
49
            ];
50
        } elseif ($node instanceof Node\Stmt\Trait_) {
51
            $this->traits[(string)$node->namespacedName] = array();
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Trait_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
        } elseif ($node instanceof Node\Stmt\Interface_) {
53
            $extends = array();
54
            foreach ($node->extends as $ancestor) {
0 ignored issues
show
Bug introduced by
The expression $node->extends of type null|object<PhpParser\Node\Name> 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...
55
                $extends[] = (string)$ancestor;
56
            }
57
            $this->interfaces[(string)$node->namespacedName] = [
0 ignored issues
show
Bug introduced by
The property namespacedName does not seem to exist in PhpParser\Node\Stmt\Interface_.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
                'extends' => $extends,
59
            ];
60
        }
61
        if (!$node instanceof Node\Stmt\Namespace_) {
62
            //break out of traversal as we only need highlevel information here!
63
            return NodeTraverser::DONT_TRAVERSE_CHILDREN;
64
        }
65
    }
66
67
    public function getClasses()
68
    {
69
        return $this->classes;
70
    }
71
72
    public function getTraits()
73
    {
74
        return $this->traits;
75
    }
76
77
    public function getInterfaces()
78
    {
79
        return $this->interfaces;
80
    }
81
}
82