Completed
Push — master ( eee693...06da57 )
by Alexander
06:29
created

SelfValueVisitor::beforeTraverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2018, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Instrument\Transformer;
13
14
use PhpParser\Node;
15
use PhpParser\Node\Name;
16
use PhpParser\Node\Name\FullyQualified;
17
use PhpParser\Node\Stmt;
18
use PhpParser\Node\Expr;
19
use PhpParser\NodeVisitorAbstract;
20
21
/**
22
 * Node visitor that resolves class name for `self` nodes with FQN
23
 */
24
final class SelfValueVisitor extends NodeVisitorAbstract
25
{
26
    /**
27
     * List of replaced nodes
28
     *
29
     * @var Node[]
30
     */
31
    protected $replacedNodes = [];
32
33
    /**
34
     * Current namespace
35
     *
36
     * @var null|Name|string
37
     */
38
    protected $namespace;
39
40
    /**
41
     * Current class name
42
     *
43
     * @var null|Name
44
     */
45
    protected $className;
46
47
    /**
48
     * Returns list of changed `self` nodes
49
     *
50
     * @return Node[]
51
     */
52 1
    public function getReplacedNodes(): array
53
    {
54 1
        return $this->replacedNodes;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 1
    public function beforeTraverse(array $nodes)
61
    {
62 1
        $this->namespace     = null;
63 1
        $this->className     = null;
64 1
        $this->replacedNodes = [];
65 1
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 1
    public function enterNode(Node $node)
71
    {
72 1
        if ($node instanceof Stmt\Namespace_) {
73 1
            $this->namespace = $node->name;
74 1
        } elseif ($node instanceof Stmt\Class_) {
75 1
            if ($node->name !== null) {
76 1
                $this->className = new Name($node->name);
77
            }
78 1
        } elseif ($node instanceof Stmt\ClassMethod || $node instanceof Expr\Closure) {
79 1
            $node->returnType = $this->resolveType($node->returnType);
80 1
        } elseif ($node instanceof Node\Param) {
81 1
            $node->type = $this->resolveType($node->type);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->resolveType($node->type) can also be of type object<PhpParser\Node>. However, the property $type is declared as type null|string|object<PhpPa...rser\Node\NullableType>. 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...
82
        } elseif (
83 1
            $node instanceof Expr\StaticCall
84 1
            || $node instanceof Expr\ClassConstFetch
85 1
            || $node instanceof Expr\New_
86 1
            || $node instanceof Expr\Instanceof_
87
        ) {
88 1
            if ($node->class instanceof Name) {
89 1
                $node->class = $this->resolveClassName($node->class);
90
            }
91 1
        } elseif ($node instanceof Stmt\Catch_) {
92 1
            foreach ($node->types as &$type) {
93 1
                $type = $this->resolveClassName($type);
94
            }
95
        }
96 1
    }
97
98
    /**
99
     * Resolves `self` class name with value
100
     *
101
     * @param Name $name Instance of original node
102
     *
103
     * @return Name|FullyQualified
104
     */
105 1
    protected function resolveClassName(Name $name)
106
    {
107
        // Skip all names except special `self`
108 1
        if (strtolower($name->toString()) !== 'self') {
109
            return $name;
110
        }
111
112
        // Save the original name
113 1
        $originalName = $name;
114 1
        $name = clone $originalName;
115 1
        $name->setAttribute('originalName', $originalName);
116
117 1
        $fullClassName    = Name::concat($this->namespace, $this->className);
118 1
        $resolvedSelfName = new FullyQualified('\\' . $fullClassName->toString(), $name->getAttributes());
119
120 1
        $this->replacedNodes[] = $resolvedSelfName;
121
122 1
        return $resolvedSelfName;
123
    }
124
125
    /**
126
     * Helper method for resolving type nodes
127
     *
128
     * @param Node|string|null $node Instance of node
129
     *
130
     * @return Node|Name|FullyQualified
131
     */
132 1
    private function resolveType($node)
133
    {
134 1
        if ($node instanceof Node\NullableType) {
135
            $node->type = $this->resolveType($node->type);
136
            return $node;
137
        }
138 1
        if ($node instanceof Name) {
139 1
            return $this->resolveClassName($node);
140
        }
141
142 1
        return $node;
143
    }
144
}
145