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