Completed
Push — master ( 3708b9...fb0aff )
by Enrico
03:36
created

VariableVariableUsage   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 110
ccs 44
cts 44
cp 1
rs 10
c 1
b 0
f 0
wmc 20
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A analyzeList() 0 15 4
A analyzePropertyFetch() 0 9 2
B analyzeArrayDimFetch() 0 18 5
A pass() 0 15 3
A analyzeAssign() 0 10 2
A analyzeVar() 0 10 2
A notice() 0 4 1
A getRegister() 0 6 1
1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression;
7
8
use PhpParser\Node\Expr;
9
use PHPSA\Analyzer\Pass;
10
use PHPSA\Context;
11
12
class VariableVariableUsage implements Pass\AnalyzerPassInterface
13
{
14
    /**
15
     * @param Expr\Assign $expr
16
     * @param Context $context
17
     * @return bool
18
     */
19 32
    public function pass(Expr\Assign $expr, Context $context)
20
    {
21
        // $(this->)something[] = …
22 32
        if ($expr->var instanceof Expr\ArrayDimFetch) {
23 2
            return $this->analyzeArrayDimFetch($expr->var, $context);
24
        }
25
26
        // $this->something = …
27 32
        if ($expr->var instanceof Expr\PropertyFetch) {
28 2
            return $this->analyzePropertyFetch($expr->var, $context);
29
        }
30
31
        // $something = …
32 32
        return $this->analyzeAssign($expr, $context);
33
    }
34
35 32
    private function analyzeAssign(Expr\Assign $expr, Context $context)
36
    {
37
        // list($a, $b) = …
38 32
        if ($expr->var instanceof Expr\List_) {
39 1
            return $this->analyzeList($expr->var, $context);
40
        }
41
42
        // $a = ... or class::$a =
43 32
        return $this->analyzeVar($expr->var, $context);
44
    }
45
46 1
    private function analyzeList(Expr\List_ $expr, Context $context)
47
    {
48 1
        $result = false;
49
50 1
        foreach ($expr->vars as $var) {
51
            // list($a, ) = …
52 1
            if (!$var) {
53 1
                continue;
54
            }
55
56 1
            $result = $this->analyzeVar($var, $context) || $result;
57 1
        }
58
59 1
        return $result;
60
    }
61
62 2
    private function analyzeArrayDimFetch(Expr\ArrayDimFetch $expr, Context $context)
63
    {
64 2
        $result = false;
65
66
        // $array[] = …
67 2
        if ($expr->var instanceof Expr\Variable) {
68 2
            $result = $this->analyzeVar($expr->var, $context);
69 2
        } else if ($expr->var instanceof Expr\PropertyFetch) {
70
            // $this->array[] = …
71 2
            $result = $this->analyzePropertyFetch($expr->var, $context);
72 2
        }
73
74 2
        if ($expr->dim instanceof Expr\Variable) {
75 1
            $result = $this->analyzeVar($expr->dim, $context) || $result;
76 1
        }
77
78 2
        return $result;
79
    }
80
81 2
    private function analyzePropertyFetch(Expr\PropertyFetch $expr, Context $context)
82
    {
83 2
        if ($expr->name instanceof Expr\Variable) {
84 1
            $this->notice($context, $expr->name);
85 1
            return true;
86
        }
87
88 2
        return false;
89
    }
90
91
92
    /**
93
     * @param Expr\Variable|Expr\StaticPropertyFetch $var
94
     * @param Context $context
95
     */
96 32
    private function analyzeVar(Expr $var, Context $context)
97
    {
98 32
        if (!$var->name instanceof Expr\Variable) {
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99 32
            return false;
100
        }
101
102 1
        $this->notice($context, $var);
103
104 1
        return true;
105
    }
106
107 1
    private function notice(Context $context, Expr $expr)
108
    {
109 1
        $context->notice('variable.dynamic_assignment', 'Dynamic assignment is greatly discouraged.', $expr);
110 1
    }
111
112
    /**
113
     * @return array
114
     */
115 1
    public function getRegister()
116
    {
117
        return [
118
            Expr\Assign::class
119 1
        ];
120
    }
121
}
122