Passed
Pull Request — master (#565)
by Théo
02:31
created

UseStmtPrefixer::shouldPrefixConstantUseStmt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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