Passed
Push — master ( b63dcc...508f71 )
by Théo
02:30
created

IdentifierNameAppender::enterNode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 17
rs 10
c 1
b 0
f 0
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;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
18
use PhpParser\Node;
19
use PhpParser\Node\Stmt\Class_;
20
use PhpParser\Node\Stmt\Interface_;
21
use PhpParser\NodeVisitorAbstract;
22
23
/**
24
 * In some contexts we need to resolve identifiers but they can no longer be
25
 * resolved on the fly. For those, we store the resolved identifier as an
26
 * attribute.
27
 *
28
 * @see ClassAliasStmtAppender
29
 *
30
 * @private
31
 */
32
final class IdentifierNameAppender extends NodeVisitorAbstract
33
{
34
    private IdentifierResolver $identifierResolver;
35
36
    public function __construct(IdentifierResolver $identifierResolver)
37
    {
38
        $this->identifierResolver = $identifierResolver;
39
    }
40
41
    public function enterNode(Node $node): ?Node
42
    {
43
        if (!($node instanceof Class_ || $node instanceof Interface_)) {
44
            return null;
45
        }
46
47
        $name = $node->name;
48
49
        if (null === $name) {
50
            return null;
51
        }
52
53
        $resolvedName = $this->identifierResolver->resolveIdentifier($name);
54
55
        $name->setAttribute('resolvedName', $resolvedName);
56
57
        return null;
58
    }
59
}
60