Passed
Push — master ( ea3c0b...d6bd39 )
by Alexey
02:36
created

MutatorFactory::getMutatorNameForClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 end;
39
use function explode;
40
use function is_a;
41
use function Safe\array_flip;
42
use function Safe\sprintf;
43
use Webmozart\Assert\Assert;
44
45
/**
46
 * @internal
47
 */
48
final class MutatorFactory
49
{
50
    /**
51
     * @param array<class-string<Mutator&ConfigurableMutator>, mixed[]> $resolvedMutators
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<Mutat...rableMutator>, mixed[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<Mutator&ConfigurableMutator>, mixed[]>.
Loading history...
52
     *
53
     * @return array<string, Mutator>
54
     */
55
    public function create(array $resolvedMutators): array
56
    {
57
        $mutators = [];
58
59
        $knownMutatorClassNames = array_flip(ProfileList::ALL_MUTATORS);
60
61
        foreach ($resolvedMutators as $mutatorClassName => $config) {
62
            Assert::keyExists(
63
                $knownMutatorClassNames,
64
                $mutatorClassName,
65
                sprintf('Unknown mutator "%s"', $mutatorClassName)
66
            );
67
            Assert::isArray(
68
                $config,
69
                sprintf(
70
                    'Expected config of the mutator "%s" to be an array. Got "%%s" instead',
71
                    $mutatorClassName
72
                )
73
            );
74
75
            /** @var mixed[] $settings */
76
            $settings = (array) ($config['settings'] ?? []);
77
            /** @var string[] $ignored */
78
            $ignored = $config['ignore'] ?? [];
79
80
            $mutator =
81
                is_a($mutatorClassName, ConfigurableMutator::class, true) ?
82
                    self::getConfigurableMutator($mutatorClassName, $settings) :
83
                    new $mutatorClassName();
84
85
            if ($ignored === []) {
86
                $mutators[$mutator->getName()] = $mutator;
87
88
                continue;
89
            }
90
91
            $mutators[$mutator->getName()] = new IgnoreMutator(
92
                new IgnoreConfig($ignored),
93
                $mutator
94
            );
95
        }
96
97
        return $mutators;
98
    }
99
100
    public static function getMutatorNameForClassName(string $className): string
101
    {
102
        $parts = explode('\\', $className);
103
104
        return (string) end($parts);
105
    }
106
107
    /**
108
     * @param class-string<ConfigurableMutator> $mutatorClassName
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ConfigurableMutator> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ConfigurableMutator>.
Loading history...
109
     * @param mixed[] $settings
110
     */
111
    private static function getConfigurableMutator(string $mutatorClassName, array $settings): ConfigurableMutator
112
    {
113
        $configClassName = $mutatorClassName::getConfigClassName();
114
115
        return new $mutatorClassName(new $configClassName($settings));
116
    }
117
}
118