Completed
Push — master ( acc9c4...a30c3e )
by BruceScrutinizer
06:36
created

PhpNode::isFalsePositives()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
nc 5
nop 1
dl 0
loc 21
rs 9.6111
c 2
b 0
f 1
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 NamespaceProtector\Config\Config;
9
use PhpParser\Node\Name\FullyQualified;
10
use PhpParser\NodeVisitor\NameResolver;
11
use NamespaceProtector\Result\ErrorResult;
12
use NamespaceProtector\Event\EventDispatcher;
13
use NamespaceProtector\Event\ListenerProvider;
14
use NamespaceProtector\Result\ResultCollector;
15
use NamespaceProtector\EnvironmentDataLoaderInterface;
16
use NamespaceProtector\Parser\Node\Event\FoundUseNamespace;
17
18
final class PhpNode extends NameResolver
19
{
20
    public const ERR = 1;
21
22
    /** @var array<Callable> */
23
    private $listNodeProcessor;
24
25
    /** @var ResultCollector  */
26
    private $resultCollector;
27
28
    /**
29
     * @param array<string,mixed> $configParser
30
     */
31
    public function __construct(
32
        Config $configGlobal,
33
        array $configParser,
34
        ResultCollector $resultCollector,
35
        EnvironmentDataLoaderInterface $metadataLoader
36
    ) {
37
        parent::__construct(null, $configParser);
38
39
        $this->resultCollector = $resultCollector;
40
41
        $listener = new ListenerProvider();
42
        $callableUseStatement = new ProcessUseStatement($metadataLoader, $configGlobal);
43
        $listener->addEventListener(FoundUseNamespace::class, $callableUseStatement);
44
        $dispatcher = new EventDispatcher($listener);
45
46
        $this->listNodeProcessor[UseUse::class] = static function (Node $node) use ($dispatcher) {
47
            /** @var UseUse $node */
48
            return $dispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->name->toCodeString()));
49
        };
50
51
        $this->listNodeProcessor[FullyQualified::class] = static function (Node $node) use ($dispatcher) {
52
            /** @var FullyQualified $node */
53
            return $dispatcher->dispatch(new FoundUseNamespace($node->getStartLine(), $node->toCodeString()));
54
        };
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
            $val = new Entry($resultProcessNode->getNodeName());
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