Completed
Push — master ( a5b8f2...ff87ce )
by Théo
03:58 queued 01:55
created

IgnoreNamespaceScoperNodeVisitor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C enterNode() 0 23 11
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;
16
17
use PhpParser\Node;
18
use PhpParser\Node\Name\FullyQualified;
19
use PhpParser\Node\Stmt\GroupUse;
20
use PhpParser\Node\Stmt\UseUse;
21
use PhpParser\NodeVisitorAbstract;
22
23
final class IgnoreNamespaceScoperNodeVisitor extends NodeVisitorAbstract
24
{
25
    private $whitelister;
26
27
    public function __construct(callable $whitelister)
28
    {
29
        $this->whitelister = $whitelister;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function enterNode(Node $node)
36
    {
37
        if ($node instanceof FullyQualified
38
            && $node->isFullyQualified()
39
            && 1 === count($node->parts)
40
            && (false === ($this->whitelister)($node->getFirst()))
41
        ) {
42
            $node->setAttribute('phpscoper_ignore', true);
43
        }
44
45
        if ($node instanceof UseUse
46
            && $node->hasAttribute('parent')
47
            && false === ($node->getAttribute('parent') instanceof GroupUse)
48
            && (
49
                (1 === count($node->name->parts) && false === ($this->whitelister)($node->name->getFirst()))
50
                || 'Composer' === $node->name->getFirst()
51
            )
52
        ) {
53
            $node->setAttribute('phpscoper_ignore', true);
54
        }
55
56
        return $node;
57
    }
58
}
59