Conversation
…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
18 tasks
wimg
approved these changes
Dec 5, 2022
|
seems it cannot generate diff-fixes ( |
Member
Author
|
@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). |
|
@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 |
Member
Author
|
@divinity76 Good to know. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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).
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:
${squizlabs/PHP_CodeSniffer#3604Related to #1348