Skip to content

Commit

Permalink
File::getMethodProperties() now supports arrow functions (ref #2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Nov 4, 2019
1 parent 68b1adb commit c8fca56
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- T_FN represents the fn string used for arrow functions
-- The token is associated with the opening and closing parenthesis of the statement
- File::getMethodParameters() now supports arrow functions
- File::getMethodProperties() now supports arrow functions
- Generic.CodeAnalysis.EmptyPhpStatement now reports unnecessary semicolons after control structure closing braces
-- Thanks to Vincent Langlet for the patch
- Fixed bug #2638 : Squiz.CSS.DuplicateClassDefinitionSniff sees comments as part of the class name
Expand Down
15 changes: 11 additions & 4 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1543,14 +1543,15 @@ public function getMethodParameters($stackPtr)
*
* @return array
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified position is not a
* T_FUNCTION token.
* T_FUNCTION, T_CLOSURE, or T_FN token.
*/
public function getMethodProperties($stackPtr)
{
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
&& $this->tokens[$stackPtr]['code'] !== T_FN
) {
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_FN');
}

if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
Expand Down Expand Up @@ -1650,8 +1651,14 @@ public function getMethodProperties($stackPtr)
}
}

$end = $this->findNext([T_OPEN_CURLY_BRACKET, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']);
$hasBody = $this->tokens[$end]['code'] === T_OPEN_CURLY_BRACKET;
if ($this->tokens[$stackPtr]['code'] === T_FN) {
$bodyToken = T_DOUBLE_ARROW;
} else {
$bodyToken = T_OPEN_CURLY_BRACKET;
}

$end = $this->findNext([$bodyToken, T_SEMICOLON], $this->tokens[$stackPtr]['parenthesis_closer']);
$hasBody = $this->tokens[$end]['code'] === $bodyToken;
}//end if

if ($returnType !== '' && $nullableReturnType === true) {
Expand Down
6 changes: 6 additions & 0 deletions tests/Core/File/GetMethodPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ interface MyInterface
/* testInterfaceMethod */
function myFunction();
}

$result = array_map(
/* testArrowFunction */
static fn(int $number) : int => $number + 1,
$numbers
);
25 changes: 24 additions & 1 deletion tests/Core/File/GetMethodPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,29 @@ public function testInterfaceMethod()
}//end testInterfaceMethod()


/**
* Test a static arrow function.
*
* @return void
*/
public function testArrowFunction()
{
$expected = [
'scope' => 'public',
'scope_specified' => false,
'return_type' => 'int',
'nullable_return_type' => false,
'is_abstract' => false,
'is_final' => false,
'is_static' => true,
'has_body' => true,
];

$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testArrowFunction()


/**
* Test helper.
*
Expand All @@ -370,7 +393,7 @@ public function testInterfaceMethod()
*/
private function getMethodPropertiesTestHelper($commentString, $expected)
{
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE]);
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE, T_FN]);
$found = self::$phpcsFile->getMethodProperties($function);

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

0 comments on commit c8fca56

Please sign in to comment.