1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This code is licensed under the BSD 3-Clause License. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017, Maks Rafalko |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Redistribution and use in source and binary forms, with or without |
9
|
|
|
* modification, are permitted provided that the following conditions are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright notice, this |
12
|
|
|
* list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, |
15
|
|
|
* this list of conditions and the following disclaimer in the documentation |
16
|
|
|
* and/or other materials provided with the distribution. |
17
|
|
|
* |
18
|
|
|
* * Neither the name of the copyright holder nor the names of its |
19
|
|
|
* contributors may be used to endorse or promote products derived from |
20
|
|
|
* this software without specific prior written permission. |
21
|
|
|
* |
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
23
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
25
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
26
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
28
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
29
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
30
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
declare(strict_types=1); |
35
|
|
|
|
36
|
|
|
namespace Infection\Mutator; |
37
|
|
|
|
38
|
|
|
use function is_a; |
39
|
|
|
use function Safe\array_flip; |
40
|
|
|
use function Safe\sprintf; |
41
|
|
|
use Webmozart\Assert\Assert; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @internal |
45
|
|
|
*/ |
46
|
|
|
final class MutatorFactory |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @param array<class-string<Mutator&ConfigurableMutator>, mixed[]> $resolvedMutators |
|
|
|
|
50
|
|
|
* |
51
|
|
|
* @return array<string, Mutator> |
52
|
|
|
*/ |
53
|
|
|
public function create(array $resolvedMutators): array |
54
|
|
|
{ |
55
|
|
|
$mutators = []; |
56
|
|
|
|
57
|
|
|
$knownMutatorClassNames = array_flip(ProfileList::ALL_MUTATORS); |
58
|
|
|
|
59
|
|
|
foreach ($resolvedMutators as $mutatorClassName => $config) { |
60
|
|
|
Assert::keyExists( |
61
|
|
|
$knownMutatorClassNames, |
62
|
|
|
$mutatorClassName, |
63
|
|
|
sprintf('Unknown mutator "%s"', $mutatorClassName) |
64
|
|
|
); |
65
|
|
|
Assert::isArray( |
66
|
|
|
$config, |
67
|
|
|
sprintf( |
68
|
|
|
'Expected config of the mutator "%s" to be an array. Got "%%s" instead', |
69
|
|
|
$mutatorClassName |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** @var mixed[] $settings */ |
74
|
|
|
$settings = (array) ($config['settings'] ?? []); |
75
|
|
|
/** @var string[] $ignored */ |
76
|
|
|
$ignored = $config['ignore'] ?? []; |
77
|
|
|
|
78
|
|
|
$mutator = |
79
|
|
|
is_a($mutatorClassName, ConfigurableMutator::class, true) ? |
80
|
|
|
self::getConfigurableMutator($mutatorClassName, $settings) : |
81
|
|
|
new $mutatorClassName(); |
82
|
|
|
|
83
|
|
|
if ($ignored === []) { |
84
|
|
|
$mutators[$mutator->getName()] = $mutator; |
85
|
|
|
|
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$mutators[$mutator->getName()] = new IgnoreMutator( |
90
|
|
|
new IgnoreConfig($ignored), |
91
|
|
|
$mutator |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $mutators; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param class-string<ConfigurableMutator> $mutatorClassName |
|
|
|
|
100
|
|
|
* @param mixed[] $settings |
101
|
|
|
*/ |
102
|
|
|
private static function getConfigurableMutator(string $mutatorClassName, array $settings): ConfigurableMutator |
103
|
|
|
{ |
104
|
|
|
$configClassName = $mutatorClassName::getConfigClassName(); |
105
|
|
|
|
106
|
|
|
return new $mutatorClassName(new $configClassName($settings)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|