Passed
Push — master ( 62af2e...d65fca )
by Théo
07:27 queued 04:04
created

FullyQualifiedNameResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveName() 0 16 2
C resolveNodeName() 0 26 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\AppendParentNode;
18
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\NamespaceStmtCollection;
19
use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\UseStmtCollection;
20
use Humbug\PhpScoper\PhpParser\NodeVisitor\NameStmtPrefixer;
21
use PhpParser\Node\Expr\ConstFetch;
22
use PhpParser\Node\Expr\FuncCall;
23
use PhpParser\Node\Name;
24
use PhpParser\Node\Name\FullyQualified;
25
use function in_array;
26
27
/**
28
 * Attempts to resolve the node name into a fully qualified node. Returns a valid (non fully-qualified) name node on
29
 * failure.
30
 *
31
 * @private
32
 */
33
final class FullyQualifiedNameResolver
34
{
35
    private $namespaceStatements;
36
    private $useStatements;
37
38 371
    public function __construct(NamespaceStmtCollection $namespaceStatements, UseStmtCollection $useStatements)
39
    {
40 371
        $this->namespaceStatements = $namespaceStatements;
41 371
        $this->useStatements = $useStatements;
42
    }
43
44 275
    public function resolveName(Name $node): ResolvedValue
45
    {
46 275
        if ($node instanceof FullyQualified) {
47 140
            return new ResolvedValue($node, null, null);
48
        }
49
50 190
        $namespaceName = $this->namespaceStatements->findNamespaceForNode($node);
51
52 190
        $useName = $this->useStatements->findStatementForNode($namespaceName, $node);
53
54 190
        return new ResolvedValue(
55 190
            $this->resolveNodeName($node, $namespaceName, $useName),
56 190
            $namespaceName,
57 190
            $useName
58
        );
59
    }
60
61 190
    private function resolveNodeName(Name $name, ?Name $namespace, ?Name $use): Name
62
    {
63 190
        if (null !== $use) {
64 97
            return FullyQualified::concat($use, $name->slice(1), $name->getAttributes());
65
        }
66
67 123
        if (null === $namespace) {
68 62
            return new FullyQualified($name, $name->getAttributes());
69
        }
70
71 77
        if (in_array((string) $name, NameStmtPrefixer::PHP_FUNCTION_KEYWORDS, true)) {
72 1
            return $name;
73
        }
74
75 77
        $parentNode = AppendParentNode::getParent($name);
76
77
        if (
78 77
            ($parentNode instanceof ConstFetch || $parentNode instanceof FuncCall)
79 77
            && 1 === count($name->parts)
80
        ) {
81
            // Ambiguous name, cannot determine the FQ name
82 58
            return $name;
83
        }
84
85 28
        return FullyQualified::concat($namespace, $name, $name->getAttributes());
86
    }
87
}
88