Skip to content

Tokenizer/PHP: bug fix for missing parenthesis owner for method named "fn" #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,8 @@ protected function tokenize($string)
if ($tokenIsArray === true
&& $token[0] === T_STRING
&& strtolower($token[1]) === 'fn'
&& isset(static::T_STRING_CONTEXTS[$finalTokens[$lastNotEmptyToken]['code']]) === false
&& $finalTokens[$lastNotEmptyToken]['content'] !== '&'
) {
// Modify the original token stack so that
// future checks (like looking for T_NULLABLE) can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ function name($a, $b/* testFunctionParenthesesCloser */) {}
function &returnByRef ($a, $b/* testFunctionParenthesesCloserReturnByRef */) {}

class ReservedKeyword {
/* testFunctionParenthesesOwnerKeywordName */
public function match($a, $b/* testFunctionParenthesesCloserKeywordName */) {}
/* testFunctionParenthesesOwnerKeywordNameMatch */
public function match($a, $b/* testFunctionParenthesesCloserKeywordNameMatch */) {}

/* testFunctionParenthesesOwnerKeywordNameFn */
public function fn($a, $b/* testFunctionParenthesesCloserKeywordNameFn */) {}

/* testFunctionParenthesesOwnerKeywordNameFnReturnByRef */
public function &fn($a, $b/* testFunctionParenthesesCloserKeywordNameFnReturnByRef */) {}
}

/* testClosureParenthesesOwner */
Expand Down
14 changes: 11 additions & 3 deletions tests/Core/Tokenizers/Tokenizer/CreateTokenMapParenthesesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,16 @@ public static function dataParenthesesWithOwner()
'testMarker' => '/* testFunctionParenthesesOwnerReturnByRef */',
'tokenCode' => T_FUNCTION,
],
'function declaration, keyword as function name' => [
'testMarker' => '/* testFunctionParenthesesOwnerKeywordName */',
'function declaration, "match" as function name' => [
'testMarker' => '/* testFunctionParenthesesOwnerKeywordNameMatch */',
'tokenCode' => T_FUNCTION,
],
'function declaration, "fn" as function name' => [
'testMarker' => '/* testFunctionParenthesesOwnerKeywordNameFn */',
'tokenCode' => T_FUNCTION,
],
'function declaration, "&fn" as function name' => [
'testMarker' => '/* testFunctionParenthesesOwnerKeywordNameFnReturnByRef */',
'tokenCode' => T_FUNCTION,
],
'closure declaration' => [
Expand Down Expand Up @@ -276,7 +284,7 @@ public static function dataParenthesesWithoutOwner()


/**
* Test parentheses owner tokens when used without parentheses (where possible) do *not* the "parenthesis_*" token indexes set.
* Test parentheses owner tokens when used without parentheses (where possible) do *not* have the "parenthesis_*" token indexes set.
*
* @param string $testMarker The comment prefacing the target token.
* @param int|string $tokenCode The token code to look for.
Expand Down