Skip to content

Commit

Permalink
infection#654 drop ereg functions in favoure of preg functions
Browse files Browse the repository at this point in the history
  • Loading branch information
majkel89 committed Mar 12, 2019
1 parent 733b40e commit f6f7ff4
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 80 deletions.
56 changes: 50 additions & 6 deletions src/Mutator/Extensions/MBString.php
Expand Up @@ -84,12 +84,12 @@ private function setupConverters(array $functionsMap): void
{
$converters = [
'mb_chr' => $this->mapNameSkipArg('chr', 1),
'mb_ereg_match' => $this->mapNameSkipArg('preg_match', 2),
'mb_ereg_replace_callback' => $this->mapNameSkipArg('preg_replace_callback', 3),
'mb_ereg_replace' => $this->mapNameSkipArg('ereg_replace', 3),
'mb_ereg' => $this->mapName('ereg'),
'mb_eregi_replace' => $this->mapNameSkipArg('eregi_replace', 3),
'mb_eregi' => $this->mapName('eregi'),
'mb_ereg_match' => $this->mapEreg($this->mapNameSkipArg('preg_match', 2), '^', '', [$this, 'warpEqOne']),
'mb_ereg_replace_callback' => $this->mapEreg($this->mapNameSkipArg('preg_replace_callback', 3)),
'mb_ereg_replace' => $this->mapEreg($this->mapNameSkipArg('preg_replace', 3)),
'mb_ereg' => $this->mapEreg($this->mapName('preg_match'), '', '', [$this, 'warpTernary']),
'mb_eregi_replace' => $this->mapEreg($this->mapNameSkipArg('preg_replace', 3), '', 'i'),
'mb_eregi' => $this->mapEreg($this->mapName('preg_match'), '', 'i', [$this, 'warpTernary']),
'mb_ord' => $this->mapNameSkipArg('ord', 1),
'mb_parse_str' => $this->mapName('parse_str'),
'mb_send_mail' => $this->mapName('mail'),
Expand Down Expand Up @@ -132,6 +132,50 @@ private function mapNameSkipArg(string $functionName, int $skipArgs): callable
};
}

private function mapEreg(callable $baseConverter, string $prefix = '', string $suffix = '', callable $warp = null): callable
{
return function (Node\Expr\FuncCall $node) use ($baseConverter, $prefix, $suffix, $warp): Generator {
foreach ($baseConverter($node) as $newNode) {
/* @var Node\Expr\FuncCall $newNode */
$newNode->args[0] = new Node\Arg(
new Node\Expr\BinaryOp\Concat(
new Node\Expr\BinaryOp\Concat(
new Node\Scalar\String_("/$prefix"),
new Node\Expr\FuncCall(
new Node\Name('\str_replace'),
[
new Node\Arg(new Node\Scalar\String_('/')),
new Node\Arg(new Node\Scalar\String_('\/')),
new Node\Arg($newNode->args[0]->value),
]
)
),
new Node\Scalar\String_("/$suffix")
)
);

yield $warp ? $warp($newNode) : $newNode;
}
};
}

private function warpEqOne(Node\Expr\FuncCall $node): Node
{
return new Node\Expr\BinaryOp\Identical(
$node,
new Node\Scalar\LNumber(1)
);
}

private function warpTernary(Node\Expr\FuncCall $node): Node
{
return new Node\Expr\Ternary(
$node,
new Node\Scalar\LNumber(1),
new Node\Expr\ConstFetch(new Node\Name('false'))
);
}

private function mapConvertCase(): callable
{
return function(Node\Expr\FuncCall $node): Generator {
Expand Down

0 comments on commit f6f7ff4

Please sign in to comment.