Util   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 45
ccs 10
cts 10
cp 1
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssoc() 0 3 2
A mergeAssocArray() 0 15 5
1
<?php
2
3
/*
4
 * This file is a part of dflydev/dot-access-data.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Dflydev\DotAccessData;
13
14
class Util
15
{
16
    /**
17
     * Test if array is an associative array
18
     *
19
     * Note that this function will return true if an array is empty. Meaning
20
     * empty arrays will be treated as if they are associative arrays.
21
     *
22
     * @param array<mixed> $arr
23
     *
24
     * @return bool
25
     *
26
     * @psalm-pure
27
     */
28 9
    public static function isAssoc(array $arr): bool
29
    {
30 9
        return !count($arr) || count(array_filter(array_keys($arr), 'is_string')) == count($arr);
31
    }
32
33
    /**
34
     * Merge contents from one associtative array to another
35
     *
36
     * @param mixed $to
37
     * @param mixed $from
38
     * @param bool  $clobber
39
     *
40
     * @return mixed
41
     *
42
     * @psalm-pure
43
     */
44 27
    public static function mergeAssocArray($to, $from, $clobber = true)
45
    {
46 27
        if (is_array($from)) {
47 27
            foreach ($from as $k => $v) {
48 27
                if (!isset($to[$k])) {
49 9
                    $to[$k] = $v;
50
                } else {
51 23
                    $to[$k] = self::mergeAssocArray($to[$k], $v, $clobber);
52
                }
53
            }
54
55 27
            return $to;
56
        }
57
58 18
        return $clobber ? $from : $to;
59
    }
60
}
61