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\NodeVisitor; |
16
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\NamespaceStmtCollection; |
18
|
|
|
use PhpParser\Node; |
19
|
|
|
use PhpParser\Node\Name; |
20
|
|
|
use PhpParser\Node\Stmt\Class_; |
21
|
|
|
use PhpParser\Node\Stmt\Interface_; |
22
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
23
|
|
|
use PhpParser\NodeVisitorAbstract; |
24
|
|
|
use function Humbug\PhpScoper\clone_node; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prefixes the relevant namespaces. |
28
|
|
|
* |
29
|
|
|
* ``` |
30
|
|
|
* namespace Foo; |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* => |
34
|
|
|
* |
35
|
|
|
* ``` |
36
|
|
|
* namespace Humbug\Foo; |
37
|
|
|
* ``` |
38
|
|
|
* |
39
|
|
|
* @private |
40
|
|
|
*/ |
41
|
|
|
final class NamespaceStmtPrefixer extends NodeVisitorAbstract |
42
|
|
|
{ |
43
|
|
|
private $prefix; |
44
|
|
|
private $namespaceStatements; |
45
|
|
|
|
46
|
371 |
|
public function __construct(string $prefix, NamespaceStmtCollection $namespaceStatements) |
47
|
|
|
{ |
48
|
371 |
|
$this->prefix = $prefix; |
49
|
371 |
|
$this->namespaceStatements = $namespaceStatements; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
370 |
|
public function enterNode(Node $node): Node |
56
|
|
|
{ |
57
|
370 |
|
return ($node instanceof Namespace_) |
58
|
368 |
|
? $this->prefixNamespaceStmt($node) |
59
|
370 |
|
: $node |
60
|
|
|
; |
61
|
|
|
} |
62
|
|
|
|
63
|
368 |
|
private function prefixNamespaceStmt(Namespace_ $namespace): Node |
64
|
|
|
{ |
65
|
368 |
|
$originalNamespace = $namespace; |
66
|
|
|
|
67
|
368 |
|
if ($this->shouldPrefixStmt($namespace)) { |
68
|
365 |
|
$originalNamespace = clone_node($namespace); |
69
|
|
|
|
70
|
365 |
|
$namespace->name = Name::concat($this->prefix, $namespace->name); |
71
|
|
|
} |
72
|
|
|
|
73
|
368 |
|
$this->namespaceStatements->add($namespace, $originalNamespace); |
|
|
|
|
74
|
|
|
|
75
|
368 |
|
return $namespace; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function isWhitelistedNode(Node $node) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (($node instanceof Class_ || $node instanceof Interface_)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Check nodes in the global namespaces. |
85
|
|
|
if ($node instanceof Namespace_ && null === $node->name) { |
86
|
|
|
foreach ($node->stmts as $statement) { |
87
|
|
|
if ($this->isWhitelistedNode($statement)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
368 |
|
private function shouldPrefixStmt(Namespace_ $namespace): bool |
97
|
|
|
{ |
98
|
368 |
|
return null === $namespace->name || (null !== $namespace->name && $this->prefix !== $namespace->name->getFirst()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.