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

IgnoreUseStmtNodeVisitor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 13 6
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 PhpParser\Node;
18
use PhpParser\Node\Stmt\GroupUse;
19
use PhpParser\Node\Stmt\UseUse;
20
use PhpParser\NodeVisitorAbstract;
21
22
/**
23
 * Whitelists from the scoping the relevant use statements:.
24
 *
25
 * ```
26
 * use Closure;
27
 * use Foo;
28
 * use Composer\Composer;
29
 * ```
30
 */
31
final class IgnoreUseStmtNodeVisitor extends NodeVisitorAbstract
32
{
33
    /**
34
     * @inheritdoc
35
     */
36
    public function enterNode(Node $node): Node
37
    {
38
        if ($node instanceof UseUse
39
            && $node->hasAttribute('parent')
40
            && false === ($node->getAttribute('parent') instanceof GroupUse)
41
            // If is a single level use statements or part of the Composer namespace
42
            && (1 === count($node->name->parts) || 'Composer' === $node->name->getFirst())
43
        ) {
44
            $node->setAttribute('phpscoper_ignore', true);
45
        }
46
47
        return $node;
48
    }
49
}
50