Skip to content

Commit 2672ddb

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

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
@@ -1379,7 +1379,7 @@ function return types. We want to keep the parenthesis map clean,
13791379
// Convert colons that are actually the ELSE component of an
13801380
// inline IF statement.
13811381
if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) {
1382-
// Make sure this isn't the return type separator of a closure.
1382+
// Make sure this isn't a return type separator.
13831383
$isInlineIf = true;
13841384
for ($i = ($stackPtr - 1); $i > 0; $i--) {
13851385
if (is_array($tokens[$i]) === false
@@ -1405,12 +1405,15 @@ function return types. We want to keep the parenthesis map clean,
14051405
}
14061406

14071407
// We've found the open parenthesis, so if the previous
1408-
// non-empty token is FUNCTION or USE, this is a closure.
1408+
// non-empty token is FUNCTION or USE, this is a return type.
1409+
// Note that we need to skip T_STRING tokens here as these
1410+
// can be function names.
14091411
for ($i--; $i > 0; $i--) {
14101412
if (is_array($tokens[$i]) === false
14111413
|| ($tokens[$i][0] !== T_DOC_COMMENT
14121414
&& $tokens[$i][0] !== T_COMMENT
1413-
&& $tokens[$i][0] !== T_WHITESPACE)
1415+
&& $tokens[$i][0] !== T_WHITESPACE
1416+
&& $tokens[$i][0] !== T_STRING)
14141417
) {
14151418
break;
14161419
}
@@ -1419,7 +1422,7 @@ function return types. We want to keep the parenthesis map clean,
14191422
if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_FN || $tokens[$i][0] === T_USE) {
14201423
$isInlineIf = false;
14211424
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1422-
Common::printStatusMessage('* token is function return type, not T_INLINE_ELSE', 2);
1425+
Common::printStatusMessage('* token is return type, not T_INLINE_ELSE', 2);
14231426
}
14241427
}
14251428
}//end if

0 commit comments

Comments
 (0)