Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#728)
by Henrique
08:20 queued 03:32
created

KeyNested::getReferenceArrayValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Rules;
13
14
use ArrayAccess;
15
use Respect\Validation\Exceptions\ComponentException;
16
17
class KeyNested extends AbstractRelated
18
{
19 13
    public function hasReference($input)
20
    {
21
        try {
22 13
            $this->getReferenceValue($input);
23 13
        } catch (ComponentException $cex) {
24 6
            return false;
25
        }
26
27 7
        return true;
28
    }
29
30 9
    private function getReferencePieces()
31
    {
32 9
        return explode('.', rtrim($this->reference, '.'));
33
    }
34
35 7
    private function getKeyValueFromArray($array, $key)
36
    {
37 7
        if (!array_key_exists($key, $array)) {
38 1
            $message = sprintf('Cannot select the key %s from the given array', $this->reference);
39 1
            throw new ComponentException($message);
40
        }
41
42 6
        return $array[$key];
43
    }
44
45 2
    private function getKeyValueFromObject($object, $property)
46
    {
47 2
        if (empty($property) || !property_exists($object, $property)) {
48 1
            $message = sprintf('Cannot select the property %s from the given object', $this->reference);
49 1
            throw new ComponentException($message);
50
        }
51
52 1
        return $object->{$property};
53
    }
54
55 9
    private function getKeyValue($value, $key)
56
    {
57 9
        if (is_array($value) || $value instanceof ArrayAccess) {
58 7
            return $this->getKeyValueFromArray($value, $key);
59
        }
60
61 2
        if (is_object($value)) {
62 2
            return $this->getKeyValueFromObject($value, $key);
63
        }
64
65
        $message = sprintf('Cannot select the property %s from the given data', $this->reference);
66
        throw new ComponentException($message);
67
    }
68
69 13
    public function getReferenceValue($input)
70
    {
71 13
        if (is_scalar($input)) {
72 4
            $message = sprintf('Cannot select the %s in the given data', $this->reference);
73 4
            throw new ComponentException($message);
74
        }
75
76 9
        $keys = $this->getReferencePieces();
77 9
        $value = $input;
78 9
        while (!is_null($key = array_shift($keys))) {
79 9
            $value = $this->getKeyValue($value, $key);
80 7
        }
81
82 7
        return $value;
83
    }
84
}
85