1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NamespaceProtector\Parser\Node; |
6
|
|
|
|
7
|
|
|
use PhpParser\Node; |
8
|
|
|
use PhpParser\Node\Stmt\UseUse; |
9
|
|
|
use PhpParser\Node\Name\FullyQualified; |
10
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
11
|
|
|
use NamespaceProtector\Result\ErrorResult; |
12
|
|
|
use NamespaceProtector\Result\ResultCollected; |
13
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use NamespaceProtector\Parser\Node\Event\FoundUseNamespace; |
15
|
|
|
use NamespaceProtector\Result\ResultCollectedReadable; |
16
|
|
|
|
17
|
|
|
final class NamespaceVisitor extends NameResolver implements NamespaceProtectorVisitorInterface |
18
|
|
|
{ |
19
|
|
|
public const ERR = 1; |
20
|
|
|
|
21
|
|
|
/** @var array<Callable> */ |
22
|
|
|
private $listNodeProcessor; |
23
|
|
|
|
24
|
|
|
/** @var ResultCollected */ |
25
|
|
|
private $storeProcessNodeResult; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array<string,mixed> $configParser |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
array $configParser, |
32
|
|
|
EventDispatcherInterface $eventDispatcher |
33
|
|
|
) { |
34
|
|
|
parent::__construct(null, $configParser); |
35
|
|
|
$this->configure($eventDispatcher); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function configure(EventDispatcherInterface $eventDispatcher): void |
39
|
|
|
{ |
40
|
|
|
$this->listNodeProcessor[UseUse::class] = static function (Node $node) use ($eventDispatcher) { |
41
|
|
|
/** @var UseUse $node */ |
42
|
|
|
return $eventDispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->name->toCodeString())); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
$this->listNodeProcessor[FullyQualified::class] = static function (Node $node) use ($eventDispatcher) { |
46
|
|
|
/** @var FullyQualified $node */ |
47
|
|
|
return $eventDispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->toCodeString())); |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
$this->storeProcessNodeResult = new ResultCollected(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function enterNode(Node $node) |
54
|
|
|
{ |
55
|
|
|
$this->processNode($node); |
56
|
|
|
return $node; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function processNode(Node $node): void |
60
|
|
|
{ |
61
|
|
|
$class = \get_class($node); |
62
|
|
|
if (!isset($this->listNodeProcessor[$class])) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$func = $this->listNodeProcessor[$class]; |
67
|
|
|
|
68
|
|
|
/** @var FoundUseNamespace */ |
69
|
|
|
$resultProcessNode = $func($node); |
70
|
|
|
|
71
|
|
|
if (!$resultProcessNode->withError()) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$additionalInformation = ''; |
|
|
|
|
76
|
|
|
if ($resultProcessNode->getAdditionalInformation() !== '') { |
77
|
|
|
$additionalInformation = '( ' . $resultProcessNode->getAdditionalInformation() . ' )'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$err = new ErrorResult( |
81
|
|
|
$node->getLine(), |
82
|
|
|
$resultProcessNode->getNodeName(), |
83
|
|
|
self::ERR |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->storeProcessNodeResult->addResult($err); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getStoreProcessNodeResult(): ResultCollectedReadable |
90
|
|
|
{ |
91
|
|
|
return new ResultCollectedReadable($this->storeProcessNodeResult); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|