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.2 | New PHPCompatibility.TextStrings.RemovedDollarBraceStringEmbeds sniff #1424

Merged
merged 1 commit into from Dec 5, 2022

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 5, 2022

PHP 8.2 will be deprecating two out of the four ways to embed variables and expressions in text strings (heredocs and double quoted strings).

PHP allows embedding variables in strings with double-quotes (") and heredoc in various ways.

  1. Directly embedding variables (“$foo”)
  2. Braces outside the variable (“{$foo}”)
  3. Braces after the dollar sign (“${foo}”)
  4. Variable variables (“${expr}”, equivalent to (string) ${expr})

Options 1 and 2 have their pros and cons. Options 3 and 4 are easily confused due to overlapping syntax, 3 is strictly less capable than 1 and 2, and 4 has completely different semantics (variable variables) that are rarely useful in string interpolation.

This RFC proposes to deprecate options 3 and 4 in PHP 8.2 and remove them in PHP 9.0.

This commit introduces a new sniff to detect this deprecation.

Analyzing code for this deprecation was affected by a bug in the PHPCS Tokenizer discovered while writing this sniff. As of PHPCSUtils 1.0.0-alpha4, the utility methods in PHPCSUtils include a work-around for this bug and are stable for our purposes.

Also note that while the included unit tests should absolutely be sufficient for our purposes, the PHPCSUtils methods used to retrieve embedded variables and expressions are tested in-depth (> 1000 tests for that functionality alone), so we should be good ;-)

Last note: the tests are split in two files as any code after a PHP 7.3 heredoc/nowdoc will not tokenize correctly on PHP < 7.3. Splitting the tests into two files works as a work-around for that.

Includes unit tests.
Includes sniff documentation.

Refs:

Related to #1348

…gEmbeds` sniff

PHP 8.2 will be deprecating two out of the four ways to embed variables and expressions in text strings (heredocs and double quoted strings).

>  PHP allows embedding variables in strings with double-quotes (") and heredoc in various ways.
> 1. Directly embedding variables (“$foo”)
> 2. Braces outside the variable (“{$foo}”)
> 3. Braces after the dollar sign (“${foo}”)
> 4. Variable variables (“${expr}”, equivalent to (string) ${expr})
>
> Options 1 and 2 have their pros and cons. Options 3 and 4 are easily confused due to overlapping syntax, 3 is strictly less capable than 1 and 2, and 4 has completely different semantics (variable variables) that are rarely useful in string interpolation.
>
> This RFC proposes to deprecate options 3 and 4 in PHP 8.2 and remove them in PHP 9.0.

This commit introduces a new sniff to detect this deprecation.

Analyzing code for this deprecation was affected by a bug in the PHPCS Tokenizer discovered while writing this sniff. As of PHPCSUtils 1.0.0-alpha4, the utility methods in PHPCSUtils include a work-around for this bug and are stable for our purposes.

Also note that while the included unit tests should absolutely be sufficient for our purposes, the PHPCSUtils methods used to retrieve embedded variables and expressions are tested **_in-depth_** (> 1000 tests for that functionality alone), so we should be good ;-)

Last note: the tests are split in two files as any code after a PHP 7.3 heredoc/nowdoc will not tokenize correctly on PHP < 7.3. Splitting the tests into two files works as a work-around for that.

Includes unit tests.
Includes sniff documentation.

Refs:
* https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation
* php/php-src 8212
* https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
* https://gist.github.com/iluuu1994/72e2154fc4150f2258316b0255b698f2
* squizlabs/PHP_CodeSniffer 3604
@wimg wimg merged commit c59371d into develop Dec 5, 2022
@wimg wimg deleted the php-8.2/new-removeddollarbraceembeds-sniff branch December 5, 2022 12:11
@divinity76
Copy link

seems it cannot generate diff-fixes (--report=diff) thought 🤔

@jrfnl
Copy link
Member Author

jrfnl commented Mar 4, 2024

@divinity76 Nearly all things flagged by PHPCompatibility will need a dev's attention to decide what and how to fix it, so no, you will not see auto-fixers included in these sniffs and rightfully so (with only one or two exceptions).

@divinity76
Copy link

divinity76 commented Mar 29, 2024

@jrfnl an automatic fixer has been made for Rector: rectorphp/rector-src#5781

Edit : but Rector is a project with substantial infrastructure for this sort of task, and aligns perfectly with the Rector project goals

@jrfnl
Copy link
Member Author

jrfnl commented Mar 30, 2024

@divinity76 Good to know.

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

3 participants