Completed
Push — master ( ed922a...ce73d9 )
by Nikola
03:26
created

ClassIntrospectionVisitor::leaveNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 20
cp 0
rs 9.2
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * This file is part of the Abstract builder package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\AbstractBuilder\Ast\Visitor;
11
12
use PhpParser\Node;
13
use PhpParser\NodeVisitorAbstract;
14
use PhpParser\Node\Stmt;
15
use RunOpenCode\AbstractBuilder\Ast\MethodMetadata;
16
use RunOpenCode\AbstractBuilder\Exception\NotSupportedException;
17
18
/**
19
 * Class ClassIntrospectionVisitor
20
 *
21
 * @package RunOpenCode\AbstractBuilder\Ast\Visitor
22
 */
23
class ClassIntrospectionVisitor extends NodeVisitorAbstract
24
{
25
    /**
26
     * @var array
27
     */
28
    private $classes;
29
30
    /**
31
     * @var array
32
     */
33
    private $ast;
34
35
    /**
36
     * @var string
37
     */
38
    private $namespace;
39
40
    /**
41
     * @var string
42
     */
43
    private $class;
44
45
    /**
46
     * @var bool
47
     */
48
    private $final;
49
50
    /**
51
     * @var bool
52
     */
53
    private $abstract;
54
55
    /**
56
     * @var MethodMetadata[]
57
     */
58
    private $methods;
59
60
    /**
61
     * @var string|null
62
     */
63
    private $parent;
64
65
    /**
66
     * {@inheritDoc}
67
     *
68
     * Cleans up internal state
69
     *
70
     * @param array $nodes
71
     */
72
    public function beforeTraverse(array $nodes)
73
    {
74
        $this->classes = [];
75
76
        $this->ast = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $ast.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->namespace = null;
78
        $this->class = null;
79
        $this->final = false;
80
        $this->abstract = false;
81
        $this->methods = [];
82
        $this->parent = null;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @throws \RunOpenCode\AbstractBuilder\Exception\NotSupportedException
89
     */
90
    public function enterNode(Node $node)
91
    {
92
        if ($node instanceof Stmt\Namespace_) {
93
94
            if (null !== $this->namespace) {
95
                throw new NotSupportedException(sprintf('Multiple namespaces in single file are not supported.'));
96
            }
97
98
            $this->namespace = $node->name->toString();
99
        }
100
101
        if ($node instanceof Stmt\Class_ && !$node->isAnonymous()) {
102
103
            if (null !== $this->class) {
104
                throw new NotSupportedException(sprintf('Multiple classes in single file are not supported.'));
105
            }
106
107
            $this->ast = $node;
0 ignored issues
show
Documentation Bug introduced by
It seems like $node of type object<PhpParser\Node\Stmt\Class_> is incompatible with the declared type array of property $ast.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
            $this->class = $node->name;
109
            $this->final = $node->isFinal();
110
            $this->abstract = $node->isAbstract();
111
112
            if (null !== $node->extends) {
113
                $this->parent = $node->extends->toString();
114
            }
115
        }
116
117
        if (null !== $this->class && $node instanceof Stmt\ClassMethod) {
118
            $this->methods[] = MethodMetadata::fromClassMethod($node);
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function leaveNode(Node $node)
126
    {
127
        if ($node instanceof Stmt\Class_) {
128
129
            $this->classes[] = [
130
                'ast' => $this->ast,
131
                'namespace' => $this->namespace,
132
                'class' => $this->class,
133
                'fqcn' => $this->namespace.'\\'.$this->class,
134
                'final' => $this->final,
135
                'abstract' => $this->abstract,
136
                'methods' => $this->methods,
137
                'parent' => $this->parent
138
            ];
139
140
            $this->class = null;
141
            $this->final = false;
142
            $this->abstract = false;
143
            $this->methods = [];
144
            $this->parent = null;
145
        }
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function getClasses()
152
    {
153
        return $this->classes;
154
    }
155
}
156