Completed
Push — master ( 71f966...a7254e )
by BruceScrutinizer
08:15
created

PhpNode::pushError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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