Passed
Push — master ( 01e41e...21756f )
by Théo
02:37
created

NamespaceStmtPrefixer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 16
cts 24
cp 0.6667
wmc 15
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A enterNode() 0 7 2
A prefixNamespaceStmt() 0 14 2
B isWhitelistedNode() 0 17 7
A shouldPrefixStmt() 0 4 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\NodeVisitor;
16
17
use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection;
18
use PhpParser\Node;
19
use PhpParser\Node\Name;
20
use PhpParser\Node\Stmt\Class_;
21
use PhpParser\Node\Stmt\Interface_;
22
use PhpParser\Node\Stmt\Namespace_;
23
use PhpParser\NodeVisitorAbstract;
24
use function Humbug\PhpScoper\clone_node;
25
26
/**
27
 * Prefixes the relevant namespaces.
28
 *
29
 * ```
30
 * namespace Foo;
31
 * ```
32
 *
33
 * =>
34
 *
35
 * ```
36
 * namespace Humbug\Foo;
37
 * ```
38
 */
39
final class NamespaceStmtPrefixer extends NodeVisitorAbstract
40
{
41
    private $prefix;
42
    private $namespaceStatements;
43
44 338
    public function __construct(string $prefix, NamespaceStmtCollection $namespaceStatements)
45
    {
46 338
        $this->prefix = $prefix;
47 338
        $this->namespaceStatements = $namespaceStatements;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 337
    public function enterNode(Node $node): Node
54
    {
55 337
        return ($node instanceof Namespace_)
56 335
            ? $this->prefixNamespaceStmt($node)
57 337
            : $node
58
        ;
59
    }
60
61 335
    private function prefixNamespaceStmt(Namespace_ $namespace): Node
62
    {
63 335
        $originalNamespace = $namespace;
64
65 335
        if ($this->shouldPrefixStmt($namespace)) {
66 332
            $originalNamespace = clone_node($namespace);
67
68 332
            $namespace->name = Name::concat($this->prefix, $namespace->name);
69
        }
70
71 335
        $this->namespaceStatements->add($namespace, $originalNamespace);
72
73 335
        return $namespace;
74
    }
75
76
    private function isWhitelistedNode(Node $node)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
77
    {
78
        if (($node instanceof Class_ || $node instanceof Interface_)) {
79
            return true;
80
        }
81
82
        // Check nodes in the global namespaces.
83
        if ($node instanceof Namespace_ && null === $node->name) {
84
            foreach ($node->stmts as $statement) {
85
                if ($this->isWhitelistedNode($statement)) {
86
                    return true;
87
                }
88
            }
89
        }
90
91
        return false;
92
    }
93
94 335
    private function shouldPrefixStmt(Namespace_ $namespace): bool
95
    {
96 335
        return null === $namespace->name || (null !== $namespace->name && $this->prefix !== $namespace->name->getFirst());
97
    }
98
}
99