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

RootNamespaceNormalizer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 21
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A beforeTraverse() 0 15 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