-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetNamespace.php
More file actions
44 lines (34 loc) · 1.01 KB
/
SetNamespace.php
File metadata and controls
44 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace RonasIT\Larabuilder\Visitors;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeVisitorAbstract;
class SetNamespace extends NodeVisitorAbstract
{
public function __construct(
protected string $namespace,
) {
}
public function afterTraverse(array $nodes): ?array
{
$declares = [];
foreach ($nodes as $key => $node) {
if ($node instanceof Namespace_) {
return $this->updateNamespace($node);
}
if ($node instanceof Declare_) {
$declares[] = $node;
unset($nodes[$key]);
}
}
return [...$declares, new Namespace_(new Name($this->namespace), array_values($nodes))];
}
protected function updateNamespace(Namespace_ $node): ?array
{
if ($node->name->toString() !== $this->namespace) {
$node->name = new Name($this->namespace);
}
return null;
}
}