Skip to content

Commit c8fca56

Browse files
committed
File::getMethodProperties() now supports arrow functions (ref #2523)
1 parent 68b1adb commit c8fca56

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3030
-- T_FN represents the fn string used for arrow functions
3131
-- The token is associated with the opening and closing parenthesis of the statement
3232
- File::getMethodParameters() now supports arrow functions
33+
- File::getMethodProperties() now supports arrow functions
3334
- Generic.CodeAnalysis.EmptyPhpStatement now reports unnecessary semicolons after control structure closing braces
3435
-- Thanks to Vincent Langlet for the patch
3536
- Fixed bug #2638 : Squiz.CSS.DuplicateClassDefinitionSniff sees comments as part of the class name

src/Files/File.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,14 +1543,15 @@ public function getMethodParameters($stackPtr)
15431543
*
15441544
* @return array
15451545
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
1546-
* T_FUNCTION token.
1546+
* T_FUNCTION, T_CLOSURE, or T_FN token.
15471547
*/
15481548
public function getMethodProperties($stackPtr)
15491549
{
15501550
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
15511551
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
1552+
&& $this->tokens[$stackPtr]['code'] !== T_FN
15521553
) {
1553-
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
1554+
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_FN');
15541555
}
15551556

15561557
if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
@@ -1650,8 +1651,14 @@ public function getMethodProperties($stackPtr)
16501651
}
16511652
}
16521653

1653-
$end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']);
1654-
$hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET;
1654+
if ($this->tokens[$stackPtr]['code'] === T_FN) {
1655+
$bodyToken = T_DOUBLE_ARROW;
1656+
} else {
1657+
$bodyToken = T_OPEN_CURLY_BRACKET;
1658+
}
1659+
1660+
$end = $this->findNext([$bodyToken, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']);
1661+
$hasBody = $this->tokens[$end]['code'] === $bodyToken;
16551662
}//end if
16561663

16571664
if ($returnType !== '' && $nullableReturnType === true) {

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,9 @@ interface MyInterface
6060
/* testInterfaceMethod */
6161
function myFunction();
6262
}
63+
64+
$result = array_map(
65+
/* testArrowFunction */
66+
static fn(int $number) : int => $number + 1,
67+
$numbers
68+
);

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,29 @@ public function testInterfaceMethod()
360360
}//end testInterfaceMethod()
361361

362362

363+
/**
364+
* Test a static arrow function.
365+
*
366+
* @return void
367+
*/
368+
public function testArrowFunction()
369+
{
370+
$expected = [
371+
'scope' => 'public',
372+
'scope_specified' => false,
373+
'return_type' => 'int',
374+
'nullable_return_type' => false,
375+
'is_abstract' => false,
376+
'is_final' => false,
377+
'is_static' => true,
378+
'has_body' => true,
379+
];
380+
381+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
382+
383+
}//end testArrowFunction()
384+
385+
363386
/**
364387
* Test helper.
365388
*
@@ -370,7 +393,7 @@ public function testInterfaceMethod()
370393
*/
371394
private function getMethodPropertiesTestHelper($commentString, $expected)
372395
{
373-
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE]);
396+
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE, T_FN]);
374397
$found = self::$phpcsFile->getMethodProperties($function);
375398

376399
$this->assertArraySubset($expected, $found, true);

0 commit comments

Comments
 (0)