SetValue::assignArrayValue()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
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