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