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

Make unique only global settings (arrays) #1495

Merged
merged 1 commit into from
Mar 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Mutator/MutatorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
namespace Infection\Mutator;

use function array_key_exists;
use function array_map;
use function array_merge_recursive;
use function array_unique;
use function array_values;
use function class_exists;
use function in_array;
use InvalidArgumentException;
use function Safe\sprintf;
use stdClass;
Expand All @@ -50,6 +50,9 @@
*/
final class MutatorResolver
{
private const IGNORE_SETTING = 'ignore';
private const IGNORE_SOURCE_CODE_BY_REGEX_SETTING = 'ignoreSourceCodeByRegex';

private const GLOBAL_IGNORE_SETTING = 'global-ignore';
private const GLOBAL_IGNORE_SOURCE_CODE_BY_REGEX_SETTING = 'global-ignoreSourceCodeByRegex';

Expand All @@ -73,15 +76,15 @@ public function resolve(array $mutatorSettings): array
/** @var string[] $globalSetting */
$globalSetting = $setting;

$globalSettings['ignore'] = $globalSetting;
$globalSettings[self::IGNORE_SETTING] = $globalSetting;
unset($mutatorSettings[self::GLOBAL_IGNORE_SETTING]);
}

if ($mutatorOrProfileOrGlobalSettingKey === self::GLOBAL_IGNORE_SOURCE_CODE_BY_REGEX_SETTING) {
/** @var string[] $globalSetting */
$globalSetting = $setting;

$globalSettings['ignoreSourceCodeByRegex'] = array_values(array_unique($globalSetting));
$globalSettings[self::IGNORE_SOURCE_CODE_BY_REGEX_SETTING] = array_values(array_unique($globalSetting));
unset($mutatorSettings[self::GLOBAL_IGNORE_SOURCE_CODE_BY_REGEX_SETTING]);
}
}
Expand Down Expand Up @@ -138,10 +141,16 @@ private static function resolveSettings($settings, array $globalSettings)
return (array) $settings;
}

return array_map(
static fn (array $values): array => array_values(array_unique($values)),
array_merge_recursive($globalSettings, (array) $settings)
);
$resultSettings = array_merge_recursive($globalSettings, (array) $settings);

foreach ($resultSettings as $key => &$settingValues) {
if (in_array($key, [self::IGNORE_SETTING, self::IGNORE_SOURCE_CODE_BY_REGEX_SETTING], true)) {
$settingValues = array_values(array_unique($settingValues));
}
}
unset($settingValues);

return $resultSettings;
}

/**
Expand Down
27 changes: 26 additions & 1 deletion tests/phpunit/Mutator/MutatorResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Infection\Mutator\Boolean\IdenticalEqual;
use Infection\Mutator\Boolean\NotIdenticalNotEqual;
use Infection\Mutator\Boolean\TrueValue;
use Infection\Mutator\Extensions\MBString;
use Infection\Mutator\Loop\For_;
use Infection\Mutator\MutatorResolver;
use Infection\Mutator\Number\DecrementInteger;
Expand Down Expand Up @@ -334,6 +335,9 @@ public function test_it_can_resolve_mutators_with_duplicate_global_settings(): v
'global-ignoreSourceCodeByRegex' => ['A::B', 'A::B', 'C::D'],
MutatorName::getName(Plus::class) => true,
MutatorName::getName(For_::class) => false,
MutatorName::getName(MBString::class) => [
'settings' => ['mb_substr' => false],
],
MutatorName::getName(IdenticalEqual::class) => [
'ignoreSourceCodeByRegex' => [],
],
Expand All @@ -342,6 +346,7 @@ public function test_it_can_resolve_mutators_with_duplicate_global_settings(): v
$this->assertSameMutatorsByClass(
[
Plus::class,
MBString::class,
IdenticalEqual::class,
],
$resolvedMutators
Expand All @@ -354,6 +359,14 @@ public function test_it_can_resolve_mutators_with_duplicate_global_settings(): v
$resolvedMutators[Plus::class]
);

$this->assertSame(
[
'ignoreSourceCodeByRegex' => ['A::B', 'C::D'],
'settings' => ['mb_substr' => false],
],
$resolvedMutators[MBString::class]
);

$this->assertSame(
[
'ignoreSourceCodeByRegex' => ['A::B', 'C::D'],
Expand All @@ -362,13 +375,16 @@ public function test_it_can_resolve_mutators_with_duplicate_global_settings(): v
);
}

public function test_it_can_resolve_mutators_with_duplicate_global_and_and_per_mutator_settings(): void
public function test_it_can_resolve_mutators_with_duplicate_global_and_per_mutator_settings(): void
{
$resolvedMutators = $this->mutatorResolver->resolve([
'global-ignore' => ['A::B'],
'global-ignoreSourceCodeByRegex' => ['A::B', 'A::B', 'C::D'],
MutatorName::getName(Plus::class) => true,
MutatorName::getName(For_::class) => false,
MutatorName::getName(MBString::class) => [
'settings' => ['mb_substr' => false],
],
MutatorName::getName(IdenticalEqual::class) => [
'ignore' => ['B::C'],
'ignoreSourceCodeByRegex' => ['A::B', 'B::C'],
Expand All @@ -378,6 +394,7 @@ public function test_it_can_resolve_mutators_with_duplicate_global_and_and_per_m
$this->assertSameMutatorsByClass(
[
Plus::class,
MBString::class,
IdenticalEqual::class,
],
$resolvedMutators
Expand All @@ -390,6 +407,14 @@ public function test_it_can_resolve_mutators_with_duplicate_global_and_and_per_m
],
$resolvedMutators[Plus::class]
);
$this->assertSame(
[
'ignore' => ['A::B'],
'ignoreSourceCodeByRegex' => ['A::B', 'C::D'],
'settings' => ['mb_substr' => false],
],
$resolvedMutators[MBString::class]
);
$this->assertSame(
[
'ignore' => ['A::B', 'B::C'],
Expand Down