Passed
Pull Request — master (#648)
by Théo
02:17
created

NamespaceStmtCollection::findNamespaceForNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\PhpParser\NodeVisitor\NamespaceStmt;
16
17
use ArrayIterator;
18
use Countable;
19
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender;
20
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
21
use IteratorAggregate;
22
use PhpParser\Node;
23
use PhpParser\Node\Name;
24
use PhpParser\Node\Stmt\Namespace_;
25
use Traversable;
26
27
use function count;
28
use function end;
29
30
/**
31
 * Utility class collecting all the namespaces for the scoped files allowing to easily find the namespace to which
32
 * belongs a node.
33
 *
34
 * @private
35
 */
36
final class NamespaceStmtCollection implements IteratorAggregate, Countable
37
{
38
    /**
39
     * @var Namespace_[]
40
     */
41
    private array $nodes = [];
42
43
    /**
44
     * @var (Name|null)[] Associative array with the potentially prefixed namespace names as keys and their original name
45
     *                    as value.
46
     */
47
    private array $mapping = [];
48
49
    /**
50
     * @param Namespace_ $namespace New namespace, may have been prefixed.
51
     */
52
    public function add(Namespace_ $namespace): void
53
    {
54
        $this->nodes[] = $namespace;
55
56
        $this->mapping[(string) $namespace->name] = NamespaceManipulator::getOriginalName($namespace);
57
    }
58
59
    public function findNamespaceForNode(Node $node): ?Name
60
    {
61
        if (0 === count($this->nodes)) {
62
            return null;
63
        }
64
65
        // Shortcut if there is only one namespace
66
        if (1 === count($this->nodes)) {
67
            return NamespaceManipulator::getOriginalName($this->nodes[0]);
68
        }
69
70
        return $this->getNodeNamespaceName($node);
71
    }
72
73
    public function getCurrentNamespaceName(): ?Name
74
    {
75
        $lastNode = end($this->nodes);
76
77
        return false === $lastNode ? null : NamespaceManipulator::getOriginalName($lastNode);
78
    }
79
80
    public function count(): int
81
    {
82
        return count($this->nodes);
83
    }
84
85
    private function getNodeNamespaceName(Node $node): ?Name
86
    {
87
        if (!ParentNodeAppender::hasParent($node)) {
88
            throw UnexpectedParsingScenario::create();
89
        }
90
91
        $parentNode = ParentNodeAppender::getParent($node);
92
93
        if ($parentNode instanceof Namespace_) {
94
            return $this->mapping[(string) $parentNode->name];
95
        }
96
97
        return $this->getNodeNamespaceName($parentNode);
98
    }
99
100
    public function getIterator(): Traversable
101
    {
102
        return new ArrayIterator($this->nodes);
103
    }
104
}
105