UseStmtPrefixer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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\UseStmt;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender;
18
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
19
use Humbug\PhpScoper\Symbol\EnrichedReflector;
20
use PhpParser\Node;
21
use PhpParser\Node\Name;
22
use PhpParser\Node\Stmt\Use_;
23
use PhpParser\Node\Stmt\UseUse;
24
use PhpParser\NodeVisitorAbstract;
25
26
/**
27
 * Prefixes the use statements.
28
 *
29
 * @private
30
 */
31
final class UseStmtPrefixer extends NodeVisitorAbstract
32
{
33
    private string $prefix;
34
    private EnrichedReflector $enrichedReflector;
35
36
    public function __construct(
37 549
        string $prefix,
38
        EnrichedReflector $enrichedReflector
39 549
    ) {
40 549
        $this->prefix = $prefix;
41 549
        $this->enrichedReflector = $enrichedReflector;
42
    }
43
44
    public function enterNode(Node $node): Node
45
    {
46
        if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) {
47 548
            self::prefixStmt($node, $this->prefix);
48
        }
49 548
50 202
        return $node;
51
    }
52
53 548
    private function shouldPrefixUseStmt(UseUse $use): bool
54
    {
55
        $useType = self::findUseType($use);
56 264
        $nameString = $use->name->toString();
57
58 264
        $alreadyPrefixed = $this->prefix === $use->name->getFirst();
59
60
        if ($alreadyPrefixed) {
61 264
            return false;
62 12
        }
63
64
        if ($this->enrichedReflector->belongsToExcludedNamespace($nameString)) {
65
            return false;
66 253
        }
67 6
68
        if (Use_::TYPE_FUNCTION === $useType) {
69
            return !$this->enrichedReflector->isFunctionInternal($nameString);
70 247
        }
71 21
72
        if (Use_::TYPE_CONSTANT === $useType) {
73
            return !$this->enrichedReflector->isExposedConstant($nameString);
74 227
        }
75
76 26
        return Use_::TYPE_NORMAL !== $useType
77 26
            || !$this->enrichedReflector->isClassInternal($nameString);
78
    }
79
80
    private static function prefixStmt(UseUse $use, string $prefix): void
81 202
    {
82
        $previousName = $use->name;
83
84
        $prefixedName = Name::concat(
85
            $prefix,
86
            $use->name,
87
            $use->name->getAttributes(),
88
        );
89
90
        if (null === $prefixedName) {
91 264
            throw UnexpectedParsingScenario::create();
92
        }
93 264
94
        // Unlike the new (prefixed name), the previous name will not be
95 259
        // traversed hence we need to manually set its parent attribute
96
        ParentNodeAppender::setParent($previousName, $use);
97 259
        UseStmtManipulator::setOriginalName($use, $previousName);
98
99
        $use->name = $prefixedName;
100 5
    }
101
102
    /**
103
     * Finds the type of the use statement.
104
     *
105
     * @return int See \PhpParser\Node\Stmt\Use_ type constants.
106
     */
107
    private static function findUseType(UseUse $use): int
108
    {
109
        if (Use_::TYPE_UNKNOWN === $use->type) {
110
            /** @var Use_ $parentNode */
111
            $parentNode = ParentNodeAppender::getParent($use);
112
113
            return $parentNode->type;
114
        }
115
116
        return $use->type;
117
    }
118
}
119