Skip to content

Commit e3a0b4f

Browse files
authored
[Php70] Treat unescaped { as literal in EregToPcreTransformer (#8964)
A { that is not followed by a digit, e.g. ereg_replace('{[0-9]*}', ...), threw InvalidEregException and aborted the whole file. Like in the original ereg implementation it is now a literal brace.
1 parent 6b18f1f commit e3a0b4f

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php70\Rector\FuncCall\EregToPregMatchRector\Fixture;
4+
5+
function eregLiteralBrace()
6+
{
7+
return eregi_replace('{[0-9]*}', '', 'a{32}b');
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
namespace Rector\Tests\Php70\Rector\FuncCall\EregToPregMatchRector\Fixture;
15+
16+
function eregLiteralBrace()
17+
{
18+
return preg_replace('#\{[0-9]*\}#mi', '', 'a{32}b');
19+
}
20+
21+
?>

‎rules/Php70/EregToPcreTransformer.php‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,8 @@ private function _ere2pcre(string $content, int $i): array
132132
} elseif (in_array($char, ['*', '+', '?'], true)) {
133133
throw new InvalidEregException('unescaped metacharacter "' . $char . '"');
134134
} elseif ($char === '{') {
135-
if ($i + 1 < $l && \str_contains('0123456789', $content[$i + 1])) {
136-
$r[$rr] .= '\{';
137-
} else {
138-
throw new InvalidEregException('unescaped metacharacter "' . $char . '"');
139-
}
135+
// a "{" in atom position is a literal, e.g. "{[0-9]*}"
136+
$r[$rr] .= '\{';
140137
} elseif ($char === '.') {
141138
$r[$rr] .= $char;
142139
} elseif ($char === '^' || $char === '$') {

0 commit comments

Comments
 (0)