Skip to content

Commit

Permalink
infection#654 mbstring mutator configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
majkel89 committed Mar 10, 2019
1 parent 2bcbf0e commit 66883f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/Mutator/Extensions/MBString.php
Expand Up @@ -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);
}

/**
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions tests/Mutator/Extensions/MBStringTest.php
Expand Up @@ -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
Expand All @@ -69,6 +69,13 @@ public function provideMutationCases(): \Generator
yield 'It converts mb_strlen with encoding to strlen' => [
"<?php mb_strlen('test', 'utf-8');",
"<?php\n\nstrlen('test');",
['settings' => ['mb_strlen' => true]],
];

yield 'It does not convert mb_strlen when disabled' => [
"<?php mb_strlen('test');",
null,
['settings' => ['mb_strlen' => false]],
];

// mb_chr-> chr
Expand Down

0 comments on commit 66883f7

Please sign in to comment.