Passed
Push — master ( 121153...da5866 )
by Théo
04:08 queued 02:04
created

UseStmtName::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 function count;
12
use function get_class;
13
use function Safe\sprintf;
14
15
final class UseStmtName
16
{
17
    private Name $name;
18
19
    public function __construct(Name $name)
20
    {
21
        $this->name = $name;
22
    }
23
24
    public function contains(Name $resolvedName): bool
25
    {
26
        return self::arrayStartsWith(
27
            $resolvedName->parts,
28
            $this->name->parts,
29
        );
30
    }
31
32
    /**
33
     * @param string[] $array
34
     * @param string[] $start
35
     */
36
    private static function arrayStartsWith(array $array, array $start): bool
37
    {
38
        $prefixLength = count($start);
39
40
        for ($index = 0; $index < $prefixLength; ++$index) {
41
            if (!isset($array[$index]) || $array[$index] !== $start[$index]) {
42
                return false;
43
            }
44
        }
45
46
        return true;
47
    }
48
49
    /**
50
     * @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...
51
     */
52
    public function getUseStmtAliasAndType(): array
53
    {
54
        $use = self::getUseNode($this->name);
55
        $useParent = self::getUseParentNode($use);
56
57
        $alias = $use->alias;
58
59
        if (null !== $alias) {
60
            $alias = (string) $alias;
61
        }
62
63
        return [
64
            $alias,
65
            $useParent->type,
66
        ];
67
    }
68
69
    private static function getUseNode(Name $name): UseUse
70
    {
71
        $use = ParentNodeAppender::getParent($name);
72
73
        if ($use instanceof UseUse) {
74
            return $use;
75
        }
76
77
        // @codeCoverageIgnoreStart
78
        throw new UnexpectedParsingScenario(
79
            sprintf(
80
                'Unexpected use statement name parent "%s"',
81
                get_class($use),
82
            ),
83
        );
84
        // @codeCoverageIgnoreEnd
85
    }
86
87
    private static function getUseParentNode(UseUse $use): Use_
88
    {
89
        $useParent = ParentNodeAppender::getParent($use);
90
91
        if ($useParent instanceof Use_) {
92
            return $useParent;
93
        }
94
95
        // @codeCoverageIgnoreStart
96
        throw new UnexpectedParsingScenario(
97
            sprintf(
98
                'Unexpected UseUse parent "%s"',
99
                get_class($useParent),
100
            ),
101
        );
102
        // @codeCoverageIgnoreEnd
103
    }
104
}
105