Skip to content
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

PHP 8.0 | Support union types in PHP Tokenizer and File class utility methods #3032

Merged
merged 10 commits into from
Nov 2, 2020
Prev Previous commit
Next Next commit
PHP 8.0 | Tokenizer/PHP: array return type keyword to T_STRING vs PHP…
…8 union types

`array` keywords used as return types in PHP 8 union types would only be correctly changed to `T_STRING` if they were the first type in the union.

Fixed now.

Includes adding `T_STATIC` to the array of allowed tokens. While previously it wasn't an issue that the token was not included in the array, it is necessary for the token to be there to support union types.

This change will be tested via the union type related tests for the `File::getMethodProperties()` method.
  • Loading branch information
jrfnl committed Sep 20, 2020
commit 8112f56b4ab79e744aa659b3d9de8634e333b099
9 changes: 6 additions & 3 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
@@ -1475,6 +1475,7 @@ function return types. We want to keep the parenthesis map clean,
T_SELF => T_SELF,
T_PARENT => T_PARENT,
T_NAMESPACE => T_NAMESPACE,
T_STATIC => T_STATIC,
T_NS_SEPARATOR => T_NS_SEPARATOR,
];

@@ -1509,12 +1510,14 @@ function return types. We want to keep the parenthesis map clean,
}//end for

// Any T_ARRAY tokens we find between here and the next
// token that can't be part of the return type need to be
// token that can't be part of the return type, need to be
// converted to T_STRING tokens.
for ($x; $x < $numTokens; $x++) {
if (is_array($tokens[$x]) === false || isset($allowed[$tokens[$x][0]]) === false) {
if ((is_array($tokens[$x]) === false && $tokens[$x] !== '|')
|| (is_array($tokens[$x]) === true && isset($allowed[$tokens[$x][0]]) === false)
) {
break;
} else if ($tokens[$x][0] === T_ARRAY) {
} else if (is_array($tokens[$x]) === true && $tokens[$x][0] === T_ARRAY) {
$tokens[$x][0] = T_STRING;

if (PHP_CODESNIFFER_VERBOSITY > 1) {