Completed
Push — master ( fdee56...483c4c )
by Alexander
02:07
created

RootNamespaceNormalizer::beforeTraverse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Parser Reflection API
4
 *
5
 * @copyright Copyright 2016, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\ParserReflection\NodeVisitor;
12
13
use PhpParser\Node;
14
use PhpParser\Node\Name\FullyQualified;
15
use PhpParser\Node\Stmt\Namespace_;
16
use PhpParser\NodeVisitorAbstract;
17
18
/**
19
 * Visitor to normalize the root namespace for the files without the namespace (root namespace)
20
 *
21
 * File->Namespace->Statements
22
 */
23
class RootNamespaceNormalizer extends NodeVisitorAbstract
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 9
    public function beforeTraverse(array $nodes)
29
    {
30
        // namespaces can be only top-level nodes, so we can scan them directly
31 9
        foreach ($nodes as $topLevelNode) {
32 9
            if ($topLevelNode instanceof Namespace_) {
33
                // file has namespace in it, nothing to change, returning null
34 9
                return null;
35
            }
36
        }
37
38
        // if we don't have a namespaces at all, this is global namespace, wrap everything in it
39 1
        $globalNamespaceNode = new Namespace_(new FullyQualified(''), $nodes);
40
41 1
        return [$globalNamespaceNode];
42
    }
43
}
44