Passed
Push — master ( fa352d...0eb7bd )
by BruceScrutinizer
02:09
created

NamespaceVisitor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A processNode() 0 29 4
A configure() 0 13 1
A enterNode() 0 4 1
A getStoreProcessedResult() 0 3 1
A clearStoredProcessedResult() 0 3 1
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;
0 ignored issues
show
introduced by
The private property $a is not used, and could be removed.
Loading history...
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 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $additionalInformation is dead and can be removed.
Loading history...
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