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

UseStmtCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
B findStatementForName() 0 16 6
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 IteratorAggregate;
19
use PhpParser\Node\Name;
20
use PhpParser\Node\Stmt\Use_;
21
use PhpParser\Node\Stmt\UseUse;
22
use function Humbug\PhpScoper\deep_clone;
23
24
final class UseStmtCollection implements IteratorAggregate
25
{
26
    /**
27
     * @var Use_[]
28
     */
29
    private $nodes = [];
30
31
    public function add(Use_ $node)
32
    {
33
        $this->nodes[] = deep_clone($node);
34
    }
35
36
    /**
37
     * Finds the statements matching the given name.
38
     *
39
     * $name = 'Foo';
40
     *
41
     * use X;
42
     * use Bar\Foo;
43
     * use Y;
44
     *
45
     * will return the use statement for `Bar\Foo`.
46
     *
47
     * @param string $name
48
     *
49
     * @return null|Name
50
     */
51
    public function findStatementForName(string $name): ?Name
52
    {
53
        foreach ($this->nodes as $use_) {
54
            foreach ($use_->uses as $useStatement) {
55
                if ($useStatement instanceof UseUse) {
56
                    if ($name === $useStatement->alias || $name === $useStatement->name->getLast()) {
57
                        return $useStatement->name;
58
                    }
59
                }
60
61
                //TODO
62
            }
63
        }
64
65
        return null;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function getIterator()
72
    {
73
        return new ArrayIterator($this->nodes);
74
    }
75
}
76