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

UnexpectedUseOfThis::OtherAsStaticVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 0
f 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 129.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Tests\Compiling\Statements;
4
5
class UnexpectedUseOfThis
6
{
7
    /**
8
     * @return string
9
     */
10
    public function thisAsArgument($this)
0 ignored issues
show
Unused Code introduced by
The parameter $this is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
11
    {
12
        return 'Cannot use $this as parameter';
13
    }
14
15
    /**
16
     * @return string
17
     */
18
    public function thisInCatch()
19
    {
20
        try {
21
            throw new \LogicException();
22
        } catch (\Exception $this) {
23
            return 'Fatal error: Cannot re-assign $this';
24
        }
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function thisAsLoopVariable()
31
    {
32
        foreach (['foo'] as $this) {
33
            return 'Fatal error: Cannot re-assign $this';
34
        }
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function thisAsStaticVariable()
41
    {
42
        static $this;
43
44
        return 'Fatal error: Cannot use $this as static variable';
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function thisAsGlobalVariable()
51
    {
52
        global $this;
53
54
        return 'Fatal error: Cannot use $this as global variable';
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function unsetThis()
61
    {
62
        unset($this);
63
64
        return 'Fatal error: Cannot unset $this';
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function OtherAsArgument($a)
0 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Method name "UnexpectedUseOfThis::OtherAsArgument" is not in camel caps format
Loading history...
71
    {
72
        return 'Ok';
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function OtherInCatch()
0 ignored issues
show
Coding Style introduced by
Method name "UnexpectedUseOfThis::OtherInCatch" is not in camel caps format
Loading history...
79
    {
80
        try {
81
            throw new \LogicException();
82
        } catch (\Exception $a) {
83
            return 'Ok';
84
        }
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function OtherAsLoopVariable()
0 ignored issues
show
Coding Style introduced by
Method name "UnexpectedUseOfThis::OtherAsLoopVariable" is not in camel caps format
Loading history...
91
    {
92
        foreach (['foo'] as $a) {
93
            return 'Ok';
94
        }
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function OtherAsStaticVariable()
0 ignored issues
show
Coding Style introduced by
Method name "UnexpectedUseOfThis::OtherAsStaticVariable" is not in camel caps format
Loading history...
101
    {
102
        static $a;
103
104
        return 'Ok';
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function OtherAsGlobalVariable()
0 ignored issues
show
Coding Style introduced by
Method name "UnexpectedUseOfThis::OtherAsGlobalVariable" is not in camel caps format
Loading history...
111
    {
112
        global $a;
113
114
        return 'Ok';
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function unsetOther()
121
    {
122
        $a = 1;
123
        unset($a);
124
125
        return 'Ok';
126
    }
127
}
128
?>
129
----------------------------
130
[
131
    {
132
        "type":"unexpected_use.this",
133
        "message":"Method thisAsArgument can not have a parameter named \"this\".",
134
        "file":"UnexpectedUseOfThis.php",
135
        "line":9
136
    },
137
    {
138
        "type":"unexpected_use.this",
139
        "message":"Catch block can not have a catch variable named \"this\".",
140
        "file":"UnexpectedUseOfThis.php",
141
        "line":21
142
    },
143
    {
144
        "type":"unexpected_use.this",
145
        "message":"Foreach loop can not use a value variable named \"this\".",
146
        "file":"UnexpectedUseOfThis.php",
147
        "line":31
148
    },
149
    {
150
        "type":"unexpected_use.this",
151
        "message":"Can not declare a static variable named \"this\".",
152
        "file":"UnexpectedUseOfThis.php",
153
        "line":41
154
    },
155
    {
156
        "type":"unexpected_use.this",
157
        "message":"Can not declare a global variable named \"this\".",
158
        "file":"UnexpectedUseOfThis.php",
159
        "line":51
160
    },
161
    {
162
        "type":"unexpected_use.this",
163
        "message":"Can not unset $this.",
164
        "file":"UnexpectedUseOfThis.php",
165
        "line":61
166
    }
167
]
168