Completed
Push — master ( 550011...0647fd )
by Théo
16:20 queued 07:57
created

UseStmtPrefixer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 28%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 7
cts 25
cp 0.28
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enterNode() 0 8 3
B shouldPrefixUseStmt() 0 27 7
A findUseType() 0 11 2
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 10
    public function __construct(string $prefix, Whitelist $whitelist, Reflector $reflector)
38
    {
39 10
        $this->prefix = $prefix;
40 10
        $this->reflector = $reflector;
41 10
        $this->whitelist = $whitelist;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 10
    public function enterNode(Node $node)
48
    {
49 10
        if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) {
50
            $node->name = Name::concat($this->prefix, $node->name, $node->name->getAttributes());
51
        }
52
53 10
        return $node;
54
    }
55
56
    private function shouldPrefixUseStmt(UseUse $use): bool
57
    {
58
        $useType = $this->findUseType($use);
59
60
        // If is already from the prefix namespace
61
        if ($this->prefix === $use->name->getFirst()) {
62
            return false;
63
        }
64
65
        // If is whitelisted
66
        if ($this->whitelist->belongsToWhitelistedNamespace((string) $use->name)) {
67
            return false;
68
        }
69
70
        if (Use_::TYPE_FUNCTION === $useType) {
71
            return false === $this->reflector->isFunctionInternal((string) $use->name);
72
        }
73
74
        if (Use_::TYPE_CONSTANT === $useType) {
75
            return
76
                false === $this->whitelist->isSymbolWhitelisted((string) $use->name)
77
                && false === $this->reflector->isConstantInternal((string) $use->name)
78
            ;
79
        }
80
81
        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
    private function findUseType(UseUse $use): int
92
    {
93
        if (Use_::TYPE_UNKNOWN === $use->type) {
94
            /** @var Use_ $parentNode */
95
            $parentNode = ParentNodeAppender::getParent($use);
96
97
            return $parentNode->type;
98
        }
99
100
        return $use->type;
101
    }
102
}
103