Passed
Branch master (ff5911)
by Matthias
02:15
created

UsedSymbolCollector::recordUsageOfByString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ComposerRequireChecker\NodeVisitor;
4
5
use PhpParser\Node;
6
use PhpParser\NodeVisitorAbstract;
7
8
final class UsedSymbolCollector extends NodeVisitorAbstract
9
{
10
    /**
11
     * @var mixed[]
12
     */
13
    private $collectedSymbols = [];
14
15 22
    public function __construct()
16
    {
17 22
    }
18
19
    /**
20
     * @return string[]
21
     */
22 22
    public function getCollectedSymbols() : array
23
    {
24 22
        return array_keys($this->collectedSymbols);
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 2
    public function beforeTraverse(array $nodes)
31
    {
32 2
        $this->collectedSymbols = [];
33
34 2
        return parent::beforeTraverse($nodes);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::beforeTraverse($nodes) targeting PhpParser\NodeVisitorAbstract::beforeTraverse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 22
    public function enterNode(Node $node)
41
    {
42 22
        $this->recordExtendsUsage($node);
43 22
        $this->recordImplementsUsage($node);
44 22
        $this->recordClassExpressionUsage($node);
45 22
        $this->recordCatchUsage($node);
46 22
        $this->recordFunctionCallUsage($node);
47 22
        $this->recordFunctionParameterTypesUsage($node);
48 22
        $this->recordFunctionReturnTypeUsage($node);
49 22
        $this->recordConstantFetchUsage($node);
50 22
        $this->recordTraitUsage($node);
51
52 22
        return parent::enterNode($node);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::enterNode($node) targeting PhpParser\NodeVisitorAbstract::enterNode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
    }
54
55 22
    private function recordExtendsUsage(Node $node)
56
    {
57 22
        if ($node instanceof Node\Stmt\Class_) {
58 4
            array_map([$this, 'recordUsageOf'], array_filter([$node->extends]));
59
        }
60
61 22
        if ($node instanceof Node\Stmt\Interface_) {
62 2
            array_map([$this, 'recordUsageOf'], array_filter($node->extends));
63
        }
64 22
    }
65
66 22
    private function recordImplementsUsage(Node $node)
67
    {
68 22
        if ($node instanceof Node\Stmt\Class_) {
69 4
            array_map([$this, 'recordUsageOf'], $node->implements);
70
        }
71 22
    }
72
73 22
    private function recordClassExpressionUsage(Node $node)
74
    {
75
        if (
76
            (
77 22
                $node instanceof Node\Expr\StaticCall
78 21
                || $node instanceof Node\Expr\StaticPropertyFetch
79 20
                || $node instanceof Node\Expr\ClassConstFetch
80 19
                || $node instanceof Node\Expr\New_
81 22
                || $node instanceof Node\Expr\Instanceof_
82
            )
83 22
            && ($nodeClass = $node->class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $node->class can also be of type PhpParser\Node\Stmt\Class_. However, the property $class is declared as type PhpParser\Node\Name|PhpParser\Node\Expr. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84 22
            && $nodeClass instanceof Node\Name
85
        ) {
86 6
            $this->recordUsageOf($nodeClass);
87
        }
88 22
    }
89
90 22
    private function recordCatchUsage(Node $node)
91
    {
92 22
        if ($node instanceof Node\Stmt\Catch_) {
93 2
            foreach($node->types as $type) {
94 2
                $this->recordUsageOf($type);
95
            }
96
        }
97 22
    }
98
99 22
    private function recordFunctionCallUsage(Node $node)
100
    {
101
        if (
102 22
            $node instanceof Node\Expr\FuncCall
103 22
            && ($nodeName = $node->name)
104 22
            && $nodeName instanceof Node\Name
105
        ) {
106 2
            $this->recordUsageOf($nodeName);
107
        }
108 22
    }
109
110 22
    private function recordFunctionParameterTypesUsage(Node $node)
111
    {
112 22
        if ($node instanceof Node\Stmt\Function_
113 22
            || $node instanceof Node\Stmt\ClassMethod
114
        ) {
115 9
            foreach ($node->getParams() as $param) {
116 5
                if ($param->type instanceof Node\Name) {
117 3
                    $this->recordUsageOf($param->type);
118
                }
119 5
                if (is_string($param->type)) {
120 5
                    $this->recordUsageOfByString($param->type);
121
                }
122
            }
123
        }
124 22
    }
125
126 22
    private function recordFunctionReturnTypeUsage(Node $node)
127
    {
128 22
        if ($node instanceof Node\Stmt\Function_
129 22
            || $node instanceof Node\Stmt\ClassMethod
130
        ) {
131 9
            if ($node->getReturnType() instanceof Node\Name) {
132 3
                $this->recordUsageOf($node->getReturnType());
133
            }
134 9
            if (is_string($node->getReturnType())) {
135 3
                $this->recordUsageOfByString($node->getReturnType());
136
            }
137
        }
138 22
    }
139
140 22
    private function recordConstantFetchUsage(Node $node)
141
    {
142 22
        if ($node instanceof Node\Expr\ConstFetch) {
143 2
            $this->recordUsageOf($node->name);
144
        }
145 22
    }
146
147 22
    private function recordTraitUsage(Node $node)
148
    {
149 22
        if (! $node instanceof Node\Stmt\TraitUse) {
150 21
            return;
151
        }
152
153 1
        array_map([$this, 'recordUsageOf'], $node->traits);
154
155 1
        foreach ($node->adaptations as $adaptation) {
156
            $this->recordUsageOf($adaptation->trait);
157
158
            if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Precedence) {
159
                array_map([$this, 'recordUsageOf'], $adaptation->insteadof);
160
            }
161
        }
162 1
    }
163
164
    /**
165
     * @param Node\Name $symbol
166
     *
167
     * @return void
168
     */
169 18
    private function recordUsageOf(Node\Name $symbol)
170
    {
171 18
        $this->collectedSymbols[(string) $symbol] = $symbol;
172 18
    }
173
174
    /**
175
     * @param Node\Name $symbol
176
     *
177
     * @return void
178
     */
179 5
    private function recordUsageOfByString(string $symbol)
180
    {
181 5
        $this->collectedSymbols[$symbol] = $symbol;
182 5
    }
183
}
184