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

UseStmtName::getUseParentNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\PhpParser;
6
7
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender;
8
use PhpParser\Node\Name;
9
use PhpParser\Node\Stmt\Use_;
10
use PhpParser\Node\Stmt\UseUse;
11
use UnexpectedValueException;
12
use function count;
13
use function get_class;
14
use function Safe\sprintf;
15
16
final class UseStmtName
17
{
18
    private Name $name;
19
20
    public function __construct(Name $name)
21
    {
22
        $this->name = $name;
23
    }
24
25
    public function contains(Name $resolvedName): bool
26
    {
27
        return self::arrayStartsWith(
28
            $resolvedName->parts,
29
            $this->name->parts,
30
        );
31
    }
32
33
    /**
34
     * @param string[] $array
35
     * @param string[] $start
36
     */
37
    private static function arrayStartsWith(array $array, array $start): bool
38
    {
39
        $prefixLength = count($start);
40
41
        for ($index = 0; $index < $prefixLength; ++$index) {
42
            if (!isset($array[$index]) || $array[$index] !== $start[$index]) {
43
                return false;
44
            }
45
        }
46
47
        return true;
48
    }
49
50
    /**
51
     * @return array{string|null, Use_::TYPE_*}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string|null, Use_::TYPE_*} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
52
     */
53
    public function getUseStmtAliasAndType(): array
54
    {
55
        $use = self::getUseNode($this->name);
56
        $useParent = self::getUseParentNode($use);
57
58
        $alias = $use->alias;
59
60
        if (null !== $alias) {
61
            $alias = (string) $alias;
62
        }
63
64
        return [
65
            $alias,
66
            $useParent->type,
67
        ];
68
    }
69
70
    private static function getUseNode(Name $name): UseUse
71
    {
72
        $use = ParentNodeAppender::getParent($name);
73
74
        if ($use instanceof UseUse) {
75
            return $use;
76
        }
77
78
        // @codeCoverageIgnoreStart
79
        throw new UnexpectedValueException(
80
            sprintf(
81
                'Unexpected use statement name parent "%s"',
82
                get_class($use),
83
            ),
84
        );
85
        // @codeCoverageIgnoreEnd
86
    }
87
88
    private static function getUseParentNode(UseUse $use): Use_
89
    {
90
        $useParent = ParentNodeAppender::getParent($use);
91
92
        if ($useParent instanceof Use_) {
93
            return $useParent;
94
        }
95
96
        // @codeCoverageIgnoreStart
97
        throw new UnexpectedValueException(
98
            sprintf(
99
                'Unexpected UseUse parent "%s"',
100
                get_class($useParent),
101
            ),
102
        );
103
        // @codeCoverageIgnoreEnd
104
    }
105
}
106