UseStatements::splitImportUseStatement(): compatibility with the PHP 8 identifier name tokenization #212
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As the PHP 8 identifier name tokens will not always exist in all supported PHP/PHPCS versions, the token code comparison needs to be switched to a token type comparison to prevent "unknown constant" errors.
Additionally, the
UseStatements::splitImportUseStatement()
method uses the last part of an imported name as the alias/array index key if no alias is given.For the PHP 5/7 tokenization, this was handled by overwriting the
$alias
while the method was still going through the various parts of the name until it found the end.For the PHP 8 tokenization, however, the various parts of the name are now all squashed together into one token, so we need to extract the last part of the name from the (fully) qualified name to get to the "alias".
As an extra issue to deal with, PHPCS re-tokenizes the next token after a
function
/const
keyword toT_STRING
. This is primarily intended for function/constant declarations, however, it affects import use statements for functions/constants as well if thefunction
/const
keyword is part of a groupuse
statement.That means that, for all PHPCS versions prior to the PHPCS version in which this will be fixed (which is expected to be version 3.5.7 as part of upstream PR 3063),
T_STRING
tokens in a group use statement may in fact be PHP 8.0 identifier name tokens and may also need to be split to extract the last part of the name from the complete name to get the alias.The existing unit test cover all the above situations.