UtilityTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 28
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayFilter() 0 10 3
A filter() 0 4 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Helper;
6
7
trait UtilityTrait
8
{
9
    /**
10
     * Enhanced version of array_filter which allow to filter recursively.
11
     *
12
     * @param mixed $callback
13
     */
14 26
    public function arrayFilter(array $array, $callback = ['self', 'filter']): array
15
    {
16 26
        foreach ($array as $k => $v) {
17 26
            if (\is_array($v)) {
18 24
                $array[$k] = $this->arrayFilter($v, $callback);
19
            }
20
        }
21
22 26
        return array_filter($array, $callback);
23
    }
24
25
    /**
26
     * Callback function for filtering.
27
     *
28
     * @param mixed $var array to filter
29
     */
30 26
    protected static function filter($var): bool
31
    {
32 26
        return $var === 0 || $var === 0.0 || $var === '0' || !empty($var);
33
    }
34
}
35