Skip to content

Commit 7c632db

Browse files
authored
Merge pull request #223 from PHPCSStandards/feature/file-getmethodparameters-improve-tests
File::getMethodParameters(): improve tests
2 parents 4635481 + 5901a6d commit 7c632db

6 files changed

+2385
-389
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 <phpcs_nospam@adviesenzo.nl>
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 <phpcs_nospam@adviesenzo.nl>
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: 147 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,75 @@ 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+
/* testVariadicFunctionClassType */
78+
function variableLengthArgument($unit, DateInterval ...$intervals) {}
79+
80+
/* testNameSpacedTypeDeclaration */
81+
function namespacedClassType( \Package\Sub\ClassName $a, ?Sub\AnotherClass $b ) {}
82+
83+
/* testWithAllTypes */
84+
class testAllTypes {
85+
function allTypes(
86+
?ClassName $a,
87+
self $b,
88+
parent $c,
89+
object $d,
90+
?int $e,
91+
string &$f,
92+
iterable $g,
93+
bool $h = true,
94+
callable $i = 'is_null',
95+
float $j = 1.1,
96+
array ...$k
97+
) {}
98+
}
99+
100+
/* testArrowFunctionWithAllTypes */
101+
$fn = fn(
102+
?ClassName $a,
103+
self $b,
104+
parent $c,
105+
object $d,
106+
?int $e,
107+
string &$f,
108+
iterable $g,
109+
bool $h = true,
110+
callable $i = 'is_null',
111+
float $j = 1.1,
112+
array ...$k
113+
) => $something;
114+
115+
/* testMessyDeclaration */
116+
function messyDeclaration(
117+
// comment
118+
?\MyNS /* comment */
119+
\ SubCat // phpcs:ignore Standard.Cat.Sniff -- for reasons.
120+
\ MyClass $a,
121+
$b /* test */ = /* test */ 'default' /* test*/,
122+
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
123+
? /*comment*/
124+
bool // phpcs:disable Stnd.Cat.Sniff -- For reasons.
125+
& /*test*/ ... /* phpcs:ignore */ $c
126+
) {}
127+
35128
/* testPHP8MixedTypeHint */
36129
function mixedTypeHint(mixed &...$var1) {}
37130

@@ -46,7 +139,7 @@ function namespaceOperatorTypeHint(?namespace\Name $var1) {}
46139
function unionTypeSimple(int|float $number, self|parent &...$obj) {}
47140

48141
/* testPHP8UnionTypesWithSpreadOperatorAndReference */
49-
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB) {}
142+
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB ) {}
50143

51144
/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
52145
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;
@@ -110,7 +203,13 @@ class ConstructorPropertyPromotionAndNormalParams {
110203

111204
class ConstructorPropertyPromotionWithReadOnly {
112205
/* testPHP81ConstructorPropertyPromotionWithReadOnly */
113-
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) {}
114213
}
115214

116215
class ConstructorPropertyPromotionWithOnlyReadOnly {
@@ -174,3 +273,49 @@ function pseudoTypeTrue(?true $var = true) {}
174273
/* testPHP82PseudoTypeFalseAndTrue */
175274
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
176275
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)