Completed
Push — master ( dcddf7...d2fb4d )
by Théo
05:16 queued 03:22
created

ScopeUseStmtNodeVisitor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
D enterNode() 0 35 10
B leaveNode() 0 16 5
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\WhitelistedStatements;
18
use PhpParser\Node;
19
use PhpParser\Node\Name;
20
use PhpParser\Node\Stmt\GroupUse;
21
use PhpParser\Node\Stmt\Use_;
22
use PhpParser\Node\Stmt\UseUse;
23
use PhpParser\NodeTraverser;
24
use PhpParser\NodeVisitorAbstract;
25
26
/**
27
 * Manipulates use statements.
28
 */
29
final class ScopeUseStmtNodeVisitor extends NodeVisitorAbstract
30
{
31
    private $prefix;
32
    private $whitelist;
33
    private $whitelistedStatements;
34
35
    /**
36
     * @param string                $prefix
37
     * @param string[]              $whitelist
38
     * @param WhitelistedStatements $whitelistedStatements
39
     */
40
    public function __construct(string $prefix, array $whitelist, WhitelistedStatements $whitelistedStatements)
41
    {
42
        $this->prefix = $prefix;
43
        $this->whitelist = $whitelist;
44
        $this->whitelistedStatements = $whitelistedStatements;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function enterNode(Node $node)
51
    {
52
        if (false === ($node instanceof UseUse)) {
53
            return $node;
54
        }
55
56
        /** @var UseUse $node */
57
        if (Use_::TYPE_UNKNOWN === $node->type) {
58
            $nodeType = $node->getAttribute('parent')->type;
59
        } else {
60
            $nodeType = $node->type;
61
        }
62
63
        // Mark use statements of whitelisted classes
64
        if (Use_::TYPE_NORMAL === $nodeType && in_array((string) $node->name, $this->whitelist)
65
        ) {
66
            $this->whitelistedStatements->addNode($node);
67
68
            return $node;
69
        }
70
71
        if ($node->hasAttribute('parent')
72
            && false === ($node->getAttribute('parent') instanceof GroupUse)
73
            && $this->prefix !== $node->name->getFirst()
74
            // Is not an ignored use statement
75
            && false === (
76
                $node->hasAttribute('phpscoper_ignore')
77
                && true === $node->getAttribute('phpscoper_ignore')
78
            )
79
        ) {
80
            $node->name = Name::concat($this->prefix, $node->name);
81
        }
82
83
        return $node;
84
    }
85
86
    /**
87
     * Removes use statements of whitelisted classes.
88
     *
89
     * {@inheritdoc}
90
     */
91
    public function leaveNode(Node $node)
92
    {
93
        if ($node instanceof Use_ && 0 === count($node->uses)) {
94
            return NodeTraverser::REMOVE_NODE;
95
        }
96
97
        if (false === ($node instanceof UseUse)) {
98
            return $node;
99
        }
100
101
        if ($this->whitelistedStatements->has($node)) {
102
            return NodeTraverser::REMOVE_NODE;
103
        }
104
105
        return $node;
106
    }
107
108
//    /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
//     * @var string
110
//     */
111
//    private $prefix;
112
//
113
//    /**
114
//     * @var array
115
//     */
116
//    private $aliases;
117
//
118
//    public function __construct(string $prefix)
119
//    {
120
//        $this->prefix = $prefix;
121
//    }
122
//
123
//    /**
124
//     * @inheritdoc
125
//     */
126
//    public function beforeTraverse(array $nodes)
127
//    {
128
//        $this->aliases = [];
129
//    }
130
//
131
//    /**
132
//     * @inheritdoc
133
//     */
134
//    public function enterNode(Node $node)
135
//    {
136
//        /* Collate all single level aliases */
137
//        if ($node instanceof UseUse
138
//            && (!$node->hasAttribute('parent')
139
//            || false === ($node->getAttribute('parent') instanceof GroupUse))
140
//            && $this->prefix !== $node->name->getFirst()
141
//            && 1 === count($node->name->parts)
142
//            && $node->alias !== $node->name->getFirst()
143
//        ) {
144
//            $this->aliases[$node->alias] = $node;
145
//
146
//            return;
147
//        }
148
//
149
//        $this->scopeUseStmtIfUsedInAnyNameAsAliasedNamespace($node);
150
//    }
151
//
152
//    /**
153
//     * @param Node $node
154
//     */
155
//    private function scopeUseStmtIfUsedInAnyNameAsAliasedNamespace(Node $node)
156
//    {
157
//        if ($node instanceof Name
158
//            && 1 < count($node->parts)
159
//            && in_array($node->getFirst(), array_keys($this->aliases))
160
//        ) {
161
//            $nodeToPrefix = $this->aliases[$node->getFirst()];
162
//            $nodeToPrefix->name = Name::concat($this->prefix, $nodeToPrefix->name);
163
//            unset($this->aliases[$node->getFirst()]);
164
//        }
165
//    }
166
}
167