Passed
Pull Request — master (#502)
by Théo
01:48
created

UseStmtPrefixer::prefixStmt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 24
ccs 5
cts 5
cp 1
crap 2
rs 9.8666
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\UseStmt;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\ParentNodeAppender;
18
use Humbug\PhpScoper\Reflector;
19
use Humbug\PhpScoper\Whitelist;
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 Whitelist $whitelist;
35
    private Reflector $reflector;
36
37 549
    public function __construct(string $prefix, Whitelist $whitelist, Reflector $reflector)
38
    {
39 549
        $this->prefix = $prefix;
40 549
        $this->reflector = $reflector;
41 549
        $this->whitelist = $whitelist;
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
57
        // If is already from the prefix namespace
58 264
        if ($this->prefix === $use->name->getFirst()) {
59
            return false;
60
        }
61 264
62 12
        // If is whitelisted
63
        if ($this->whitelist->belongsToWhitelistedNamespace((string) $use->name)) {
64
            return false;
65
        }
66 253
67 6
        if (Use_::TYPE_FUNCTION === $useType) {
68
            return false === $this->reflector->isFunctionInternal((string) $use->name);
69
        }
70 247
71 21
        if (Use_::TYPE_CONSTANT === $useType) {
72
            return self::shouldPrefixConstantUseStmt(
73
                $use->name->toString(),
74 227
                $this->whitelist,
75
                $this->reflector,
76 26
            );
77 26
        }
78
79
        return Use_::TYPE_NORMAL !== $useType
80
            || !$this->reflector->isClassInternal((string) $use->name);
81 202
    }
82
83
    private static function prefixStmt(UseUse $use, string $prefix): void
84
    {
85
        $previousName = $use->name;
86
87
        $prefixedName = Name::concat(
88
            $prefix,
89
            $use->name,
90
            $use->name->getAttributes(),
91 264
        );
92
93 264
        if (null === $prefixedName) {
94
            return;
95 259
        }
96
97 259
        // TODO: move this to ParentNodeAppender or Manipulator
98
        // Unlike the new (prefixed name), the previous name will not be
99
        // traversed hence we need to manually set its parent attribute
100 5
        $previousName->setAttribute(
101
            ParentNodeAppender::PARENT_ATTRIBUTE,
102
            $use,
103
        );
104
        UseStmtManipulator::setOriginalName($use, $previousName);
105
106
        $use->name = $prefixedName;
107
    }
108
109
    private static function shouldPrefixConstantUseStmt(
110
        string $name,
111
        Whitelist $whitelist,
112
        Reflector $reflector
113
    ): bool {
114
        return !$whitelist->isGlobalWhitelistedConstant($name)
115
            && !$whitelist->isSymbolWhitelisted($name)
116
            && !$reflector->isConstantInternal($name);
117
    }
118
119
    /**
120
     * Finds the type of the use statement.
121
     *
122
     * @param UseUse $use
123
     *
124
     * @return int See \PhpParser\Node\Stmt\Use_ type constants.
125
     */
126
    private static function findUseType(UseUse $use): int
127
    {
128
        if (Use_::TYPE_UNKNOWN === $use->type) {
129
            /** @var Use_ $parentNode */
130
            $parentNode = ParentNodeAppender::getParent($use);
131
132
            return $parentNode->type;
133
        }
134
135
        return $use->type;
136
    }
137
}
138