|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCompatibility, an external standard for PHP_CodeSniffer. |
| 4 | + * |
| 5 | + * @package PHPCompatibility |
| 6 | + * @copyright 2012-2022 PHPCompatibility Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCompatibility/PHPCompatibility |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Magento2\Sniffs\PHPCompatibility; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHPCompatibility\Sniff; |
| 15 | +use PHPCSUtils\Utils\GetTokensAsString; |
| 16 | +use PHPCSUtils\Utils\TextStrings; |
| 17 | + |
| 18 | +/** |
| 19 | + * Detect use of select forms of variable embedding in heredocs and double strings as deprecated per PHP 8.2. |
| 20 | + * |
| 21 | + * > PHP allows embedding variables in strings with double-quotes (") and heredoc in various ways. |
| 22 | + * > 1. Directly embedding variables (`$foo`) |
| 23 | + * > 2. Braces outside the variable (`{$foo}`) |
| 24 | + * > 3. Braces after the dollar sign (`${foo}`) |
| 25 | + * > 4. Variable variables (`${expr}`, equivalent to `(string) ${expr}`) |
| 26 | + * > |
| 27 | + * > [...] to deprecate options 3 and 4 in PHP 8.2 and remove them in PHP 9.0. |
| 28 | + * |
| 29 | + * PHP version 8.2 |
| 30 | + * PHP version 9.0 |
| 31 | + * |
| 32 | + * @link https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation |
| 33 | + * |
| 34 | + * @since 10.0.0 |
| 35 | + */ |
| 36 | +class RemovedDollarBraceStringEmbedsSniff extends Sniff |
| 37 | +{ |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns an array of tokens this test wants to listen for. |
| 41 | + * |
| 42 | + * @since 10.0.0 |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + public function register() |
| 47 | + { |
| 48 | + return [ |
| 49 | + \T_DOUBLE_QUOTED_STRING, |
| 50 | + \T_START_HEREDOC, |
| 51 | + \T_DOLLAR_OPEN_CURLY_BRACES, |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Processes this test, when one of its tokens is encountered. |
| 57 | + * |
| 58 | + * @since 10.0.0 |
| 59 | + * |
| 60 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 61 | + * @param int $stackPtr The position of the current token in the |
| 62 | + * stack passed in $tokens. |
| 63 | + * |
| 64 | + * @return void|int Void or a stack pointer to skip forward. |
| 65 | + */ |
| 66 | + public function process(File $phpcsFile, $stackPtr) |
| 67 | + { |
| 68 | + if ($this->supportsAbove('8.2') === false) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $tokens = $phpcsFile->getTokens(); |
| 73 | + |
| 74 | + /* |
| 75 | + * Defensive coding, this code is not expected to ever actually be hit since PHPCS#3604 |
| 76 | + * (included in 3.7.0), but _will_ be hit if a file containing a PHP 7.3 indented heredoc/nowdocs |
| 77 | + * is scanned with PHPCS on PHP < 7.3. People shouldn't do that, but hey, we can't stop them. |
| 78 | + */ |
| 79 | + if ($tokens[$stackPtr]['code'] === \T_DOLLAR_OPEN_CURLY_BRACES) { |
| 80 | + // @codeCoverageIgnoreStart |
| 81 | + if ($tokens[($stackPtr - 1)]['code'] === \T_DOUBLE_QUOTED_STRING) { |
| 82 | + --$stackPtr; |
| 83 | + } else { |
| 84 | + // Throw an error anyway, though it won't be very informative. |
| 85 | + $message = 'Using ${} in strings is deprecated since PHP 8.2, use {$var} or {${expr}} instead.'; |
| 86 | + $code = 'DeprecatedDollarBraceEmbed'; |
| 87 | + $phpcsFile->addWarning($message, $stackPtr, $code); |
| 88 | + return; |
| 89 | + } |
| 90 | + // @codeCoverageIgnoreEnd |
| 91 | + } |
| 92 | + |
| 93 | + $endOfString = TextStrings::getEndOfCompleteTextString($phpcsFile, $stackPtr); |
| 94 | + $startOfString = $stackPtr; |
| 95 | + if ($tokens[$stackPtr]['code'] === \T_START_HEREDOC) { |
| 96 | + $startOfString = ($stackPtr + 1); |
| 97 | + } |
| 98 | + |
| 99 | + $contents = GetTokensAsString::normal($phpcsFile, $startOfString, $endOfString); |
| 100 | + if (\strpos($contents, '${') === false) { |
| 101 | + // No interpolation found or only interpolations which are still supported. |
| 102 | + return ($endOfString + 1); |
| 103 | + } |
| 104 | + |
| 105 | + $embeds = TextStrings::getEmbeds($contents); |
| 106 | + foreach ($embeds as $offset => $embed) { |
| 107 | + if (\strpos($embed, '${') !== 0) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + // Figure out the stack pointer to throw the warning on. |
| 112 | + $errorPtr = $startOfString; |
| 113 | + $length = 0; |
| 114 | + while (($length + $tokens[$errorPtr]['length']) < $offset) { |
| 115 | + $length += $tokens[$errorPtr]['length']; |
| 116 | + ++$errorPtr; |
| 117 | + } |
| 118 | + |
| 119 | + // Type 4. |
| 120 | + $message = 'Using %s (variable variables) in strings is deprecated since PHP 8.2, use {${expr}} instead.'; |
| 121 | + $code = 'DeprecatedExpressionSyntax'; |
| 122 | + if (\preg_match('`^\$\{(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)(?:\[([\'"])?[^\$\{\}\]]+(?:\2)?\])?\}$`', $embed) === 1) { |
| 123 | + // Type 3. |
| 124 | + $message = 'Using ${var} in strings is deprecated since PHP 8.2, use {$var} instead. Found: %s'; |
| 125 | + $code = 'DeprecatedVariableSyntax'; |
| 126 | + } |
| 127 | + |
| 128 | + $phpcsFile->addWarning($message, $errorPtr, $code, [$embed]); |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + return ($endOfString + 1); |
| 133 | + } |
| 134 | +} |
0 commit comments