Completed
Push — master ( 0647fd...b9e92e )
by Théo
27:07 queued 08:01
created

UseStmtPrefixer::enterNode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 3
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\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 $prefix;
34
    private $whitelist;
35
    private $reflector;
36
37 548
    public function __construct(string $prefix, Whitelist $whitelist, Reflector $reflector)
38
    {
39 548
        $this->prefix = $prefix;
40 548
        $this->reflector = $reflector;
41 548
        $this->whitelist = $whitelist;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 547
    public function enterNode(Node $node)
48
    {
49 547
        if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) {
50 202
            $node->name = Name::concat($this->prefix, $node->name, $node->name->getAttributes());
51
        }
52
53 547
        return $node;
54
    }
55
56 264
    private function shouldPrefixUseStmt(UseUse $use): bool
57
    {
58 264
        $useType = $this->findUseType($use);
59
60
        // If is already from the prefix namespace
61 264
        if ($this->prefix === $use->name->getFirst()) {
62 12
            return false;
63
        }
64
65
        // If is whitelisted
66 253
        if ($this->whitelist->belongsToWhitelistedNamespace((string) $use->name)) {
67 6
            return false;
68
        }
69
70 247
        if (Use_::TYPE_FUNCTION === $useType) {
71 21
            return false === $this->reflector->isFunctionInternal((string) $use->name);
72
        }
73
74 227
        if (Use_::TYPE_CONSTANT === $useType) {
75
            return
76 26
                false === $this->whitelist->isSymbolWhitelisted((string) $use->name)
77 26
                && false === $this->reflector->isConstantInternal((string) $use->name)
78
            ;
79
        }
80
81 202
        return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal((string) $use->name);
82
    }
83
84
    /**
85
     * Finds the type of the use statement.
86
     *
87
     * @param UseUse $use
88
     *
89
     * @return int See \PhpParser\Node\Stmt\Use_ type constants.
90
     */
91 264
    private function findUseType(UseUse $use): int
92
    {
93 264
        if (Use_::TYPE_UNKNOWN === $use->type) {
94
            /** @var Use_ $parentNode */
95 259
            $parentNode = ParentNodeAppender::getParent($use);
96
97 259
            return $parentNode->type;
98
        }
99
100 5
        return $use->type;
101
    }
102
}
103