Completed
Pull Request — master (#115)
by Enrico
03:29
created

UnexpectedUseOfThis::pass()   F

Complexity

Conditions 13
Paths 729

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 15
nc 729
nop 2
dl 0
loc 30
ccs 21
cts 21
cp 1
crap 13
rs 3.0089
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Statement;
7
8
use PhpParser\Node\Stmt;
9
use PhpParser\Node;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
14
class UnexpectedUseOfThis implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface
15
{
16
    /**
17
     * @param Node\Stmt $stmt
18
     * @param Context $context
19
     * @return bool
20
     */
21 14
    public function pass(Node\Stmt $stmt, Context $context)
22
    {
23 14
        $result = false;
24
25 14
        if ($stmt instanceof Stmt\ClassMethod) {
26 14
            $result = $this->inspectClassMethodArguments($stmt, $context) || $result;
27 14
        }
28
29 14
        if ($stmt instanceof Stmt\TryCatch) {
30 1
            $result = $this->inspectTryCatch($stmt, $context) || $result;
31 1
        }
32
33 14
        if ($stmt instanceof Stmt\Foreach_) {
34 1
            $result = $this->inspectForeach($stmt, $context) || $result;
35 1
        }
36
37 14
        if ($stmt instanceof Stmt\Static_) {
38 1
            $result = $this->inspectStaticVar($stmt, $context) || $result;
39 1
        }
40
41 14
        if ($stmt instanceof Stmt\Global_) {
42 1
            $result = $this->inspectGlobalVar($stmt, $context) || $result;
43 1
        }
44
45 14
        if ($stmt instanceof Stmt\Unset_) {
46 1
            $result = $this->inspectUnset($stmt, $context) || $result;
47 1
        }
48
49 14
        return $result;
50
    }
51
52
    /**
53
     * @return TreeBuilder
54
     */
55
    public function getConfiguration()
56
    {
57
        $treeBuilder = new TreeBuilder();
58
        $treeBuilder->root('unexpected_use.this')
59
            ->canBeDisabled()
60
        ;
61
62
        return $treeBuilder;
63
    }
64
65
    /**
66
     * @return array
67
     */
68 1
    public function getRegister()
69
    {
70
        return [
71 1
            Stmt\ClassMethod::class,
72 1
            Stmt\TryCatch::class,
73 1
            Stmt\Foreach_::class,
74 1
            Stmt\Static_::class,
75 1
            Stmt\Global_::class,
76 1
            Stmt\Unset_::class,
77 1
        ];
78
    }
79
80
    /**
81
     * @param Stmt\ClassMethod $methodStmt
82
     * @param Context $context
83
     * @return bool
84
     */
85 14
    private function inspectClassMethodArguments(Stmt\ClassMethod $methodStmt, Context $context)
86
    {
87
        /** @var \PhpParser\Node\Param $param */
88 14
        foreach ($methodStmt->getParams() as $param) {
89 2
            if ($param->name === 'this') {
90 1
                $context->notice(
91 1
                    'unexpected_use.this',
92 1
                    sprintf('Method %s can not have a parameter named "this".', $methodStmt->name),
93
                    $param
94 1
                );
95
96 1
                return true;
97
            }
98 14
        }
99
100 14
        return false;
101
    }
102
103
    /**
104
     * @param Stmt\TryCatch $tryCatchStmt
105
     * @param Context $context
106
     * @return bool
107
     */
108 1
    private function inspectTryCatch(Stmt\TryCatch $tryCatchStmt, Context $context)
109
    {
110 1
        $result = false;
111
112
        /** @var Stmt\Catch_ $catch */
113 1
        foreach ($tryCatchStmt->catches as $catch) {
114 1
            if ($catch->var === 'this') {
115 1
                $result = true;
116 1
                $context->notice(
117 1
                    'unexpected_use.this',
118 1
                    'Catch block can not have a catch variable named "this".',
119
                    $catch
120 1
                );
121 1
            }
122 1
        }
123
124 1
        return $result;
125
    }
126
127
    /**
128
     * @param Stmt\Foreach_ $foreachStmt
129
     * @param Context $context
130
     * @return bool
131
     */
132 1
    private function inspectForeach(Stmt\Foreach_ $foreachStmt, Context $context)
133
    {
134 1
        if ($foreachStmt->valueVar->name === 'this') {
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...
135 1
            $context->notice(
136 1
                'unexpected_use.this',
137 1
                'Foreach loop can not use a value variable named "this".',
138 1
                $foreachStmt->valueVar
139 1
            );
140
141 1
            return true;
142
        }
143
144 1
        return false;
145
    }
146
147
    /**
148
     * @param Stmt\Static_ $staticStmt
149
     * @param Context $context
150
     * @return bool
151
     */
152 1
    private function inspectStaticVar(Stmt\Static_ $staticStmt, Context $context)
153
    {
154 1
        $result = false;
155
156
        /** @var Stmt\StaticVar $var */
157 1
        foreach ($staticStmt->vars as $var) {
158 1
            if ($var->name === 'this') {
159 1
                $result = true;
160
161 1
                $context->notice(
162 1
                    'unexpected_use.this',
163 1
                    'Can not declare a static variable named "this".',
164
                    $var
165 1
                );
166 1
            }
167 1
        }
168
169 1
        return $result;
170
    }
171
172
    /**
173
     * @param Stmt\Global_ $globalStmt
174
     * @param Context $context
175
     * @return bool
176
     */
177 1
    private function inspectGlobalVar(Stmt\Global_ $globalStmt, Context $context)
178
    {
179 1
        $result = false;
180
181 1
        foreach ($globalStmt->vars as $var) {
182 1
            if ($var->name === 'this') {
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...
183 1
                $result = true;
184
185 1
                $context->notice(
186 1
                    'unexpected_use.this',
187 1
                    'Can not declare a global variable named "this".',
188
                    $var
189 1
                );
190 1
            }
191 1
        }
192
193 1
        return $result;
194
    }
195
196
    /**
197
     * @param Stmt\Unset_ $unsetStmt
198
     * @param Context $context
199
     * @return bool
200
     */
201 1
    private function inspectUnset(Stmt\Unset_ $unsetStmt, Context $context)
202
    {
203 1
        $result = false;
204
205 1
        foreach ($unsetStmt->vars as $var) {
206 1
            if ($var->name === 'this') {
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...
207 1
                $result = true;
208
209 1
                $context->notice(
210 1
                    'unexpected_use.this',
211 1
                    'Can not unset $this.',
212
                    $var
213 1
                );
214 1
            }
215 1
        }
216
217 1
        return $result;
218
    }
219
}
220