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

Passed
Pull Request — master (#957)
by Henrique
02:12
created

KeyNested   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueFromObject() 0 8 3
A getValueFromArray() 0 8 2
A getReferencePieces() 0 3 1
A getReferenceValue() 0 14 3
A hasReference() 0 9 2
A getValue() 0 12 4
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
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use ArrayAccess;
17
use Respect\Validation\Exceptions\ComponentException;
18
19
class KeyNested extends AbstractRelated
20
{
21 12
    public function hasReference($input)
22
    {
23
        try {
24 12
            $this->getReferenceValue($input);
25 6
        } catch (ComponentException $cex) {
26 6
            return false;
27
        }
28
29 6
        return true;
30
    }
31
32 8
    private function getReferencePieces()
33
    {
34 8
        return explode('.', rtrim((string) $this->reference, '.'));
35
    }
36
37 6
    private function getValueFromArray($array, $key)
38
    {
39 6
        if (!array_key_exists($key, $array)) {
40 1
            $message = sprintf('Cannot select the key %s from the given array', $this->reference);
41 1
            throw new ComponentException($message);
42
        }
43
44 5
        return $array[$key];
45
    }
46
47 2
    private function getValueFromObject($object, $property)
48
    {
49 2
        if (empty($property) || !property_exists($object, $property)) {
50 1
            $message = sprintf('Cannot select the property %s from the given object', $this->reference);
51 1
            throw new ComponentException($message);
52
        }
53
54 1
        return $object->{$property};
55
    }
56
57 8
    private function getValue($value, $key)
58
    {
59 8
        if (is_array($value) || $value instanceof ArrayAccess) {
60 6
            return $this->getValueFromArray($value, $key);
61
        }
62
63 2
        if (is_object($value)) {
64 2
            return $this->getValueFromObject($value, $key);
65
        }
66
67
        $message = sprintf('Cannot select the property %s from the given data', $this->reference);
68
        throw new ComponentException($message);
69
    }
70
71 12
    public function getReferenceValue($input)
72
    {
73 12
        if (is_scalar($input)) {
74 4
            $message = sprintf('Cannot select the %s in the given data', $this->reference);
75 4
            throw new ComponentException($message);
76
        }
77
78 8
        $keys = $this->getReferencePieces();
79 8
        $value = $input;
80 8
        while (!is_null($key = array_shift($keys))) {
81 8
            $value = $this->getValue($value, $key);
82
        }
83
84 6
        return $value;
85
    }
86
}
87