Skip to content

Commit

Permalink
[Php70] More delimiter case in the middle fix on EregToPregMatchRector (
Browse files Browse the repository at this point in the history
#6357)

* [Php70] More delimiter case in the middle fix on EregToPregMatchRector

* [Php70] More delimiter case in the middle fix on EregToPregMatchRector
  • Loading branch information
samsonasik authored Oct 5, 2024
1 parent 3d76a1a commit cff2ed2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\EregToPregMatchRector\Fixture;

function withDelimiterInMiddle2($text)
{
return (bool) ereg('A# (minor|major)', $text);
}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\FuncCall\EregToPregMatchRector\Fixture;

function withDelimiterInMiddle2($text)
{
return (bool) preg_match('#A\# (minor|major)#m', $text);
}

?>
20 changes: 7 additions & 13 deletions rules/Php70/EregToPcreTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,8 @@ public function __construct(
) {
}

public function transform(string $ereg, bool $isCaseInsensitive): string
{
if (! \str_contains($ereg, $this->pcreDelimiter)) {
return $this->ere2pcre($ereg, $isCaseInsensitive);
}

// fallback
$quotedEreg = preg_quote($ereg, $this->pcreDelimiter);
return $this->ere2pcre($quotedEreg, $isCaseInsensitive);
}

// converts the ERE $s into the PCRE $r. triggers error on any invalid input.
private function ere2pcre(string $content, bool $ignorecase): string
public function transform(string $content, bool $ignorecase): string
{
if ($ignorecase) {
if (isset($this->icache[$content])) {
Expand Down Expand Up @@ -201,11 +190,16 @@ private function _ere2pcre(string $content, int $i): array
}

return [
str_replace($this->pcreDelimiter, '\\' . $this->pcreDelimiter, implode('|', $r)),
$this->normalize(implode('|', $r)),
$i
];
}

private function normalize(string $content): string
{
return str_replace($this->pcreDelimiter, '\\' . $this->pcreDelimiter, $content);
}

/**
* @param mixed[] $r
*/
Expand Down

0 comments on commit cff2ed2

Please sign in to comment.