Open
Description
Is your feature request related to a problem?
Simplistic example:
$text = 'The product is free';
$price = '$0.5';
echo preg_replace( '/is free/', 'costs ' . $price, $text ) . PHP_EOL;
// wrong:
// The product costs is free.5
echo preg_replace( '/is free/', addcslashes( 'costs ' . $price, '\\$' ), $text ) . PHP_EOL;
// correct:
// The product costs $0.5
In practice anytime you use a variable as the replacement, if it contains \
or $
followed by a number, preg replace will convert this to a back replacement group.
Since this is an extremely rare occurence, most people don't know that this can possibly happen or aren't aware.
Describe the solution you'd like
If a preg_replace call uses a variable as 2nd argument, the whole replacement arg or each variable in it must be wrapped in addcslashes( $variable, '\\$' )
to fix this issue.
Additional context (optional)
If this rule is something you think is better added in another phpcs ruleset, please let me know.
- I intend to create a pull request to implement this feature.