Skip to content

Commit

Permalink
File::getMethodParameters() 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 8fedf8c commit b74e813
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- The PHP 7.4 T_FN token has been made available for older versions
-- 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
- 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
6 changes: 4 additions & 2 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,15 +1304,17 @@ public function getDeclarationName($stackPtr)
*
* @return array
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified $stackPtr is not of
* type T_FUNCTION, T_CLOSURE, or T_USE.
* type T_FUNCTION, T_CLOSURE, T_USE,
* or T_FN.
*/
public function getMethodParameters($stackPtr)
{
if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION
&& $this->tokens[$stackPtr]['code'] !== T_CLOSURE
&& $this->tokens[$stackPtr]['code'] !== T_USE
&& $this->tokens[$stackPtr]['code'] !== T_FN
) {
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_USE');
throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_USE or T_FN');
}

if ($this->tokens[$stackPtr]['code'] === T_USE) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ function nullableTypeHint(?int $var1, ?\bar $var2) {}

/* testBitwiseAndConstantExpressionDefaultValue */
function myFunction($a = 10 & 20) {}

/* testArrowFunction */
fn(int $a, ...$b) => $b;
33 changes: 32 additions & 1 deletion tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@ public function testBitwiseAndConstantExpressionDefaultValue()
}//end testBitwiseAndConstantExpressionDefaultValue()


/**
* Verify that arrow functions are supported.
*
* @return void
*/
public function testArrowFunction()
{
$expected = [];
$expected[0] = [
'name' => '$a',
'content' => 'int $a',
'pass_by_reference' => false,
'variable_length' => false,
'type_hint' => 'int',
'nullable_type' => false,
];

$expected[1] = [
'name' => '$b',
'content' => '...$b',
'pass_by_reference' => false,
'variable_length' => true,
'type_hint' => '',
'nullable_type' => false,
];

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

}//end testArrowFunction()


/**
* Test helper.
*
Expand All @@ -253,7 +284,7 @@ public function testBitwiseAndConstantExpressionDefaultValue()
*/
private function getMethodParametersTestHelper($commentString, $expected)
{
$function = $this->getTargetToken($commentString, [T_FUNCTION]);
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_FN]);
$found = self::$phpcsFile->getMethodParameters($function);

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

0 comments on commit b74e813

Please sign in to comment.