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

IgnoreNamespaceScoperNodeVisitor   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 102
Duplicated Lines 6.86 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 7
loc 102
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D enterNode() 7 83 21

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Expr\ClassConstFetch;
19
use PhpParser\Node\Expr\FuncCall;
20
use PhpParser\Node\Expr\New_;
21
use PhpParser\Node\Expr\StaticCall;
22
use PhpParser\Node\Name;
23
use PhpParser\Node\Name\FullyQualified;
24
use PhpParser\Node\Stmt\GroupUse;
25
use PhpParser\Node\Stmt\Use_;
26
use PhpParser\Node\Stmt\UseUse;
27
use PhpParser\NodeVisitorAbstract;
28
29
final class IgnoreNamespaceScoperNodeVisitor extends NodeVisitorAbstract
30
{
31
    private $whitelist;
32
    private $whitelister;
33
34
    /**
35
     * @param string[] $whitelist
36
     * @param callable $whitelister
37
     */
38
    public function __construct(array $whitelist, callable $whitelister)
39
    {
40
        $this->whitelist = $whitelist;
41
        $this->whitelister = $whitelister;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function enterNode(Node $node)
48
    {
49
        //
50
        // For use statements
51
        //
52
        if ($node instanceof UseUse
53
            && $node->hasAttribute('parent')
54
            && false === ($node->getAttribute('parent') instanceof GroupUse)
55
            && (
56
                // Is not a whitelisted use statement of the global namespace
57
                (1 === count($node->name->parts) && false === ($this->whitelister)($node->name->getFirst()))
58
                // Is not from the Composer namespace
59
                || 'Composer' === $node->name->getFirst()
60
                // Is not a whitelisted class
61
                || ($node->getAttribute('parent') instanceof Use_
62
                    && Use_::TYPE_NORMAL === $node->getAttribute('parent')->type
63
                    && in_array((string) $node->name, $this->whitelist)
64
                )
65
            )
66
        ) {
67
            $node->setAttribute('phpscoper_ignore', true);
68
69
            return $node;
70
        } elseif (false === ($node instanceof FullyQualified) || false === $node->isFullyQualified()) {
71
            return $node;
72
        }
73
74
        /** @var FullyQualified $node */
75
76
        // Is a fully qualified call from the global namespace which is not whitelisted
77 View Code Duplication
        if (1 === count($node->parts)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            && (false === ($this->whitelister)($node->getFirst()))
79
        ) {
80
            $node->setAttribute('phpscoper_ignore', true);
81
82
            return $node;
83
        }
84
85
        if (false === $node->hasAttribute('parent')) {
86
            return $node;
87
        }
88
89
        $parentNode = $node->getAttribute('parent');
90
91
        // Is a static method call of a whitelisted class
92
        if ($parentNode instanceof StaticCall
93
            && in_array((string) $parentNode->class, $this->whitelist)
94
        ) {
95
            $node->setAttribute('phpscoper_ignore', true);
96
97
            return $node;
98
        }
99
100
//        // Is a method call of a whitelisted class
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
101
//        if ($parentNode instanceof FuncCall
102
//            && $parentNode->name instanceof Name
103
//            && in_array((string) $parentNode->name->slice(0, -1), $this->whitelist)
104
//        ) {
105
//            $node->setAttribute('phpscoper_ignore', true);
106
//
107
//            return $node;
108
//        }
109
110
        // Is a new instance of a whitelisted class
111
        if ($parentNode instanceof New_
112
            && in_array((string) $parentNode->class->toString(), $this->whitelist)
113
        ) {
114
            $node->setAttribute('phpscoper_ignore', true);
115
116
            return $node;
117
        }
118
119
        // Is a constant call of a whitelisted class
120
        if ($parentNode instanceof ClassConstFetch
121
            && in_array((string) $node, $this->whitelist)
122
        ) {
123
            $node->setAttribute('phpscoper_ignore', true);
124
125
            return $node;
126
        }
127
128
        return $node;
129
    }
130
}
131