From 66883f7c505bea731550bcd69bf4c95c8649414b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kowalik?= Date: Sun, 10 Mar 2019 13:44:08 +0100 Subject: [PATCH] #654 mbstring mutator configuration --- src/Mutator/Extensions/MBString.php | 15 ++++++++++++--- tests/Mutator/Extensions/MBStringTest.php | 11 +++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Mutator/Extensions/MBString.php b/src/Mutator/Extensions/MBString.php index b4b0742ac..3dd3e686a 100644 --- a/src/Mutator/Extensions/MBString.php +++ b/src/Mutator/Extensions/MBString.php @@ -61,7 +61,10 @@ final class MBString extends Mutator public function __construct(MutatorConfig $config) { parent::__construct($config); - $this->setupConverters(); + + $settings = $this->getSettings(); + + $this->setupConverters($settings); } /** @@ -77,9 +80,9 @@ protected function mutatesNode(Node $node): bool return isset($this->converters[$this->getFunctionName($node)]); } - private function setupConverters(): void + private function setupConverters(array $functionsMap): void { - $this->converters = [ + $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), @@ -107,6 +110,12 @@ private function setupConverters(): void 'mb_substr' => $this->mapNameSkipArg('substr', 3), 'mb_convert_case' => $this->mapConvertCase(), ]; + + $functionsToRemove = \array_filter($functionsMap, function ($isOn) { + return !$isOn; + }); + + $this->converters = \array_diff_key($converters, $functionsToRemove); } private function mapName(string $functionName): callable diff --git a/tests/Mutator/Extensions/MBStringTest.php b/tests/Mutator/Extensions/MBStringTest.php index 2ba851173..f8180700f 100644 --- a/tests/Mutator/Extensions/MBStringTest.php +++ b/tests/Mutator/Extensions/MBStringTest.php @@ -45,9 +45,9 @@ final class MBStringTest extends AbstractMutatorTestCase /** * @dataProvider provideMutationCases */ - public function test_mutator(string $input, string $expected = null): void + public function test_mutator(string $input, string $expected = null, array $settings = []): void { - $this->doTest($input, $expected); + $this->doTest($input, $expected, $settings); } public function provideMutationCases(): \Generator @@ -69,6 +69,13 @@ public function provideMutationCases(): \Generator yield 'It converts mb_strlen with encoding to strlen' => [ " ['mb_strlen' => true]], + ]; + + yield 'It does not convert mb_strlen when disabled' => [ + " ['mb_strlen' => false]], ]; // mb_chr-> chr