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\ResultCollector; |
13
|
|
|
use NamespaceProtector\Parser\Node\Event\FoundUseNamespace; |
14
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
final class NamespaceVisitor extends NameResolver |
17
|
|
|
{ |
18
|
|
|
public const ERR = 1; |
19
|
|
|
|
20
|
|
|
/** @var array<Callable> */ |
21
|
|
|
private $listNodeProcessor; |
22
|
|
|
|
23
|
|
|
/** @var ResultCollector */ |
24
|
|
|
private $resultCollector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array<string,mixed> $configParser |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
array $configParser, |
31
|
|
|
ResultCollector $resultCollector, |
32
|
|
|
EventDispatcherInterface $eventDispatcher |
33
|
|
|
) { |
34
|
|
|
parent::__construct(null, $configParser); |
35
|
|
|
|
36
|
|
|
$this->resultCollector = $resultCollector; |
37
|
|
|
$this->configureEvents($eventDispatcher); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function configureEvents(EventDispatcherInterface $eventDispatcher): void |
41
|
|
|
{ |
42
|
|
|
$this->listNodeProcessor[UseUse::class] = static function (Node $node) use ($eventDispatcher) { |
43
|
|
|
/** @var UseUse $node */ |
44
|
|
|
return $eventDispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->name->toCodeString())); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$this->listNodeProcessor[FullyQualified::class] = static function (Node $node) use ($eventDispatcher) { |
48
|
|
|
/** @var FullyQualified $node */ |
49
|
|
|
return $eventDispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->toCodeString())); |
50
|
|
|
}; |
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
|
|
|
|
69
|
|
|
/** @var FoundUseNamespace */ |
70
|
|
|
$resultProcessNode = $func($node); |
71
|
|
|
|
72
|
|
|
if (!$resultProcessNode->withError()) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$additionalInformation = ''; |
77
|
|
|
if ($resultProcessNode->getAdditionalInformation() !== '') { |
78
|
|
|
$additionalInformation = '( ' . $resultProcessNode->getAdditionalInformation() . ' )'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$err = new ErrorResult( |
82
|
|
|
$node->getLine(), |
83
|
|
|
$resultProcessNode->getNodeName() . $additionalInformation . \PHP_EOL, |
84
|
|
|
self::ERR |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->resultCollector->addResult($err); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|