ArrayHelper::removeNumericKeys()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3
1
<?php
2
namespace Subreality\Dilmun\Anshar\Utils;
3
4
class ArrayHelper
5
{
6
    /** @var  array */
7
    protected $array;
8
9
    /**
10
     * ArrayHelper constructor.  Accepts and stores an array.
11
     *
12
     * @throws \InvalidArgumentException    if not initialized with an array
13
     * @param mixed[] $array                The array needing help
14
     */
15 101
    public function __construct($array)
16
    {
17 101
        if (!is_array($array)) {
18 6
            throw new \InvalidArgumentException("Supplied array is not an array");
19
        }
20
21 95
        $this->array = $array;
22 95
    }
23
24
    /**
25
     * Returns the value corresponding with a supplied key; returns FALSE if the supplied key does not exist within
26
     * the array.
27
     *
28
     * **WARNING** This function may return FALSE or a non-Boolean value that evaluates to FALSE even if a supplied
29
     * key exists
30
     *
31
     * @param int|string $key   The key to look up
32
     * @return bool|mixed       Returns FALSE if the supplied key does not exist within the array
33
     *                          Returns the value corresponding with the key otherwise
34
     */
35 96
    public function valueLookup($key)
36
    {
37 96
        $value = false;
38
39 96
        if (array_key_exists($key, $this->array)) {
40 11
            $value = $this->array[$key];
41 11
        }
42
43 96
        return $value;
44
    }
45
46
    /**
47
     * Returns a copy of the array with numeric key/value pairs stripped.
48
     *
49
     * Preserves the original array.
50
     *
51
     * @return mixed[]  A copy of the array with numeric key/value pairs stripped
52
     */
53 1
    public function removeNumericKeys()
54
    {
55 1
        $non_numeric_keys = $this->array;
56
57 1
        foreach ($this->array as $key => $value) {
58 1
            if (is_int($key)) {
59 1
                unset($non_numeric_keys[$key]);
60 1
            }
61 1
        }
62
63 1
        return $non_numeric_keys;
64
    }
65
}
66