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