Skip to content
Draft
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
10 changes: 9 additions & 1 deletion src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,8 @@ public function getMethodProperties(int $stackPtr)
T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
];

$hasColon = false;

for ($i = $this->tokens[$stackPtr]['parenthesis_closer']; $i < $this->numTokens; $i++) {
if (($scopeOpener === null && $this->tokens[$i]['code'] === T_SEMICOLON)
|| ($scopeOpener !== null && $i === $scopeOpener)
Expand All @@ -1800,11 +1802,17 @@ public function getMethodProperties(int $stackPtr)
continue;
}

if ($this->tokens[$i]['code'] === T_COLON) {
$hasColon = true;
continue;
}

if ($this->tokens[$i]['code'] === T_NULLABLE) {
$nullableReturnType = true;
continue;
}

if (isset($valid[$this->tokens[$i]['code']]) === true) {
if ($hasColon === true && isset($valid[$this->tokens[$i]['code']]) === true) {
if ($returnTypeToken === false) {
$returnTypeToken = $i;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Core/Files/File/GetMethodPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ $closure = function () use /*comment*/ ($a): Type {};
/* testClosureWithUseMultiParamWithReturnType */
$closure = function () use ($a, &$b, $c, $d, $e, $f, $g): ?array {};

/* testMissingColonParseError */
// Intentional parse error.
$closure = function () null { return null; };

/* testArrowFunctionLiveCoding */
// Intentional parse error. This has to be the last test in the file.
$fn = fn
25 changes: 25 additions & 0 deletions tests/Core/Files/File/GetMethodPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,31 @@ public function testClosureWithUseWithReturnType()
}


/**
* Test handling of a parse error where the colon is missing from the return type declaration.
*
* @return void
*/
public function testMissingColonParseError()
{
// Offsets are relative to the T_CLOSURE token.
$expected = [
'scope' => 'public',
'scope_specified' => false,
'return_type' => '',
'return_type_token' => false,
'return_type_end_token' => false,
'nullable_return_type' => false,
'is_abstract' => false,
'is_final' => false,
'is_static' => false,
'has_body' => true,
];

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


/**
* Test handling of closure declarations with a use variable import with a return type declaration.
*
Expand Down
Loading