@@ -20,6 +20,59 @@ class GetMethodPropertiesTest extends AbstractMethodUnitTest
20
20
{
21
21
22
22
23
+ /**
24
+ * Test receiving an expected exception when a non function token is passed.
25
+ *
26
+ * @param string $commentString The comment which preceeds the test.
27
+ * @param string|int|array<int|string> $targetTokenType The token type to search for after $commentString.
28
+ *
29
+ * @dataProvider dataNotAFunctionException
30
+ *
31
+ * @return void
32
+ */
33
+ public function testNotAFunctionException ($ commentString , $ targetTokenType )
34
+ {
35
+ $ this ->expectRunTimeException ('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_FN ' );
36
+
37
+ $ next = $ this ->getTargetToken ($ commentString , $ targetTokenType );
38
+ self ::$ phpcsFile ->getMethodProperties ($ next );
39
+
40
+ }//end testNotAFunctionException()
41
+
42
+
43
+ /**
44
+ * Data Provider.
45
+ *
46
+ * @see testNotAFunctionException() For the array format.
47
+ *
48
+ * @return array<string, array<string, string|int|array<int|string>>>
49
+ */
50
+ public static function dataNotAFunctionException ()
51
+ {
52
+ return [
53
+ 'return ' => [
54
+ 'commentString ' => '/* testNotAFunction */ ' ,
55
+ 'targetTokenType ' => T_RETURN ,
56
+ ],
57
+ 'function-call-fn-phpcs-3.5.3-3.5.4 ' => [
58
+ 'commentString ' => '/* testFunctionCallFnPHPCS353-354 */ ' ,
59
+ 'targetTokenType ' => [
60
+ T_FN ,
61
+ T_STRING ,
62
+ ],
63
+ ],
64
+ 'fn-live-coding ' => [
65
+ 'commentString ' => '/* testArrowFunctionLiveCoding */ ' ,
66
+ 'targetTokenType ' => [
67
+ T_FN ,
68
+ T_STRING ,
69
+ ],
70
+ ],
71
+ ];
72
+
73
+ }//end dataNotAFunctionException()
74
+
75
+
23
76
/**
24
77
* Test a basic function.
25
78
*
@@ -328,6 +381,58 @@ public function testReturnMultilineNamespace()
328
381
}//end testReturnMultilineNamespace()
329
382
330
383
384
+ /**
385
+ * Test a method with an unqualified named return type.
386
+ *
387
+ * @return void
388
+ */
389
+ public function testReturnUnqualifiedName ()
390
+ {
391
+ // Offsets are relative to the T_FUNCTION token.
392
+ $ expected = [
393
+ 'scope ' => 'private ' ,
394
+ 'scope_specified ' => true ,
395
+ 'return_type ' => '?MyClass ' ,
396
+ 'return_type_token ' => 8 ,
397
+ 'return_type_end_token ' => 8 ,
398
+ 'nullable_return_type ' => true ,
399
+ 'is_abstract ' => false ,
400
+ 'is_final ' => false ,
401
+ 'is_static ' => false ,
402
+ 'has_body ' => true ,
403
+ ];
404
+
405
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
406
+
407
+ }//end testReturnUnqualifiedName()
408
+
409
+
410
+ /**
411
+ * Test a method with a partially qualified namespaced return type.
412
+ *
413
+ * @return void
414
+ */
415
+ public function testReturnPartiallyQualifiedName ()
416
+ {
417
+ // Offsets are relative to the T_FUNCTION token.
418
+ $ expected = [
419
+ 'scope ' => 'public ' ,
420
+ 'scope_specified ' => false ,
421
+ 'return_type ' => 'Sub\Level\MyClass ' ,
422
+ 'return_type_token ' => 7 ,
423
+ 'return_type_end_token ' => 11 ,
424
+ 'nullable_return_type ' => false ,
425
+ 'is_abstract ' => false ,
426
+ 'is_final ' => false ,
427
+ 'is_static ' => false ,
428
+ 'has_body ' => true ,
429
+ ];
430
+
431
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
432
+
433
+ }//end testReturnPartiallyQualifiedName()
434
+
435
+
331
436
/**
332
437
* Test a basic abstract method.
333
438
*
@@ -1056,6 +1161,116 @@ public function testPHP82PseudoTypeFalseAndTrue()
1056
1161
}//end testPHP82PseudoTypeFalseAndTrue()
1057
1162
1058
1163
1164
+ /**
1165
+ * Test for incorrect tokenization of array return type declarations in PHPCS < 2.8.0.
1166
+ *
1167
+ * @link https://github.com/squizlabs/PHP_CodeSniffer/pull/1264
1168
+ *
1169
+ * @return void
1170
+ */
1171
+ public function testPhpcsIssue1264 ()
1172
+ {
1173
+ // Offsets are relative to the T_FUNCTION token.
1174
+ $ expected = [
1175
+ 'scope ' => 'public ' ,
1176
+ 'scope_specified ' => false ,
1177
+ 'return_type ' => 'array ' ,
1178
+ 'return_type_token ' => 8 ,
1179
+ 'return_type_end_token ' => 8 ,
1180
+ 'nullable_return_type ' => false ,
1181
+ 'is_abstract ' => false ,
1182
+ 'is_final ' => false ,
1183
+ 'is_static ' => false ,
1184
+ 'has_body ' => true ,
1185
+ ];
1186
+
1187
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
1188
+
1189
+ }//end testPhpcsIssue1264()
1190
+
1191
+
1192
+ /**
1193
+ * Test handling of incorrect tokenization of array return type declarations for arrow functions
1194
+ * in a very specific code sample in PHPCS < 3.5.4.
1195
+ *
1196
+ * @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2773
1197
+ *
1198
+ * @return void
1199
+ */
1200
+ public function testArrowFunctionArrayReturnValue ()
1201
+ {
1202
+ // Offsets are relative to the T_FN token.
1203
+ $ expected = [
1204
+ 'scope ' => 'public ' ,
1205
+ 'scope_specified ' => false ,
1206
+ 'return_type ' => 'array ' ,
1207
+ 'return_type_token ' => 5 ,
1208
+ 'return_type_end_token ' => 5 ,
1209
+ 'nullable_return_type ' => false ,
1210
+ 'is_abstract ' => false ,
1211
+ 'is_final ' => false ,
1212
+ 'is_static ' => false ,
1213
+ 'has_body ' => true ,
1214
+ ];
1215
+
1216
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
1217
+
1218
+ }//end testArrowFunctionArrayReturnValue()
1219
+
1220
+
1221
+ /**
1222
+ * Test handling of an arrow function returning by reference.
1223
+ *
1224
+ * @return void
1225
+ */
1226
+ public function testArrowFunctionReturnByRef ()
1227
+ {
1228
+ // Offsets are relative to the T_FN token.
1229
+ $ expected = [
1230
+ 'scope ' => 'public ' ,
1231
+ 'scope_specified ' => false ,
1232
+ 'return_type ' => '?string ' ,
1233
+ 'return_type_token ' => 12 ,
1234
+ 'return_type_end_token ' => 12 ,
1235
+ 'nullable_return_type ' => true ,
1236
+ 'is_abstract ' => false ,
1237
+ 'is_final ' => false ,
1238
+ 'is_static ' => false ,
1239
+ 'has_body ' => true ,
1240
+ ];
1241
+
1242
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
1243
+
1244
+ }//end testArrowFunctionReturnByRef()
1245
+
1246
+
1247
+ /**
1248
+ * Test handling of function declaration nested in a ternary, where the colon for the
1249
+ * return type was incorrectly tokenized as T_INLINE_ELSE prior to PHPCS 3.5.7.
1250
+ *
1251
+ * @return void
1252
+ */
1253
+ public function testFunctionDeclarationNestedInTernaryPHPCS2975 ()
1254
+ {
1255
+ // Offsets are relative to the T_FN token.
1256
+ $ expected = [
1257
+ 'scope ' => 'public ' ,
1258
+ 'scope_specified ' => true ,
1259
+ 'return_type ' => 'c ' ,
1260
+ 'return_type_token ' => 7 ,
1261
+ 'return_type_end_token ' => 7 ,
1262
+ 'nullable_return_type ' => false ,
1263
+ 'is_abstract ' => false ,
1264
+ 'is_final ' => false ,
1265
+ 'is_static ' => false ,
1266
+ 'has_body ' => true ,
1267
+ ];
1268
+
1269
+ $ this ->getMethodPropertiesTestHelper ('/* ' .__FUNCTION__ .' */ ' , $ expected );
1270
+
1271
+ }//end testFunctionDeclarationNestedInTernaryPHPCS2975()
1272
+
1273
+
1059
1274
/**
1060
1275
* Test helper.
1061
1276
*
0 commit comments