Completed
Push — master ( 28987c...71f966 )
by BruceScrutinizer
01:51
created

PhpNode::configureEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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