From f6f7ff4a6e4659cef5130afc95a5ee95039ecdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kowalik?= Date: Wed, 13 Mar 2019 00:58:49 +0100 Subject: [PATCH] #654 drop ereg functions in favoure of preg functions --- src/Mutator/Extensions/MBString.php | 56 +++++- tests/Mutator/Extensions/MBStringTest.php | 224 +++++++++++++++------- 2 files changed, 200 insertions(+), 80 deletions(-) diff --git a/src/Mutator/Extensions/MBString.php b/src/Mutator/Extensions/MBString.php index 0fe91e649..9018e1598 100644 --- a/src/Mutator/Extensions/MBString.php +++ b/src/Mutator/Extensions/MBString.php @@ -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'), @@ -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 { diff --git a/tests/Mutator/Extensions/MBStringTest.php b/tests/Mutator/Extensions/MBStringTest.php index f8180700f..451190711 100644 --- a/tests/Mutator/Extensions/MBStringTest.php +++ b/tests/Mutator/Extensions/MBStringTest.php @@ -35,6 +35,7 @@ namespace Infection\Tests\Mutator\Extensions; +use Generator; use Infection\Tests\Mutator\AbstractMutatorTestCase; /** @@ -50,7 +51,7 @@ public function test_mutator(string $input, string $expected = null, array $sett $this->doTest($input, $expected, $settings); } - public function provideMutationCases(): \Generator + public function provideMutationCases(): Generator { yield 'It converts mb_strlen with leading slash' => [ " ['mb_strlen' => false]], ]; - // mb_chr-> chr + yield from $this->provideMutationCasesForChr(); + yield from $this->provideMutationCasesForEregMatch(); + + yield from $this->provideMutationCasesForEregReplaceCallback(); + + yield from $this->provideMutationCasesForEregReplace(); + + yield from $this->provideMutationCasesForEreg(); + + yield from $this->provideMutationCasesForEregiReplace(); + + yield from $this->provideMutationCasesForEregi(); + + yield from $this->provideMutationCasesForOrd(); + + yield from $this->provideMutationCasesForParseStr(); + + yield from $this->provideMutationCasesForSendMail(); + + yield from $this->provideMutationCasesForSplit(); + + yield from $this->provideMutationCasesForStrCut(); + + yield from $this->provideMutationCasesForStrPos(); + + yield from $this->provideMutationCasesForStrIPos(); + + yield from $this->provideMutationCasesForStrIStr(); + + yield from $this->provideMutationCasesForStrRiPos(); + + yield from $this->provideMutationCasesForStrRPos(); + + yield from $this->provideMutationCasesForStrStr(); + + yield from $this->provideMutationCasesForStrToLower(); + + yield from $this->provideMutationCasesForStrToUpper(); + + yield from $this->provideMutationCasesForSubStrCount(); + + yield from $this->provideMutationCasesForSubStr(); + + yield from $this->provideMutationCasesForStrRChr(); + + yield from $this->provideMutationCasesForStrRiChr(); + + yield from $this->provideMutationCasesForConvertCase(); + } + + private function provideMutationCasesForChr(): Generator + { yield 'It converts mb_chr to chr' => [ ' preg_match - + private function provideMutationCasesForEregMatch(): Generator + { yield 'It converts mb_ereg_match to preg_match' => [ - " [ - " preg_replace_callback - + private function provideMutationCasesForEregReplaceCallback(): Generator + { yield 'It converts mb_ereg_replace_callback to preg_replace_callback' => [ - " [ - " ereg_replace - + private function provideMutationCasesForEregReplace(): Generator + { yield 'It converts mb_ereg_replace to ereg_replace' => [ - " [ - " ereg - + private function provideMutationCasesForEreg(): Generator + { yield 'It converts mb_ereg to ereg' => [ - " [ - " eregi_replace - + private function provideMutationCasesForEregiReplace(): Generator + { yield 'It converts mb_eregi_replace to eregi_replace' => [ - " [ - " eregi - + private function provideMutationCasesForEregi(): Generator + { yield 'It converts mb_eregi to eregi' => [ - " [ - " ord - + private function provideMutationCasesForOrd(): Generator + { yield 'It converts mb_ord to ord' => [ " parse_str - + private function provideMutationCasesForParseStr(): Generator + { yield 'It converts mb_parse_str to parse_str' => [ " mail - + private function provideMutationCasesForSendMail(): Generator + { yield 'It converts mb_send_mail to mail' => [ " split - + private function provideMutationCasesForSplit(): Generator + { yield 'It converts mb_split to split' => [ " substr - + private function provideMutationCasesForStrCut(): Generator + { yield 'It converts mb_strcut to substr' => [ " strpos - + private function provideMutationCasesForStrPos(): Generator + { yield 'It converts mb_strpos to strpos' => [ " stripos - + private function provideMutationCasesForStrIPos(): Generator + { yield 'It converts mb_stripos to stripos' => [ " stristr - + private function provideMutationCasesForStrIStr(): Generator + { yield 'It converts mb_stristr to stristr' => [ " strripos - + private function provideMutationCasesForStrRiPos(): Generator + { yield 'It converts mb_strripos to strripos' => [ " strrpos - + private function provideMutationCasesForStrRPos(): Generator + { yield 'It converts mb_strrpos to strrpos' => [ " strstr - + private function provideMutationCasesForStrStr(): Generator + { yield 'It converts mb_strstr to strstr' => [ " strtolower - + private function provideMutationCasesForStrToLower(): Generator + { yield 'It converts mb_strtolower to strtolower' => [ " strtoupper - + private function provideMutationCasesForStrToUpper(): Generator + { yield 'It converts mb_strtoupper to strtoupper' => [ " substr_count - + private function provideMutationCasesForSubStrCount(): Generator + { yield 'It converts mb_substr_count to substr_count' => [ " substr - + private function provideMutationCasesForSubStr(): Generator + { yield 'It converts mb_substr to substr' => [ " strrchr - + private function provideMutationCasesForStrRChr(): Generator + { yield 'It converts mb_strrchr to strrchr' => [ " strrchr - + private function provideMutationCasesForStrRiChr(): Generator + { yield 'It converts mb_strrichr to strrchr' => [ " [ "