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

IgnoreNamespaceScoperNodeVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
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;
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