Passed
Push — master ( c8ed24...0a0550 )
by Ehsan
02:51
created

ArrayUtility::arrayKeyValueExists()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 10
cp 0.9
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 2
crap 6.0359
1
<?php
2
3
namespace Botonomous\utility;
4
5
/**
6
 * Class ArrayUtility.
7
 */
8
class ArrayUtility extends AbstractUtility
9
{
10
    /**
11
     * @param array $toFilter
12
     * @param array $keepKeys Includes the keys that need to be kept in $toFilter array
13
     *
14
     * @return array
15
     */
16 25
    public function filterArray(array $toFilter, array $keepKeys)
17
    {
18 25
        return array_intersect_key($toFilter, array_flip($keepKeys));
19
    }
20
21
    /**
22
     * Check if key is present in $search array and has got a value
23
     * It considers values such as 0, '0', true and false AS true
24
     *
25
     * @param       $key
26
     * @param array $search
27
     *
28
     * @return bool
29
     */
30 27
    public function arrayKeyValueExists($key, array $search)
31
    {
32 27
        if (!array_key_exists($key, $search)) {
33 4
            return false;
34
        }
35
36 26
        if (is_string($search[$key])) {
37 26
            if (strlen(trim($search[$key])) > 0) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return strlen(trim($search[$key])) > 0;.
Loading history...
38 26
                return true;
39
            } else {
40 1
                return false;
41
            }
42
        }
43
44 1
        if (empty($search[$key]) && !in_array($search[$key], [0, true, false])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(empty($search[$...rray(0, true, false)));.
Loading history...
45
            return false;
46
        }
47
48 1
        return true;
49
    }
50
}
51