Skip to content

Commit a7063f6

Browse files
committed
Tests/GetMethodParametersTest: add extra tests
This adds some extra tests which were already in use in PHPCSUtils.
1 parent d467cbc commit a7063f6

6 files changed

+1563
-152
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
/* testParseError */
4+
function missingOpenParens // Intentional parse error.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2019-2024 PHPCSStandards Contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\File;
11+
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13+
14+
/**
15+
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
16+
*
17+
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
18+
*/
19+
class GetMethodParametersParseError1Test extends AbstractMethodUnitTest
20+
{
21+
22+
23+
/**
24+
* Test receiving an empty array when encountering a specific parse error.
25+
*
26+
* @return void
27+
*/
28+
public function testParseError()
29+
{
30+
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
31+
$result = self::$phpcsFile->getMethodParameters($target);
32+
33+
$this->assertSame([], $result);
34+
35+
}//end testParseError()
36+
37+
38+
}//end class
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
/* testParseError */
4+
function missingCloseParens( // Intentional parse error.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2019-2024 PHPCSStandards Contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\File;
11+
12+
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13+
14+
/**
15+
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
16+
*
17+
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
18+
*/
19+
class GetMethodParametersParseError2Test extends AbstractMethodUnitTest
20+
{
21+
22+
23+
/**
24+
* Test receiving an empty array when encountering a specific parse error.
25+
*
26+
* @return void
27+
*/
28+
public function testParseError()
29+
{
30+
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
31+
$result = self::$phpcsFile->getMethodParameters($target);
32+
33+
$this->assertSame([], $result);
34+
35+
}//end testParseError()
36+
37+
38+
}//end class

tests/Core/File/GetMethodParametersTest.inc

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
<?php
22

3+
/* testImportUse */
4+
use Vendor\Package\Sub as Alias;
5+
6+
/* testImportGroupUse */
7+
use Vendor\Package\Sub\{
8+
ClassA,
9+
ClassB as BAlias,
10+
};
11+
12+
if ($foo) {}
13+
14+
/* testTraitUse */
15+
class TraitUse {
16+
use ImportedTrait;
17+
18+
function methodName() {}
19+
}
20+
21+
/* testNotAFunction */
22+
interface NotAFunction {};
23+
24+
/* testFunctionNoParams */
25+
function noParams() {}
26+
327
/* testPassByReference */
428
function passByReference(&$var) {}
529

@@ -32,6 +56,69 @@ function myFunction($a = 10 & 20) {}
3256
/* testArrowFunction */
3357
fn(int $a, ...$b) => $b;
3458

59+
/* testArrowFunctionReturnByRef */
60+
fn&(?string $a) => $b;
61+
62+
/* testArrayDefaultValues */
63+
function arrayDefaultValues($var1 = [], $var2 = array(1, 2, 3) ) {}
64+
65+
/* testConstantDefaultValueSecondParam */
66+
function constantDefaultValueSecondParam($var1, $var2 = M_PI) {}
67+
68+
/* testScalarTernaryExpressionInDefault */
69+
function ternayInDefault( $a = FOO ? 'bar' : 10, ? bool $b ) {}
70+
71+
/* testVariadicFunction */
72+
function variadicFunction( int ... $a ) {}
73+
74+
/* testVariadicByRefFunction */
75+
function variadicByRefFunction( &...$a ) {}
76+
77+
/* testWithAllTypes */
78+
class testAllTypes {
79+
function allTypes(
80+
?ClassName $a,
81+
self $b,
82+
parent $c,
83+
object $d,
84+
?int $e,
85+
string &$f,
86+
iterable $g,
87+
bool $h = true,
88+
callable $i = 'is_null',
89+
float $j = 1.1,
90+
array ...$k
91+
) {}
92+
}
93+
94+
/* testArrowFunctionWithAllTypes */
95+
$fn = fn(
96+
?ClassName $a,
97+
self $b,
98+
parent $c,
99+
object $d,
100+
?int $e,
101+
string &$f,
102+
iterable $g,
103+
bool $h = true,
104+
callable $i = 'is_null',
105+
float $j = 1.1,
106+
array ...$k
107+
) => $something;
108+
109+
/* testMessyDeclaration */
110+
function messyDeclaration(
111+
// comment
112+
?\MyNS /* comment */
113+
\ SubCat // phpcs:ignore Standard.Cat.Sniff -- for reasons.
114+
\ MyClass $a,
115+
$b /* test */ = /* test */ 'default' /* test*/,
116+
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
117+
? /*comment*/
118+
bool // phpcs:disable Stnd.Cat.Sniff -- For reasons.
119+
& /*test*/ ... /* phpcs:ignore */ $c
120+
) {}
121+
35122
/* testPHP8MixedTypeHint */
36123
function mixedTypeHint(mixed &...$var1) {}
37124

@@ -52,7 +139,7 @@ function namespacedClassType( \Package\Sub\ClassName $a, ?Sub\AnotherClass $b )
52139
function unionTypeSimple(int|float $number, self|parent &...$obj) {}
53140

54141
/* testPHP8UnionTypesWithSpreadOperatorAndReference */
55-
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB) {}
142+
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB ) {}
56143

57144
/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
58145
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;
@@ -116,7 +203,13 @@ class ConstructorPropertyPromotionAndNormalParams {
116203

117204
class ConstructorPropertyPromotionWithReadOnly {
118205
/* testPHP81ConstructorPropertyPromotionWithReadOnly */
119-
public function __construct(public readonly ?int $promotedProp, readonly private string|bool &$promotedToo) {}
206+
public function __construct(public readonly ?int $promotedProp, ReadOnly private string|bool &$promotedToo) {}
207+
}
208+
209+
class ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration {
210+
/* testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration */
211+
// Intentional fatal error. Readonly properties MUST be typed.
212+
public function __construct(public readonly $promotedProp, ReadOnly private &$promotedToo) {}
120213
}
121214

122215
class ConstructorPropertyPromotionWithOnlyReadOnly {
@@ -180,3 +273,49 @@ function pseudoTypeTrue(?true $var = true) {}
180273
/* testPHP82PseudoTypeFalseAndTrue */
181274
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
182275
function pseudoTypeFalseAndTrue(true|false $var = true) {}
276+
277+
/* testPHP81NewInInitializers */
278+
function newInInitializers(
279+
TypeA $new = new TypeA(self::CONST_VALUE),
280+
\Package\TypeB $newToo = new \Package\TypeB(10, 'string'),
281+
) {}
282+
283+
/* testFunctionCallFnPHPCS353-354 */
284+
$value = $obj->fn(true);
285+
286+
/* testClosureNoParams */
287+
function() {};
288+
289+
/* testClosure */
290+
function( $a = 'test' ) {};
291+
292+
/* testClosureUseNoParams */
293+
function() use() {};
294+
295+
/* testClosureUse */
296+
function() use( $foo, $bar ) {};
297+
298+
/* testFunctionParamListWithTrailingComma */
299+
function trailingComma(
300+
?string $foo /*comment*/ ,
301+
$bar = 0,
302+
) {}
303+
304+
/* testClosureParamListWithTrailingComma */
305+
function(
306+
$foo,
307+
$bar,
308+
) {};
309+
310+
/* testArrowFunctionParamListWithTrailingComma */
311+
$fn = fn( ?int $a , ...$b, ) => $b;
312+
313+
/* testClosureUseWithTrailingComma */
314+
function() use(
315+
$foo /*comment*/ ,
316+
$bar,
317+
) {};
318+
319+
/* testArrowFunctionLiveCoding */
320+
// Intentional parse error. This has to be the last test in the file.
321+
$fn = fn

0 commit comments

Comments
 (0)