Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve EregToPcreTransformer complexity #2915

Merged
merged 1 commit into from
Feb 23, 2020
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
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,6 @@ parameters:

- '#Method Rector\\BetterPhpDocParser\\PhpDocNode\\JMS\\JMSInjectParamsTagValueNode\:\:__toString\(\) should return string but returns string\|null#'
- '#Method Rector\\BetterPhpDocParser\\PhpDocNode\\JMS\\JMSServiceValueNode\:\:__toString\(\) should return string but returns string\|null#'

- '#Strict comparison using \=\=\= between mixed and (.*?) will always evaluate to false#'
- '#Parameter \#1 \$c of method Rector\\Php70\\EregToPcreTransformer\:\:_ere2pcre_escape\(\) expects string, mixed given#'
154 changes: 84 additions & 70 deletions rules/php-70/src/EregToPcreTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/**
* @author Kang Seonghoon <public+ere2pcre@mearie.org>
* @source https://gist.github.com/lifthrasiir/704754/7e486f43e62fd1c9d3669330c251f8ca4a59a3f8
* @see \Rector\Php\Tests\EregToPcreTransformerTest
*
* @see \Rector\Php70\Tests\EregToPcreTransformerTest
*/
final class EregToPcreTransformer
{
Expand Down Expand Up @@ -116,48 +117,10 @@ private function _ere2pcre(string $s, int $i): array
throw new InvalidEregException('"[" does not have a matching "]"');
}
$start = true;
do {
if ($s[$i] === '[' &&
$i + 1 < $l && Strings::contains('.=:', $s[$i + 1])) {
$offset = (int) $i;
$ii = strpos($s, ']', $offset);
if ($ii === false) {
throw new InvalidEregException('"[" does not have a matching "]"');
}

$start = (int) $i + 1;
$length = (int) ($ii - ($i + 1));
$ccls = Strings::substring($s, $start, $length);
if (! isset(self::CCLSMAP[$ccls])) {
throw new InvalidEregException(
'an invalid or unsupported character class [' . $ccls . ']'
);
}
$cls .= self::CCLSMAP[$ccls];
$i = $ii + 1;
} else {
$a = $s[$i++];
if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) {
throw new InvalidEregException(
'"-" is invalid for the start character in the brackets'
);
}
if ($i < $l && $s[$i] === '-') {
++$i;
$b = $s[$i++];
if ($b === ']') {
$cls .= $this->_ere2pcre_escape($a) . '\-';
break;
} elseif (ord($a) > ord($b)) {
throw new InvalidEregException(sprintf('an invalid character range %d-%d"', $a, $b));
}
$cls .= $this->_ere2pcre_escape($a) . '-' . $this->_ere2pcre_escape($b);
} else {
$cls .= $this->_ere2pcre_escape($a);
}
}
$start = false;
} while ($i < $l && $s[$i] !== ']');
$i = (int) $i;
[$cls, $i] = $this->processSquareBracket($s, $i, $l, $cls, $start);

if ($i >= $l) {
throw new InvalidEregException('"[" does not have a matching "]"');
}
Expand Down Expand Up @@ -205,35 +168,8 @@ private function _ere2pcre(string $s, int $i): array
++$i;
} elseif ($c === '{') {
$i = (int) $i;
$ii = strpos($s, '}', $i);
if ($ii === false) {
throw new InvalidEregException('"{" does not have a matching "}"');
}

$start = (int) $i + 1;
$length = (int) $ii - ($i + 1);
$bound = Strings::substring($s, $start, $length);

$m = Strings::match($bound, '/^(\d|[1-9]\d|1\d\d|
2[0-4]\d|25[0-5])
(,(\d|[1-9]\d|1\d\d|
2[0-4]\d|25[0-5])?)?$/x');

if (! $m) {
throw new InvalidEregException('an invalid bound');
}

if (isset($m[3])) {
if ($m[1] > $m[3]) {
throw new InvalidEregException('an invalid bound');
}
$r[$rr] .= '{' . $m[1] . ',' . $m[3] . '}';
} elseif (isset($m[2])) {
$r[$rr] .= '{' . $m[1] . ',}';
} else {
$r[$rr] .= '{' . $m[1] . '}';
}
$i = $ii + 1;
[$start, $i] = $this->processCurlyBracket($s, $i, $r, $rr);
}
}
if ($r[$rr] === '') {
Expand All @@ -253,4 +189,82 @@ private function _ere2pcre_escape(string $c): string

return $c;
}

private function processSquareBracket(string &$s, int &$i, int &$l, string &$cls, bool &$start): array
{
do {
if ($s[$i] === '[' &&
$i + 1 < $l && Strings::contains('.=:', $s[$i + 1])) {
$offset = (int) $i;
$ii = strpos($s, ']', $offset);
if ($ii === false) {
throw new InvalidEregException('"[" does not have a matching "]"');
}

$start = (int) $i + 1;
$length = (int) ($ii - ($i + 1));
$ccls = Strings::substring($s, $start, $length);
if (! isset(self::CCLSMAP[$ccls])) {
throw new InvalidEregException('an invalid or unsupported character class [' . $ccls . ']');
}
$cls .= self::CCLSMAP[$ccls];
$i = $ii + 1;
} else {
$a = $s[$i++];
if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) {
throw new InvalidEregException('"-" is invalid for the start character in the brackets');
}
if ($i < $l && $s[$i] === '-') {
++$i;
$b = $s[$i++];
if ($b === ']') {
$cls .= $this->_ere2pcre_escape($a) . '\-';
break;
} elseif (ord($a) > ord($b)) {
throw new InvalidEregException(sprintf('an invalid character range %d-%d"', $a, $b));
}
$cls .= $this->_ere2pcre_escape($a) . '-' . $this->_ere2pcre_escape($b);
} else {
$cls .= $this->_ere2pcre_escape($a);
}
}
$start = false;
} while ($i < $l && $s[$i] !== ']');

return [$cls, $i];
}

private function processCurlyBracket(string $s, int $i, array &$r, int $rr): array
{
$ii = strpos($s, '}', $i);
if ($ii === false) {
throw new InvalidEregException('"{" does not have a matching "}"');
}

$start = (int) $i + 1;
$length = (int) $ii - ($i + 1);
$bound = Strings::substring($s, $start, $length);

$m = Strings::match($bound, '/^(\d|[1-9]\d|1\d\d|
2[0-4]\d|25[0-5])
(,(\d|[1-9]\d|1\d\d|
2[0-4]\d|25[0-5])?)?$/x');
if (! $m) {
throw new InvalidEregException('an invalid bound');
}

if (isset($m[3])) {
if ($m[1] > $m[3]) {
throw new InvalidEregException('an invalid bound');
}
$r[$rr] .= '{' . $m[1] . ',' . $m[3] . '}';
} elseif (isset($m[2])) {
$r[$rr] .= '{' . $m[1] . ',}';
} else {
$r[$rr] .= '{' . $m[1] . '}';
}
$i = $ii + 1;

return [$start, $i];
}
}
1 change: 1 addition & 0 deletions rules/php-70/src/Rector/FuncCall/EregToPregMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @see http://php.net/reference.pcre.pattern.posix
* @see https://stackoverflow.com/a/17033826/1348344
* @see https://docstore.mik.ua/orelly/webprog/pcook/ch13_02.htm
*
* @see \Rector\Php70\Tests\Rector\FuncCall\EregToPregMatchRector\EregToPregMatchRectorTest
*/
final class EregToPregMatchRector extends AbstractRector
Expand Down