Passed
Push — master ( 3749c1...b0239f )
by Théo
02:58
created

UseStmtPrefixer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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\AppendParentNode;
18
use Humbug\PhpScoper\Reflector;
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 $prefix;
33
    private $reflector;
34
35 422
    public function __construct(string $prefix, Reflector $reflector)
36
    {
37 422
        $this->prefix = $prefix;
38 422
        $this->reflector = $reflector;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 421
    public function enterNode(Node $node)
45
    {
46 421
        if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) {
47 191
            $node->name = Name::concat($this->prefix, $node->name, $node->name->getAttributes());
48
        }
49
50 421
        return $node;
51
    }
52
53 240
    private function shouldPrefixUseStmt(UseUse $use): bool
54
    {
55 240
        $useType = $this->findUseType($use);
56
57
        // If is already from the prefix namespace
58 240
        if ($this->prefix === $use->name->getFirst()) {
59 11
            return false;
60
        }
61
62 229
        if (Use_::TYPE_FUNCTION === $useType) {
63 17
            return false === $this->reflector->isFunctionInternal((string) $use->name);
64
        }
65
66 213
        if (Use_::TYPE_CONSTANT === $useType) {
67 18
            return false === $this->reflector->isConstantInternal((string) $use->name);
68
        }
69
70 196
        return Use_::TYPE_NORMAL !== $useType || false === $this->reflector->isClassInternal((string) $use->name);
71
    }
72
73
    /**
74
     * Finds the type of the use statement.
75
     *
76
     * @param UseUse $use
77
     *
78
     * @return int See \PhpParser\Node\Stmt\Use_ type constants.
79
     */
80 240
    private function findUseType(UseUse $use): int
81
    {
82 240
        if (Use_::TYPE_UNKNOWN === $use->type) {
83
            /** @var Use_ $parentNode */
84 235
            $parentNode = AppendParentNode::getParent($use);
85
86 235
            return $parentNode->type;
87
        }
88
89 5
        return $use->type;
90
    }
91
}
92