Skip to content

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 31, 2024

Description

You learn something new every day 😉

While probably exceedingly rare to be found in actual codebases, the PHP tokenizer apparently allows for whitespace between the <<< and the heredoc/nowdoc identifier. See: https://3v4l.org/NUHZd

Both spaces as well as tabs are allowed. New lines are not allowed. Comments are also not allowed. See: https://3v4l.org/7PIEK

The PHPCS Tokenizer did not execute tab replacement on these tokens leading to unexpected 'content' and incorrect 'length' values in the File::$tokens array, which in turn could lead to incorrect sniff results and incorrect fixes.

This commit adds the T_START_HEREDOC/T_START_NOWDOC tokens to the array of tokens for which to do tab replacement to make them more consistent with the rest of PHPCS.

Includes unit tests safeguarding this change.

Suggested changelog entry

Tokenizer not applying tab replacement to heredoc/nowdoc openers

Related issues/external references

Ref: https://externals.io/message/124462#124518

Loosely related to: squizlabs/PHP_CodeSniffer#3639 in which a similar change was made for heredoc/nowdoc closers (in the context of PHP 7.3+ flexible heredocs/nowdocs).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

_You learn something new every day ;-)_

While probably exceedingly rare to be found in actual codebases, the PHP tokenizer apparently allows for whitespace between the `<<<` and the heredoc/nowdoc identifier. See: https://3v4l.org/NUHZd

Both spaces as well as tabs are allowed. New lines are not allowed. Comments are also not allowed. See: https://3v4l.org/7PIEK

The PHPCS `Tokenizer` did not execute tab replacement on these tokens leading to unexpected `'content'` and incorrect `'length'` values in the `File::$tokens` array, which in turn could lead to incorrect sniff results and incorrect fixes.

This commit adds the `T_START_HEREDOC`/`T_START_NOWDOC` tokens to the array of tokens for which to do tab replacement to make them more consistent with the rest of PHPCS.

Includes unit tests safeguarding this change.

Ref: https://externals.io/message/124462#124518
@jrfnl jrfnl force-pushed the feature/tokenizer-bugfix-replace-tabs-in-heredoc-nowdoc-opener branch from 237cd58 to 51278fd Compare August 4, 2024 23:00
@jrfnl
Copy link
Member Author

jrfnl commented Aug 5, 2024

Rebased without changes. Merging once the builds has passed.

@jrfnl jrfnl merged commit 638cfb6 into master Aug 5, 2024
@jrfnl jrfnl deleted the feature/tokenizer-bugfix-replace-tabs-in-heredoc-nowdoc-opener branch August 5, 2024 00:27
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.

1 participant