KeyExists::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper\Operation\ArrayOperation;
14
15
class KeyExists extends AbstractOperation implements KeyExistsInterface
16
{
17 11
    public function execute($key, array $array): bool
18
    {
19 11
        $keys = $this->splitDotNotatedKeyToArray($key);
20
21 11
        while (\count($keys) > 1) {
22 6
            $key = \array_shift($keys);
23
24 6
            if ($this->keyIsSetAndIsArray($array, $key)) {
25 5
                $array = &$array[$key];
26
            }
27
        }
28
29 11
        $key = \array_shift($keys);
30
31 11
        return $this->keyIsSetAndExists($array, $key);
32
    }
33
34 6
    private function keyIsSetAndIsArray(array $array, $key): bool
35
    {
36 6
        return isset($array[$key]) && \is_array($array[$key]);
37
    }
38
39 11
    private function keyIsSetAndExists(array $array, $key): bool
40
    {
41 11
        return isset($array[$key]) || \array_key_exists($key, $array);
42
    }
43
}
44