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

ScopeStaticCallStmtNodeVisitor::enterNode()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 54
Code Lines 29

Duplication

Lines 22
Ratio 40.74 %

Importance

Changes 0
Metric Value
cc 12
eloc 29
nc 18
nop 1
dl 22
loc 54
rs 6.7848
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\FunctionStmt;
16
17
use Humbug\PhpScoper\NodeVisitor\NamespaceStmtCollection;
18
use Humbug\PhpScoper\NodeVisitor\UseStmtCollection;
19
use PhpParser\Node;
20
use PhpParser\Node\Expr\StaticCall;
21
use PhpParser\Node\Name;
22
use PhpParser\Node\Name\FullyQualified;
23
use PhpParser\NodeVisitorAbstract;
24
25
final class ScopeStaticCallStmtNodeVisitor extends NodeVisitorAbstract
26
{
27
    private $prefix;
28
    private $namespaceStatements;
29
    private $useStmtCollection;
30
    private $whitelist;
31
32 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
        string $prefix,
34
        NamespaceStmtCollection $namespaceStatements,
35
        UseStmtCollection $useStatements,
36
        array $whitelist
37
    ) {
38
        $this->prefix = $prefix;
39
        $this->namespaceStatements = $namespaceStatements;
40
        $this->useStmtCollection = $useStatements;
41
        $this->whitelist = $whitelist;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function enterNode(Node $node): Node
48
    {
49 View Code Duplication
        if (false === ($node instanceof Name)
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...
50
            || false === $node->hasAttribute('parent')
51
            || ($node->hasAttribute('phpscoper_ignore') && $node->getAttribute('phpscoper_ignore'))
52
        ) {
53
            return $node;
54
        }
55
56
        $parentNode = $node->getAttribute('parent');
57
58
        if (false === ($parentNode instanceof StaticCall)) {
59
            return $node;
60
        }
61
62
        if (1 === count($node->parts)) {
63
            //TODO
64
            $x = '';
0 ignored issues
show
Unused Code introduced by
$x is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
        }
66
67
        $useStatement = $this->useStmtCollection->findStatementForName($node->getFirst());
68
69
        $prefix = false;
70
71
        if (null === $useStatement) {
72
            if (0 === count($this->namespaceStatements)) {
73
                $newNodeClass = new FullyQualified($node, $node->getAttributes());
74
75
                $prefix = (false === in_array((string) $newNodeClass, $this->whitelist));
76 View Code Duplication
            } else {
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...
77
                $namespaceStatement = $this->namespaceStatements->getNamespaceName();
78
79
                $newNodeClass = FullyQualified::concat($namespaceStatement, $node, $node->getAttributes());
80
81
                if (false === in_array((string) $newNodeClass, $this->whitelist)) {
82
                    return $node;
83
                }
84
            }
85 View Code Duplication
        } else {
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...
86
            $newNodeClass = FullyQualified::concat($useStatement, $node->slice(1), $node->getAttributes());
87
88
            if (false === in_array((string) $newNodeClass, $this->whitelist)) {
89
                return $node;
90
            }
91
        }
92
93
        $newNodeClass->setAttribute('phpscoper_ignore', true);
94
95
        if ($prefix) {
96
            return FullyQualified::concat($this->prefix, $newNodeClass, $newNodeClass->getAttributes());
97
        }
98
99
        return $newNodeClass;
100
    }
101
}
102