AddSubkeyMutation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 29
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 26 6
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