Completed
Push — master ( dcddf7...d2fb4d )
by Théo
05:16 queued 03:22
created

NamespaceStmtCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A getNamespaceName() 0 12 3
A count() 0 4 1
A getIterator() 0 4 1
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 ArrayIterator;
18
use Countable;
19
use InvalidArgumentException;
20
use IteratorAggregate;
21
use PhpParser\Node\Name;
22
use PhpParser\Node\Stmt\Namespace_;
23
use function Humbug\PhpScoper\deep_clone;
24
25
final class NamespaceStmtCollection implements IteratorAggregate, Countable
26
{
27
    /**
28
     * @var Namespace_[]
29
     */
30
    private $nodes = [];
31
32
    public function add(Namespace_ $node)
33
    {
34
        $this->nodes[] = deep_clone($node);
35
    }
36
37
    public function getNamespaceName(): Name
38
    {
39
        if (0 === count($this->nodes)) {
40
            throw new InvalidArgumentException('No name can be given: no namespace found.');
41
        }
42
43
        if (1 < count($this->nodes)) {
44
            throw new InvalidArgumentException('No name can be given: more than one namespace found.');
45
        }
46
47
        return $this->nodes[0]->name;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function count(): int
54
    {
55
        return count($this->nodes);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getIterator()
62
    {
63
        return new ArrayIterator($this->nodes);
64
    }
65
}
66