Arr::attach()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 1
cts 1
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Braunstetter\Helper;
4
5
class Arr
6
{
7 1
    /**
8
     * This method is just handy when it comes to add a class to an existing class string.
9 1
     */
10
    public static function attachClass(array $array, string $class): array
11
    {
12 2
        return static::attach($array, ['class' => $class]);
13
    }
14 2
15 2
16 2
    /**
17
     * Sometimes just want to add some new attribute to an existing string.
18
     */
19
    public static function attach(array $array, array $data): array
20 2
    {
21
        foreach ($data as $key => $value) {
22
            $array = array_replace(
23
                $array, [$key => array_key_exists($key, $array) ? $array[$key] . ' ' . $value : $value]
24
            );
25
        }
26
27
        return $array;
28
    }
29
30 1
    /**
31
     * Returns the first value of a given array.
32 1
     * If the array is empty it returns null.
33
     */
34
    public static function firstValue(array $array): mixed
35
    {
36
        return array_values($array)[0] ?? null;
37
    }
38
}