Completed
Push — master ( 74c262...0b4843 )
by Théo
03:38 queued 01:33
created

UseStmtPrefixer::shouldPrefixUseStmt()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 1
dl 0
loc 26
rs 8.439
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\NodeVisitor\UseStmt;
16
17
use Humbug\PhpScoper\NodeVisitor\AppendParentNode;
18
use PhpParser\Node;
19
use PhpParser\Node\Name;
20
use PhpParser\Node\Stmt\Use_;
21
use PhpParser\Node\Stmt\UseUse;
22
use PhpParser\NodeVisitorAbstract;
23
24
/**
25
 * Prefixes the use statements.
26
 */
27
final class UseStmtPrefixer extends NodeVisitorAbstract
28
{
29
    private $prefix;
30
    private $whitelist;
31
    private $globalWhitelister;
32
33
    /**
34
     * @param string   $prefix
35
     * @param string[] $whitelist
36
     * @param callable $globalWhitelister
37
     */
38
    public function __construct(string $prefix, array $whitelist, callable $globalWhitelister)
39
    {
40
        $this->prefix = $prefix;
41
        $this->whitelist = $whitelist;
42
        $this->globalWhitelister = $globalWhitelister;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function enterNode(Node $node)
49
    {
50
        if ($node instanceof UseUse && $this->shouldPrefixUseStmt($node)) {
51
            $node->name = Name::concat($this->prefix, $node->name);
52
        }
53
54
        return $node;
55
    }
56
57
    private function shouldPrefixUseStmt(UseUse $use): bool
58
    {
59
        $useType = $this->findUseType($use);
60
61
        // If is already from the prefix namespace
62
        if ($this->prefix === $use->name->getFirst()) {
63
            return false;
64
        }
65
66
        // Is not from the Composer namespace
67
        if ('Composer' === $use->name->getFirst()) {
68
            return false;
69
        }
70
71
        if (1 === count($use->name->parts)) {
72
            return
73
                $useType !== Use_::TYPE_NORMAL
74
                || ($this->globalWhitelister)($use->name->getFirst())
75
            ;
76
        }
77
78
        return false === (
79
            $useType === Use_::TYPE_NORMAL
80
            && in_array((string) $use->name, $this->whitelist)
81
        );
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 = AppendParentNode::getParent($use);
96
97
            return $parentNode->type;
98
        }
99
100
        return $use->type;
101
    }
102
}
103