Completed
Pull Request — master (#9)
by Jodie
03:44
created

ArrayHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mutate() 0 17 4
1
<?php
2
3
namespace Rexlabs\Smokescreen\Helpers;
4
5
class ArrayHelper
6
{
7
    /**
8
     * Mutate an array using dot-notation.
9
     *
10
     * @param array  $array
11
     * @param string $writeKey
12
     * @param mixed  $value
13
     *
14
     * @return void
15
     */
16 6
    public static function mutate(array &$array, string $writeKey, $value)
17
    {
18 6
        $ref = &$array;
19 6
        $keys = explode('.', $writeKey);
20 6
        while (($key = array_shift($keys)) !== null) {
21 6
            $hasMoreKeys = \count($keys) > 0;
22 6
            if ($hasMoreKeys) {
23 1
                if (!\array_key_exists($key, $ref)) {
24
                    // Prepare the array
25 1
                    $ref[$key] = [];
26
                }
27 1
                $ref = &$ref[$key];
28 1
                continue;
29
            }
30
31
            // Last key
32 6
            $ref[$key] = $value;
33
        }
34 6
    }
35
}
36