KeyExists   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 3
A keyIsSetAndIsArray() 0 3 2
A keyIsSetAndExists() 0 3 2
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