Util::isAssoc()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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