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

ScopeConstStmtNodeVisitor::enterNode()   C

Complexity

Conditions 9
Paths 12

Size

Total Lines 47
Code Lines 26

Duplication

Lines 20
Ratio 42.55 %

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 12
nop 1
dl 20
loc 47
rs 5.2941
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\Arg;
19
use PhpParser\Node\Expr\ClassConstFetch;
20
use PhpParser\Node\Name;
21
use PhpParser\Node\Name\FullyQualified;
22
use PhpParser\NodeVisitorAbstract;
23
24
final class ScopeConstStmtNodeVisitor extends NodeVisitorAbstract
25
{
26
    private $prefix;
27
    private $namespaceStatements;
28
    private $useStmtCollection;
29
    private $whitelist;
30
31 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...
32
        string $prefix,
33
        NamespaceStmtCollection $namespaceStatements,
34
        UseStmtCollection $useStatements,
35
        array $whitelist
36
    ) {
37
        $this->prefix = $prefix;
38
        $this->namespaceStatements = $namespaceStatements;
39
        $this->useStmtCollection = $useStatements;
40
        $this->whitelist = $whitelist;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function enterNode(Node $node): Node
47
    {
48
        if (false === ($node instanceof ClassConstFetch)) {
49
            return $node;
50
        }
51
        /** @var ClassConstFetch $node */
52
        $constClassNode = $node->class;
53
54
        if (false === ($constClassNode instanceof Name)) {
55
            return $node;
56
        }
57
        /* @var Name $useStatement */
58
59
        if ($node->hasAttribute('parent') && $node->getAttribute('parent') instanceof Arg) {
60
            return $node;
61
        }
62
63
        $useStatement = $this->useStmtCollection->findStatementForName($constClassNode->getFirst());
0 ignored issues
show
Bug introduced by
The method getFirst does only exist in PhpParser\Node\Name, but not in PhpParser\Node\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
64
65
        $prefix = false;
66
67 View Code Duplication
        if (null === $useStatement) {
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...
68
            if (0 === count($this->namespaceStatements)) {
69
                $newClassNode = new FullyQualified($constClassNode, $constClassNode->getAttributes());
70
71
                $prefix = true;
72
            } else {
73
                $namespaceStatement = $this->namespaceStatements->getNamespaceName();
74
75
                $newClassNode = FullyQualified::concat($namespaceStatement, $constClassNode, $constClassNode->getAttributes());
76
            }
77
        } else {
78
            $newClassNode = FullyQualified::concat($useStatement, $constClassNode->slice(1), $constClassNode->getAttributes());
0 ignored issues
show
Bug introduced by
The method slice does only exist in PhpParser\Node\Name, but not in PhpParser\Node\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
        }
80
81
        $newClassNode->setAttribute('phpscoper_ignore', true);
82
83 View Code Duplication
        if (in_array((string) $newClassNode, $this->whitelist)) {
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...
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
84
            // Continue
85
        } elseif ($prefix) {
86
            $newClassNode = FullyQualified::concat($this->prefix, $newClassNode, $newClassNode->getAttributes());
87
        } else {
88
            return $node;
89
        }
90
91
        return new ClassConstFetch($newClassNode, $node->name, $node->getAttributes());
92
    }
93
}
94