-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move "interpolated variable" related utilities to dedicated `TextStri…
…ngHelper` The "interpolated variable" related utilities are only used by a small set of sniffs, so are better placed in a dedicated class. This commit moves the `REGEX_COMPLEX_VARS` constant, the `get_interpolated_variables()` method and the `strip_interpolated_variables()` method to a new `WordPressCS\WordPress\Helpers\TextStringHelper` and starts using that class in the relevant sniffs. In contrast to some of the other "move methods out of the Sniff class" PRs, these methods have been moved to a class and made `static` - instead of moved to a `trait`. The reason for this difference is that the methods in other "moves" are setting properties which the sniff classes would need access to, while these methods are 100% stand-alone. **Note**: It is expected for PHPCSUtils to have dedicated methods for the same at some point in the future. If/when those methods become available, it is recommended for the sniffs to start using the PHPCSUtils methods and for this class to be deprecated and removed in the next major release. Also note that PHP 8.2 will be making changes to the kind of interpolation supported by PHP. This will need further investigation at a future point in time (probably in PHPCSUtils rather than here), if nothing else to ensure that what was supported in PHP prior to PHP 8.2 is fully supported by these methods (which is probably not the case at this moment). Ref: https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation Related to 1465
- Loading branch information
Showing
7 changed files
with
94 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Helpers; | ||
|
||
/** | ||
* Helper utilities for handling text strings. | ||
* | ||
* {@internal The functionality in this class will likely be replaced at some point in | ||
* the future by functions from PHPCSUtils.} | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @since 3.0.0 The constant and methods in this class were previously contained in the | ||
* `WordPressCS\WordPress\Sniff` class and have been moved here. | ||
*/ | ||
final class TextStringHelper { | ||
|
||
/** | ||
* Regex to get complex variables from T_DOUBLE_QUOTED_STRING or T_HEREDOC. | ||
* | ||
* @since 0.14.0 | ||
* @since 3.0.0 Moved from the Sniff class to this class. | ||
* | ||
* @var string | ||
*/ | ||
const REGEX_COMPLEX_VARS = '`(?:(\{)?(?<!\\\\)\$)?(\{)?(?<!\\\\)\$(\{)?(?P<varname>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:->\$?(?P>varname)|\[[^\]]+\]|::\$?(?P>varname)|\([^\)]*\))*(?(3)\}|)(?(2)\}|)(?(1)\}|)`'; | ||
|
||
/** | ||
* Get the interpolated variable names from a string. | ||
* | ||
* Check if '$' is followed by a valid variable name, and that it is not preceded by an escape sequence. | ||
* | ||
* @since 0.9.0 | ||
* @since 3.0.0 - Moved from the Sniff class to this class. | ||
* - Visibility is now `public` (was `protected`) and the method `static`. | ||
* | ||
* @param string $string The contents of a T_DOUBLE_QUOTED_STRING or T_HEREDOC token. | ||
* | ||
* @return array Variable names (without '$' sigil). | ||
*/ | ||
public static function get_interpolated_variables( $string ) { | ||
$variables = array(); | ||
if ( preg_match_all( '/(?P<backslashes>\\\\*)\$(?P<symbol>\w+)/', $string, $match_sets, \PREG_SET_ORDER ) ) { | ||
foreach ( $match_sets as $matches ) { | ||
if ( ! isset( $matches['backslashes'] ) || ( \strlen( $matches['backslashes'] ) % 2 ) === 0 ) { | ||
$variables[] = $matches['symbol']; | ||
} | ||
} | ||
} | ||
return $variables; | ||
} | ||
|
||
/** | ||
* Strip variables from an arbitrary double quoted/heredoc string. | ||
* | ||
* Intended for use with the contents of a T_DOUBLE_QUOTED_STRING or T_HEREDOC token. | ||
* | ||
* @since 0.14.0 | ||
* @since 3.0.0 - Moved from the Sniff class to this class. | ||
* - The method was made `static`. | ||
* | ||
* @param string $string The raw string. | ||
* | ||
* @return string String without variables in it. | ||
*/ | ||
public static function strip_interpolated_variables( $string ) { | ||
if ( strpos( $string, '$' ) === false ) { | ||
return $string; | ||
} | ||
|
||
return preg_replace( self::REGEX_COMPLEX_VARS, '', $string ); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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