Skip to content

Tokenizer/PHP: change tokenization of long PHP open tags #1015

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
merged 1 commit into from
Apr 17, 2025

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 15, 2025

Description

⚠️ UPDATED: This PR depends on PR #1010, which needs to be merged first. ⚠️


Change tokenization of long PHP open tags to no longer include whitespace (single space/new line).

This ensures the tokenization of all PHP open tags - long open tags <?php, short open tags <? and short open echo tags <?= - is consistent and should simplify handling of open tag tokens in sniffs and lower the cognitive load of sniff developers.

As per the detailed proposal in ticket #593.

Includes extensive unit tests, both ensuring the correct tokenization, as well as safeguarding consistency across the different open tags.

Includes the following additional updates to maintain the pre-existing behaviour:

  • AbstractPatternSniff The AbstractPatternSniff tokenizes an arbitrary code pattern to determine the token pattern to sniff for. This tokenization now to takes the change in the PHP open tag tokenization into account.
  • Generic/EmptyPHPStatement - replace now redundant special condition for PHP open tags + update "fixed" file for improved output + add extra test.
  • PSR12/OpenTag - allow for the changed tokenization. The updated code also means that the fixer will no longer leave stray whitespace behind after a PHP open tag.
  • Squiz/CommentedOutCode - allow for the changed tokenization.
  • Squiz/EmbeddedPhp - code simplification.
  • FindStartOfStatementTest - fixed token offset expectation as there is now an extra whitespace token.
  • ReplaceTabsInTokenMiscTest - updated target finding

Suggested changelog entry

Changed:

Related issues/external references

Fixes #593

@jrfnl jrfnl added this to the 4.0.0 milestone Apr 15, 2025
@jrfnl jrfnl force-pushed the phpcs-4.0/feature/593-tokenizer-php-open-tag-consistency branch from c6f0061 to f0ee904 Compare April 16, 2025 00:04
@jrfnl jrfnl force-pushed the phpcs-4.0/feature/sq-1612-stdout-vs-stderr branch from a78b0dd to c3fc316 Compare April 17, 2025 09:00
@jrfnl jrfnl force-pushed the phpcs-4.0/feature/sq-1612-stdout-vs-stderr branch 4 times, most recently from 547a345 to 93ce29b Compare April 17, 2025 13:00
Base automatically changed from phpcs-4.0/feature/sq-1612-stdout-vs-stderr to 4.x April 17, 2025 13:09
@jrfnl jrfnl force-pushed the phpcs-4.0/feature/593-tokenizer-php-open-tag-consistency branch from f0ee904 to f7b079f Compare April 17, 2025 14:30
@jrfnl
Copy link
Member Author

jrfnl commented Apr 17, 2025

Rebased without changes after the merge of #1010, which also gets rid of the merge conflict.

... to no longer include whitespace (single space/new line).

This ensures the tokenization of all PHP open tags - long open tags `<?php`, short open tags `<?` and short open echo tags `<?=` - is consistent and should simplify handling of open tag tokens in sniffs and lower the cognitive load of sniff developers.

As per the detailed proposal in ticket 593.

Includes extensive unit tests, both ensuring the correct tokenization, as well as safeguarding consistency across the different open tags.

Includes the following additional updates to maintain the pre-existing behaviour:
* `AbstractPatternSniff`
    The `AbstractPatternSniff` tokenizes an arbitrary code pattern to determine the token pattern to sniff for. This tokenization now to takes the change in the PHP open tag tokenization into account.
* `Generic/EmptyPHPStatement` - replace now redundant special condition for PHP open tags + update "fixed" file for improved output + add extra test.
* `PSR12/OpenTag` - allow for the changed tokenization.
    The updated code also means that the fixer will no longer leave stray whitespace behind after a PHP open tag.
* `Squiz/CommentedOutCode` - allow for the changed tokenization.
* `Squiz/EmbeddedPhp` - code simplification.
* `FindStartOfStatementTest` - fixed token offset expectation as there is now an extra whitespace token.
* `ReplaceTabsInTokenMiscTest` - updated target finding

Fixes 593
@jrfnl jrfnl force-pushed the phpcs-4.0/feature/593-tokenizer-php-open-tag-consistency branch from f7b079f to b00f2e4 Compare April 17, 2025 15:09
@jrfnl
Copy link
Member Author

jrfnl commented Apr 17, 2025

Found a potential edge case bug while talking it through. Fixed now.

@jrfnl jrfnl merged commit 8c0f336 into 4.x Apr 17, 2025
60 checks passed
@jrfnl jrfnl deleted the phpcs-4.0/feature/593-tokenizer-php-open-tag-consistency branch April 17, 2025 15:20
@rodrigoprimo rodrigoprimo requested review from rodrigoprimo and removed request for rodrigoprimo April 17, 2025 15:21
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