Completed
Pull Request — master (#240)
by Дмитрий
08:27
created

VariableVariableUsage::analyzeList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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\Helper\DefaultMetadataPassTrait;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
13
class VariableVariableUsage implements Pass\AnalyzerPassInterface
14
{
15
    use DefaultMetadataPassTrait;
16
17
    const DESCRIPTION = 'Discourages the use of variable variables.';
18
19
    /**
20
     * @param Expr\Assign $expr
21
     * @param Context $context
22
     * @return bool
23
     */
24 29
    public function pass(Expr\Assign $expr, Context $context)
25
    {
26
        // $(this->)something[] = …
27 29
        if ($expr->var instanceof Expr\ArrayDimFetch) {
28 1
            return $this->analyzeArrayDimFetch($expr->var, $context);
29
        }
30
31
        // $this->something = …
32 29
        if ($expr->var instanceof Expr\PropertyFetch) {
33 1
            return $this->analyzePropertyFetch($expr->var, $context);
34
        }
35
36
        // $something = …
37 29
        return $this->analyzeAssign($expr, $context);
38
    }
39
40 29
    private function analyzeAssign(Expr\Assign $expr, Context $context)
41
    {
42
        // list($a, $b) = …
43 29
        if ($expr->var instanceof Expr\List_) {
44 1
            return $this->analyzeList($expr->var, $context);
45
        }
46
47
        // $a = ... or class::$a =
48 29
        return $this->analyzeVar($expr->var, $context);
49
    }
50
51 1
    private function analyzeList(Expr\List_ $expr, Context $context)
52
    {
53 1
        $result = false;
54
55 1
        foreach ($expr->vars as $var) {
56
            // list($a, ) = …
57 1
            if (!$var) {
58 1
                continue;
59
            }
60
61 1
            $result = $this->analyzeVar($var, $context) || $result;
62 1
        }
63
64 1
        return $result;
65
    }
66
67 1
    private function analyzeArrayDimFetch(Expr\ArrayDimFetch $expr, Context $context)
68
    {
69 1
        $result = false;
70
71
        // $array[] = …
72 1
        if ($expr->var instanceof Expr\Variable) {
73 1
            $result = $this->analyzeVar($expr->var, $context);
74 1
        } else if ($expr->var instanceof Expr\PropertyFetch) {
75
            // $this->array[] = …
76 1
            $result = $this->analyzePropertyFetch($expr->var, $context);
77 1
        }
78
79 1
        if ($expr->dim instanceof Expr\Variable) {
80
            $result = $this->analyzeVar($expr->dim, $context) || $result;
81
        }
82
83 1
        return $result;
84
    }
85
86 1
    private function analyzePropertyFetch(Expr\PropertyFetch $expr, Context $context)
87
    {
88 1
        if ($expr->name instanceof Expr\Variable) {
89 1
            $this->notice($context, $expr->name);
90 1
            return true;
91
        }
92
93 1
        return false;
94
    }
95
96
97
    /**
98
     * @param Expr\Variable|Expr\StaticPropertyFetch $var
99
     * @param Context $context
100
     */
101 29
    private function analyzeVar(Expr $var, Context $context)
102
    {
103 29
        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...
104 29
            return false;
105
        }
106
107 1
        $this->notice($context, $var);
108
109 1
        return true;
110
    }
111
112 1
    private function notice(Context $context, Expr $expr)
113
    {
114 1
        $context->notice('variable.dynamic_assignment', 'Dynamic assignment is greatly discouraged.', $expr);
115 1
    }
116
117
    /**
118
     * @return array
119
     */
120 1
    public function getRegister()
121
    {
122
        return [
123
            Expr\Assign::class
124 1
        ];
125
    }
126
}
127