Skip to content

Commit e866f6d

Browse files
committed
Fixed bug #2975 : Undefined offset in PSR12.Functions.ReturnTypeDeclaration when checking function return type inside ternary
1 parent e976278 commit e866f6d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ function functionName(?string $arg1, ?int &$arg2):
6262
?string {}
6363

6464
fn (?\DateTime $arg) : ?\DateTime => $arg;
65+
66+
return (!$a ? [ new class { public function b(): c {} } ] : []);

src/Standards/PSR12/Tests/Functions/ReturnTypeDeclarationUnitTest.inc.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ function functionName(?string $arg1, ?int &$arg2): ?string {}
5858
function functionName(?string $arg1, ?int &$arg2): ?string {}
5959

6060
fn (?\DateTime $arg): ?\DateTime => $arg;
61+
62+
return (!$a ? [ new class { public function b(): c {} } ] : []);

src/Tokenizers/PHP.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ function return types. We want to keep the parenthesis map clean,
15741574
// Convert colons that are actually the ELSE component of an
15751575
// inline IF statement.
15761576
if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) {
1577-
// Make sure this isn't the return type separator of a closure.
1577+
// Make sure this isn't a return type separator.
15781578
$isInlineIf = true;
15791579
for ($i = ($stackPtr - 1); $i > 0; $i--) {
15801580
if (is_array($tokens[$i]) === false
@@ -1600,12 +1600,15 @@ function return types. We want to keep the parenthesis map clean,
16001600
}
16011601

16021602
// We've found the open parenthesis, so if the previous
1603-
// non-empty token is FUNCTION or USE, this is a closure.
1603+
// non-empty token is FUNCTION or USE, this is a return type.
1604+
// Note that we need to skip T_STRING tokens here as these
1605+
// can be function names.
16041606
for ($i--; $i > 0; $i--) {
16051607
if (is_array($tokens[$i]) === false
16061608
|| ($tokens[$i][0] !== T_DOC_COMMENT
16071609
&& $tokens[$i][0] !== T_COMMENT
1608-
&& $tokens[$i][0] !== T_WHITESPACE)
1610+
&& $tokens[$i][0] !== T_WHITESPACE
1611+
&& $tokens[$i][0] !== T_STRING)
16091612
) {
16101613
break;
16111614
}
@@ -1614,7 +1617,7 @@ function return types. We want to keep the parenthesis map clean,
16141617
if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_FN || $tokens[$i][0] === T_USE) {
16151618
$isInlineIf = false;
16161619
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1617-
echo "\t\t* token is function return type, not T_INLINE_ELSE".PHP_EOL;
1620+
echo "\t\t* token is return type, not T_INLINE_ELSE".PHP_EOL;
16181621
}
16191622
}
16201623
}//end if

0 commit comments

Comments
 (0)