SetValue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 19 3
A assignArrayValue() 0 8 3
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\ArrayOperation;
14
15
class SetValue extends AbstractOperation implements SetValueInterface
16
{
17 7
    public function execute(array &$array, $path, $value): void
18
    {
19 7
        if ($path === null) {
20 2
            $array = $value;
21
22 2
            return;
23
        }
24
25 5
        $keys = $this->splitDotNotatedKeyToArray($path);
26
27 5
        while (\count($keys) > 1) {
28 3
            $key = \array_shift($keys);
29
30 3
            $this->assignArrayValue($array, $key);
31
32 3
            $array = &$array[$key];
33
        }
34
35 5
        $array[\array_shift($keys)] = $value;
36 5
    }
37
38 3
    private function assignArrayValue(array &$array, $key): void
39
    {
40 3
        if (!isset($array[$key])) {
41 2
            $array[$key] = [];
42
        }
43
44 3
        if (!\is_array($array[$key])) {
45 1
            $array[$key] = [$array[$key]];
46
        }
47 3
    }
48
}
49