-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathStaticThisUsageUnitTest.inc
More file actions
178 lines (149 loc) · 4.17 KB
/
StaticThisUsageUnitTest.inc
File metadata and controls
178 lines (149 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
abstract class MyClass {
abstract public static function ignoreMe($a, $b);
public static function func1()
{
$value = 'hello';
$newValue = array($this->func2());
$result = $this->getValue($value);
return $this->setValue($result);
}
public static function /* */ func1()
{
return $this->setValue($result);
}
public static function
func1()
{
return $this->setValue($result);
}
public function func1()
{
$value = 'hello';
$newValue = array($this->func2());
$result = $this->getValue($value);
return $this->setValue($result);
}
function func1()
{
$value = 'hello';
$newValue = array($this->func2());
$result = $this->getValue($value);
return $this->setValue($result);
}
public static function func1() {
return static function() {
echo $this->name;
};
}
private static function func1(array $data)
{
return new class()
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
};
}
public function getAnonymousClass() {
return new class() {
public static function something() {
$this->doSomething();
}
};
}
}
trait MyTrait {
public static function myFunc() {
$this->doSomething();
}
}
$b = new class()
{
public static function myFunc() {
$this->doSomething();
}
public static function other() {
return fn () => $this->name;
}
public static function anonClassUseThis() {
return new class($this) {
public function __construct($class) {
}
};
}
public static function anonClassAnotherThis() {
return new class() {
public function __construct() {
$this->id = 1;
}
};
}
public static function anonClassNestedUseThis() {
return new class(new class($this) {}) {
};
}
public static function anonClassNestedAnotherThis() {
return new class(new class() {
public function __construct() {
$this->id = 1;
}
}) {
};
}
public static function thisMustBeLowercase() {
$This = 'hey';
return $This;
}
}
enum MyEnum {
private function notStatic () {
$this->doSomething();
}
public static function myFunc() {
$this->doSomething();
}
}
class NestedScopesShouldBeSkipped
{
public static function examineMe() {
$this->shouldBeFlagged();
function nestedFunction() {
$this->nestedFunctionsDoNotHaveAccessToThis();
}
\Closure::bind(
function ($this) { // Cannot use $this as a param, so this should still be flagged.
$this->x = 1; // This is fine. $this related to the Target class, not to the NestedScopesShouldBeSkipped class.
},
new Target(),
Target::class,
)();
$callable = static function ($a, $b) {
return $a * $b * $this->multiplier; // This should be flagged. The closure is declared as static.
};
}
public function dontExamineMe() {
$this->shouldNotBeFlagged();
\Closure::bind(
function () {
$this->x = 1; // This is fine. $this related to the Target class, not to the NestedScopesShouldBeSkipped class.
},
new Target(),
Target::class,
)();
$callable = static function ($a, $b) {
return $a * $b * $this->multiplier; // This should be flagged. The closure is declared as static.
};
}
}
function ignoreMe() {
$this->notAllowedButNotTheSameIssueAsThisSniffIsLookingFor();
}
$callable = function ($a, $b) {
return $a * $b * $this->multiplier; // This should NOT be flagged. The closure might be bound to an object.
};
$callable = static function ($a, $b) {
return $a * $b * $this->multiplier; // This should be flagged. The closure is declared as static.
};