AddSubkeyMutation::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
nc 5
nop 2
dl 0
loc 26
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
namespace Blackmine\Mutator\Mutation;
4
5
use Blackmine\Tool\Inflect;
6
7
class AddSubkeyMutation extends AbstractMutation
8
{
9
10
    public function __invoke(array $target, array ...$args): array
11
    {
12
        [$key, $subkey] = $args[0];
13
14
        if (isset($target[$key]) && is_array($target[$key])) {
15
            $target[$key][$subkey] = $target[$key];
16
            return $target;
17
        }
18
19
        if (is_object($target[$key])) {
20
            $getter = "get" . Inflect::camelize(ucfirst((string) $subkey));
21
            if (method_exists($target[$key], $getter)) {
22
                $target[$key][$subkey] = $target[$key]->$getter();
23
            }
24
            return $target;
25
        }
26
27
        if (isset($target[$key])) {
28
            $old_value = $target[$key];
29
            unset($target[$key]);
30
31
            $target[$key][$subkey] = $old_value;
32
            return $target;
33
        }
34
35
        return $target;
36
    }
37
}
38