Skip to content

Tokenizer: improve tokenization of T_NULLABLE vs T_INLINE_THEN #2794

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ class TestTokenizingOfNullableVsInlineThen {
}
}

// Issue #2641.
$foo = new static(
is_null($a) ? foo($a) : $a,
is_null($b) ? $b : $c
);

// Issue #2791.
class testInstanceOf() {
function testIt() {
$foo = $value instanceof static ? '(' . $value . ')' : $value;
$bar = $value instanceof static ? function_call($value) : $value;
$baz = $value instanceof static ? array($value) : $value;
$bal = $value instanceof static ? \className::$property : $value;
$bal = $value instanceof static ? CONSTANT_NAME : $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,19 @@ class TestTokenizingOfNullableVsInlineThen {
}
}

// Issue #2641.
$foo = new static(
is_null($a) ? foo($a) : $a,
is_null($b) ? $b : $c
);

// Issue #2791.
class testInstanceOf() {
function testIt() {
$foo = $value instanceof static ? '(' . $value . ')' : $value;
$bar = $value instanceof static ? function_call($value) : $value;
$baz = $value instanceof static ? array($value) : $value;
$bal = $value instanceof static ? \className::$property : $value;
$bal = $value instanceof static ? CONSTANT_NAME : $value;
}
}
59 changes: 59 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,65 @@ protected function tokenize($string)
$newToken = [];
$newToken['content'] = '?';

/*
* Check if the next non-empty token is one of the tokens which can be used
* in type declarations. If not, it's definitely a ternary.
* At this point, the only token types which need to be taken into consideration
* as potential type declarations are T_STRING, T_ARRAY, T_CALLABLE and T_NS_SEPARATOR.
*/

$lastRelevantNonEmpty = null;

for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === true) {
$tokenType = $tokens[$i][0];
} else {
$tokenType = $tokens[$i];
}

if (isset(Util\Tokens::$emptyTokens[$tokenType]) === true) {
continue;
}

if ($tokenType === T_STRING
|| $tokenType === T_ARRAY
|| $tokenType === T_NS_SEPARATOR
) {
$lastRelevantNonEmpty = $tokenType;
continue;
}

if (($tokenType !== T_CALLABLE
&& isset($lastRelevantNonEmpty) === false)
|| ($lastRelevantNonEmpty === T_ARRAY
&& $tokenType === '(')
|| ($lastRelevantNonEmpty === T_STRING
&& ($tokenType === T_DOUBLE_COLON
|| $tokenType === '('
|| $tokenType === ':'))
) {
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t* token $stackPtr changed from ? to T_INLINE_THEN".PHP_EOL;
}

$newToken['code'] = T_INLINE_THEN;
$newToken['type'] = 'T_INLINE_THEN';

$insideInlineIf[] = $stackPtr;

$finalTokens[$newStackPtr] = $newToken;
$newStackPtr++;
continue 2;
}

break;
}//end for

/*
* This can still be a nullable type or a ternary.
* Do additional checking.
*/

$prevNonEmpty = null;
$lastSeenNonEmpty = null;

Expand Down