Completed
Pull Request — 2.x (#381)
by Alexander
02:35
created

SelfValueVisitor::resolveClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2.004
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2018, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\Transformer;
12
13
use PhpParser\Node;
14
use PhpParser\Node\Name;
15
use PhpParser\Node\Name\FullyQualified;
16
use PhpParser\Node\Stmt;
17
use PhpParser\Node\Expr;
18
use PhpParser\NodeVisitorAbstract;
19
20
/**
21
 * Node visitor that resolves class name for `self` nodes with FQN
22
 */
23
final class SelfValueVisitor extends NodeVisitorAbstract
24
{
25
    /**
26
     * List of replaced nodes
27
     *
28
     * @var Node[]
29
     */
30
    protected $replacedNodes = [];
31
32
    /**
33
     * Current namespace
34
     *
35
     * @var null|Name
36
     */
37
    protected $namespace;
38
39
    /**
40
     * Current class name
41
     *
42
     * @var null|Name
43
     */
44
    protected $className;
45
46
    /**
47
     * Returns list of changed `self` nodes
48
     *
49
     * @return Node[]
50
     */
51 1
    public function getReplacedNodes()
52
    {
53 1
        return $this->replacedNodes;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 1
    public function beforeTraverse(array $nodes)
60
    {
61 1
        $this->namespace     = null;
62 1
        $this->className     = null;
63 1
        $this->replacedNodes = [];
64 1
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 1
    public function enterNode(Node $node)
70
    {
71 1
        if ($node instanceof Stmt\Namespace_) {
72 1
            $this->namespace = $node->name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $node->name can also be of type string. However, the property $namespace is declared as type null|object<PhpParser\Node\Name>. 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...
73 1
        } elseif ($node instanceof Stmt\Class_) {
74 1
            if ($node->name !== null) {
75 1
                $this->className = new Name($node->name);
76
            }
77 1
        } elseif ($node instanceof Stmt\ClassMethod || $node instanceof Expr\Closure) {
78 1
            $node->returnType = $this->resolveType($node->returnType);
79 1
        } elseif ($node instanceof Node\Param) {
80 1
            $node->type = $this->resolveType($node->type);
0 ignored issues
show
Bug introduced by
It seems like $node->type can also be of type string; however, Go\Instrument\Transforme...eVisitor::resolveType() does only seem to accept null|object<PhpParser\Node>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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...
81
        } elseif (
82 1
            $node instanceof Expr\StaticCall
83 1
            || $node instanceof Expr\ClassConstFetch
84 1
            || $node instanceof Expr\New_
85 1
            || $node instanceof Expr\Instanceof_
86
        ) {
87 1
            if ($node->class instanceof Name) {
88 1
                $node->class = $this->resolveClassName($node->class);
89
            }
90 1
        } elseif ($node instanceof Stmt\Catch_) {
91 1
            foreach ($node->types as &$type) {
92 1
                $type = $this->resolveClassName($type);
93
            }
94
        }
95 1
    }
96
97
    /**
98
     * Resolves `self` class name with value
99
     *
100
     * @param Name $name Instance of original node
101
     *
102
     * @return Name|FullyQualified
103
     */
104 1
    protected function resolveClassName(Name $name)
105
    {
106
        // Skip all names except special `self`
107 1
        if (strtolower($name->toString()) !== 'self') {
108
            return $name;
109
        }
110
111
        // Save the original name
112 1
        $originalName = $name;
113 1
        $name = clone $originalName;
114 1
        $name->setAttribute('originalName', $originalName);
115
116 1
        $fullClassName    = Name::concat($this->namespace, $this->className);
117 1
        $resolvedSelfName = new FullyQualified('\\' . $fullClassName->toString(), $name->getAttributes());
118
119 1
        $this->replacedNodes[] = $resolvedSelfName;
120
121 1
        return $resolvedSelfName;
122
    }
123
124
    /**
125
     * Helper method for resolving type nodes
126
     *
127
     * @param Node|null $node Instance of node
128
     *
129
     * @return Node|Name|FullyQualified
130
     */
131 1
    private function resolveType(Node $node = null)
132
    {
133 1
        if ($node instanceof Node\NullableType) {
134
            $node->type = $this->resolveType($node->type);
0 ignored issues
show
Bug introduced by
It seems like $node->type can also be of type string; however, Go\Instrument\Transforme...eVisitor::resolveType() does only seem to accept null|object<PhpParser\Node>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
135
            return $node;
136
        }
137 1
        if ($node instanceof Name) {
138 1
            return $this->resolveClassName($node);
139
        }
140
141 1
        return $node;
142
    }
143
}
144