Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php55\Rector\FuncCall\PregReplaceEModifierRector\Fixture;

class CallFunctionVariableHexBackreference
{
public function run()
{
$result = preg_replace('/%u([a-f0-9]{4,4})/ei', 'utf8_chr(0x$1)', $_REQUEST['content']);
}
}

?>
-----
<?php

namespace Rector\Tests\Php55\Rector\FuncCall\PregReplaceEModifierRector\Fixture;

class CallFunctionVariableHexBackreference
{
public function run()
{
$result = preg_replace_callback('/%u([a-f0-9]{4,4})/i', function ($matches) {
return utf8_chr(hexdec($matches[1]));
}, $_REQUEST['content']);
}
}

?>
19 changes: 19 additions & 0 deletions src/PhpParser/Parser/InlineCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
*/
private const string BACKREFERENCE_NO_DOUBLE_QUOTE_START_REGEX = '#(?<!")(?<backreference>\$\d+)#';

/**
* @see https://regex101.com/r/13mVVg/1
*/
private const string HEX_BACKREFERENCE_REGEX = '#0x(?<backreference>\$\d+)#';

public function __construct(
private BetterStandardPrinter $betterStandardPrinter,
private SimplePhpParser $simplePhpParser,
Expand Down Expand Up @@ -81,6 +86,20 @@ public function parseString(string $fileContent): array
public function stringify(Expr $expr): string
{
if ($expr instanceof String_) {
if (! str_contains($expr->value, "'") && ! str_contains($expr->value, '"') && StringUtils::isMatch(
$expr->value,
self::HEX_BACKREFERENCE_REGEX
)) {
return Strings::replace(
$expr->value,
self::HEX_BACKREFERENCE_REGEX,
static function (array $match): string {
$number = ltrim((string) $match['backreference'], '\\$');
return 'hexdec($matches[' . $number . '])';
}
);
}

if (! StringUtils::isMatch($expr->value, self::BACKREFERENCE_NO_QUOTE_REGEX)) {
return Strings::replace(
$expr->value,
Expand Down