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
Push — master ( d37876...d71621 )
by Henrique
08:43
created

KeyNested::getValueFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
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 getValueFromArray($array, $key)
36
    {
37 7
        if (!array_key_exists($key, $array)) {
38 7
            $message = sprintf('Cannot select the key %s from the given array', $this->reference);
39
            throw new ComponentException($message);
40 7
        }
41 7
42 1
        return $array[$key];
43 1
    }
44
45
    private function getValueFromObject($object, $property)
46 6
    {
47 6
        if (empty($property) || !property_exists($object, $property)) {
48
            $message = sprintf('Cannot select the property %s from the given object', $this->reference);
49 6
            throw new ComponentException($message);
50
        }
51
52 2
        return $object->{$property};
53
    }
54 2
55 2
    private function getValue($value, $key)
56
    {
57 2
        if (is_array($value) || $value instanceof ArrayAccess) {
58
            return $this->getValueFromArray($value, $key);
59 2
        }
60 2
61 1
        if (is_object($value)) {
62 1
            return $this->getValueFromObject($value, $key);
63
        }
64
65 1
        $message = sprintf('Cannot select the property %s from the given data', $this->reference);
66 1
        throw new ComponentException($message);
67
    }
68 1
69
    public function getReferenceValue($input)
70
    {
71 13
        if (is_scalar($input)) {
72
            $message = sprintf('Cannot select the %s in the given data', $this->reference);
73 13
            throw new ComponentException($message);
74 7
        }
75
76
        $keys = $this->getReferencePieces();
77 6
        $value = $input;
78 2
        while (!is_null($key = array_shift($keys))) {
79
            $value = $this->getValue($value, $key);
80
        }
81 4
82 4
        return $value;
83
    }
84
}
85