1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* New keywords sniff test file |
4
|
|
|
* |
5
|
|
|
* @package PHPCompatibility |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* New keywords sniff tests |
11
|
|
|
* |
12
|
|
|
* @uses BaseSniffTest |
13
|
|
|
* @package PHPCompatibility |
14
|
|
|
* @author Jansen Price <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class NewKeywordsSniffTest extends BaseSniffTest |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
const TEST_FILE = 'sniff-examples/new_keywords.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test allow_url_include |
22
|
|
|
* |
23
|
|
|
* @group newKeywords |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function testDirMagicConstant() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
30
|
|
|
$this->assertError($file, 3, "__DIR__ magic constant is not present in PHP version 5.2 or earlier"); |
31
|
|
|
|
32
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
33
|
|
|
$this->assertNoViolation($file, 3); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test insteadof |
38
|
|
|
* |
39
|
|
|
* @group newKeywords |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function testInsteadOf() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
46
|
|
|
$this->assertError($file, 15, "\"insteadof\" keyword (for traits) is not present in PHP version 5.3 or earlier"); |
47
|
|
|
$this->assertError($file, 16, "\"insteadof\" keyword (for traits) is not present in PHP version 5.3 or earlier"); |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.4'); |
51
|
|
|
$this->assertNoViolation($file, 15); |
52
|
|
|
$this->assertNoViolation($file, 16); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test namespace keyword |
57
|
|
|
* |
58
|
|
|
* @group newKeywords |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testNamespaceKeyword() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
65
|
|
|
$this->assertError($file, 20, "\"namespace\" keyword is not present in PHP version 5.2 or earlier"); |
66
|
|
|
|
67
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
68
|
|
|
$this->assertNoViolation($file, 20); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* testNamespaceConstant |
73
|
|
|
* |
74
|
|
|
* @group newKeywords |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function testNamespaceConstant() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
81
|
|
|
$this->assertError($file, 22, "__NAMESPACE__ magic constant is not present in PHP version 5.2 or earlier"); |
82
|
|
|
|
83
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
84
|
|
|
$this->assertNoViolation($file, 22); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test trait keyword |
89
|
|
|
* |
90
|
|
|
* @group newKeywords |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testTraitKeyword() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
97
|
|
|
$this->assertError($file, 24, "\"trait\" keyword is not present in PHP version 5.3 or earlier"); |
98
|
|
|
|
99
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.4'); |
100
|
|
|
$this->assertNoViolation($file, 24); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test trait magic constant |
105
|
|
|
* |
106
|
|
|
* @group newKeywords |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function testTraitConstant() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
113
|
|
|
$this->assertError($file, 26, "__TRAIT__ magic constant is not present in PHP version 5.3 or earlier"); |
114
|
|
|
|
115
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.4'); |
116
|
|
|
$this->assertNoViolation($file, 26); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Test the use keyword |
121
|
|
|
* |
122
|
|
|
* @group newKeywords |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function testUse() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
129
|
|
|
$this->assertError($file, 14, "\"use\" keyword (for traits/namespaces/anonymous functions) is not present in PHP version 5.2 or earlier"); |
130
|
|
|
|
131
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
132
|
|
|
$this->assertNoViolation($file, 14); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Test yield |
137
|
|
|
* |
138
|
|
|
* @group newKeywords |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function testYield() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.4'); |
145
|
|
|
$this->assertError($file, 33, "\"yield\" keyword (for generators) is not present in PHP version 5.4 or earlier"); |
146
|
|
|
|
147
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.5'); |
148
|
|
|
$this->assertNoViolation($file, 33); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* testFinally |
153
|
|
|
* |
154
|
|
|
* @group newKeywords |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function testFinally() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.4'); |
161
|
|
|
$this->assertError($file, 9, "\"finally\" keyword (in exception handling) is not present in PHP version 5.4 or earlier"); |
162
|
|
|
|
163
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.5'); |
164
|
|
|
$this->assertNoViolation($file, 9); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* testNowdoc |
169
|
|
|
* |
170
|
|
|
* @group newKeywords |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function testNowdoc() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
177
|
|
|
$this->assertError($file, 37, "nowdoc functionality is not present in PHP version 5.2 or earlier"); |
178
|
|
|
$this->assertError($file, 41, "nowdoc functionality is not present in PHP version 5.2 or earlier"); |
179
|
|
|
|
180
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
181
|
|
|
$this->assertNoViolation($file, 37); |
182
|
|
|
$this->assertNoViolation($file, 41); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* testConst |
187
|
|
|
* |
188
|
|
|
* @group newKeywords |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function testConst() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
195
|
|
|
$this->assertError($file, 43, "\"const\" keyword is not present in PHP version 5.2 or earlier"); |
196
|
|
|
$this->assertNoViolation($file, 46); |
197
|
|
|
$this->assertNoViolation($file, 47); |
198
|
|
|
|
199
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
200
|
|
|
$this->assertNoViolation($file, 43); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* testCallable |
205
|
|
|
* |
206
|
|
|
* @group newKeywords |
207
|
|
|
* |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
public function testCallable() |
211
|
|
|
{ |
212
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
213
|
|
|
$this->assertError($file, 50, "\"callable\" keyword is not present in PHP version 5.3 or earlier"); |
214
|
|
|
|
215
|
|
|
// Not testing no violations as the ForbiddenNames sniff will also kick in. |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* testGoto |
220
|
|
|
* |
221
|
|
|
* @group newKeywords |
222
|
|
|
* |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function testGoto() |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
228
|
|
|
$this->assertError($file, 52, "\"goto\" keyword is not present in PHP version 5.2 or earlier"); |
229
|
|
|
|
230
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.3'); |
231
|
|
|
$this->assertNoViolation($file, 52); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* testHaltCompiler |
236
|
|
|
* |
237
|
|
|
* @group newKeywords |
238
|
|
|
* |
239
|
|
|
* @requires PHP 5.3 |
240
|
|
|
* |
241
|
|
|
|
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public function testHaltCompiler() |
245
|
|
|
{ |
246
|
|
|
if (version_compare(phpversion(), '5.3', '=')) { |
247
|
|
|
// PHP 5.3 actually shows the warning. |
248
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.0'); |
249
|
|
|
$this->assertError($file, 58, "\"__halt_compiler\" keyword is not present in PHP version 5.0 or earlier"); |
250
|
|
|
} |
251
|
|
|
else { |
252
|
|
|
/* |
253
|
|
|
* Usage of `__halt_compiler()` cannot be tested on its own token as the compiler |
254
|
|
|
* will be halted... |
255
|
|
|
* So testing that any violations created *after* the compiler is halted will |
256
|
|
|
* not be reported. |
257
|
|
|
*/ |
258
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.2'); |
259
|
|
|
$this->assertNoViolation($file, 61); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.