Completed
Push — master ( 72342f...ed922a )
by Nikola
06:37
created

ClassIntrospectionVisitor::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\Exception\NotSupportedException;
16
17
/**
18
 * Class ClassIntrospectionVisitor
19
 *
20
 * @package RunOpenCode\AbstractBuilder\Ast\Visitor
21
 */
22
class ClassIntrospectionVisitor extends NodeVisitorAbstract
23
{
24
    /**
25
     * @var string
26
     */
27
    private $namespace;
28
29
    /**
30
     * @var string
31
     */
32
    private $class;
33
34
    /**
35
     * @var bool
36
     */
37
    private $final;
38
39
    /**
40
     * @var bool
41
     */
42
    private $abstract;
43
44
    /**
45
     * {@inheritDoc}
46
     *
47
     * Cleans up internal state
48
     *
49
     * @param array $nodes
50
     */
51
    public function beforeTraverse(array $nodes)
52
    {
53
        $this->namespace = null;
54
        $this->class = null;
55
        $this->final = false;
56
        $this->abstract = false;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @throws \RunOpenCode\AbstractBuilder\Exception\NotSupportedException
63
     */
64
    public function enterNode(Node $node)
65
    {
66
        if ($node instanceof Stmt\Namespace_) {
67
68
            if (null !== $this->namespace) {
69
                throw new NotSupportedException(sprintf('Multiple namespaces in single file are not supported.'));
70
            }
71
72
            $this->namespace = $node->name->toString();
73
        }
74
75
        if ($node instanceof Stmt\Class_) {
76
77
            if (null !== $this->class) {
78
                throw new NotSupportedException(sprintf('Multiple namespaces in single file are not supported.'));
79
            }
80
81
            $this->class = $node->name;
82
            $this->final = $node->isFinal();
83
            $this->abstract = $node->isAbstract();
84
        }
85
86
        if ($node instanceof Stmt\Property) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
87
88
        }
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getNamespace()
95
    {
96
        return $this->namespace;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getClass()
103
    {
104
        return $this->class;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function isFinal()
111
    {
112
        return $this->final;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function isAbstract()
119
    {
120
        return $this->abstract;
121
    }
122
}
123