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

UseStatements::splitImportUseStatement(): compatibility with the PHP 8 identifier name tokenization #212

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Sep 13, 2020

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 to T_STRING. This is primarily intended for function/constant declarations, however, it affects import use statements for functions/constants as well if the function/const keyword is part of a group use 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.

…8 identifier name tokenization

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 to `T_STRING`. This is primarily intended for function/constant declarations, however, it affects import use statements for functions/constants as well if the `function`/`const` keyword is part of a group `use` 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.
@jrfnl jrfnl added this to the 1.0.0-alpha4 milestone Sep 13, 2020
@jrfnl jrfnl merged commit cb58a41 into develop Sep 13, 2020
@jrfnl jrfnl deleted the php-8.0/usestatements-splitimportusestatement-identifier-name-compatibility branch September 13, 2020 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant