Passed
Pull Request — master (#506)
by Théo
02:19
created

IdentifierNameAppender::enterNode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
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\Node\ClassAliasFuncCall;
18
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory;
19
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
20
use Humbug\PhpScoper\Whitelist;
21
use PhpParser\Node;
22
use PhpParser\Node\Name;
23
use PhpParser\Node\Name\FullyQualified;
24
use PhpParser\Node\Stmt;
25
use PhpParser\Node\Stmt\Class_;
26
use PhpParser\Node\Stmt\Expression;
27
use PhpParser\Node\Stmt\Interface_;
28
use PhpParser\Node\Stmt\Namespace_;
29
use PhpParser\NodeVisitor\NameResolver;
30
use PhpParser\NodeVisitorAbstract;
31
use function array_reduce;
32
33
/**
34
 * In some contexts we need to resolve identifiers but they can no longer be
35
 * resolved on the fly. For those, we store the resolved identifier as an
36
 * attribute.
37
 *
38
 * @see ClassAliasStmtAppender
39
 *
40
 * @private
41
 */
42
final class IdentifierNameAppender extends NodeVisitorAbstract
43
{
44
    private IdentifierResolver $identifierResolver;
45
46
    public function __construct(IdentifierResolver $identifierResolver)
47
    {
48
        $this->identifierResolver = $identifierResolver;
49
    }
50
51
    public function enterNode(Node $node): void
52
    {
53
        if (!($node instanceof Class_ || $node instanceof Interface_)) {
54
            return;
55
        }
56
57
        $name = $node->name;
58
59
        if (null === $name) {
60
            return;
61
        }
62
63
        $resolvedName = $this->identifierResolver->resolveIdentifier($node->name);
0 ignored issues
show
Bug introduced by
It seems like $node->name can also be of type null; however, parameter $identifier of Humbug\PhpScoper\PhpPars...er::resolveIdentifier() does only seem to accept PhpParser\Node\Identifier, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $resolvedName = $this->identifierResolver->resolveIdentifier(/** @scrutinizer ignore-type */ $node->name);
Loading history...
64
65
        $name->setAttribute('resolvedName', $resolvedName);
66
    }
67
}
68