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