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 — 2.0 ( e6a123 )
by Henrique
05:00
created

KeyNested   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 94
ccs 30
cts 35
cp 0.8571
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasReference() 0 10 2
A getReferenceValue() 0 15 3
A getValue() 0 13 4
A getValueFromArray() 0 10 4
A getValueFromObject() 0 9 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
use Respect\Validation\Rule;
17
18
/**
19
 * Validates an array key or an object property using `.` to represent nested data.
20
 *
21
 * @author Henrique Moody <[email protected]>
22
 * @author Ivan Zinovyev <[email protected]>
23
 *
24
 * @since 1.0.0
25
 */
26
final class KeyNested extends AbstractRelated
27
{
28
    /**
29
     * Initializes the rule.
30
     *
31
     * @param string $reference
32
     * @param Rule   $rule
33
     * @param bool   $mandatory
34
     */
35
    public function __construct(string $reference, Rule $rule = null, bool $mandatory = true)
36
    {
37
        parent::__construct($reference, $rule, $mandatory);
38
    }
39
40
    /**
41
     * Verifies if the input has the reference.
42
     *
43
     * @param mixed  $input
44
     * @param string $reference
45
     *
46
     * @return bool
47
     */
48 33
    protected function hasReference($input, $reference): bool
49
    {
50
        try {
51 33
            $this->getReferenceValue($input, $reference);
52 12
        } catch (ComponentException $exceptions) {
53 12
            return false;
54
        }
55
56 21
        return true;
57
    }
58
59
    /**
60
     * Get the value for the reference on the input.
61
     *
62
     * @param mixed  $input
63
     * @param string $reference
64
     *
65
     * @throws ComponentException When the value cannot be fetch
66
     *
67
     * @return mixed
68
     */
69 33
    protected function getReferenceValue($input, $reference)
70
    {
71 33
        if (is_scalar($input)) {
72 6
            $message = sprintf('Cannot select the %s in the given data', $reference);
73 6
            throw new ComponentException($message);
74
        }
75
76 27
        $keys = explode('.', rtrim($reference, '.'));
77 27
        $value = $input;
78 27
        while (!is_null($key = array_shift($keys))) {
79 27
            $value = $this->getValue($reference, $value, $key);
80
        }
81
82 21
        return $value;
83
    }
84
85 27
    private function getValue(string $reference, $value, string $key)
86
    {
87 27
        if (is_array($value) || $value instanceof ArrayAccess) {
88 27
            return $this->getValueFromArray($reference, $value, $key);
89
        }
90
91 9
        if (is_object($value)) {
92 6
            return $this->getValueFromObject($reference, $value, $key);
93
        }
94
95 3
        $message = sprintf('Cannot select the reference %s from the given data', $reference);
96 3
        throw new ComponentException($message);
97
    }
98
99 27
    private function getValueFromArray(string $reference, $array, string $key)
100
    {
101 27
        if ((is_array($array) && !array_key_exists($key, $array))
102 27
            || !isset($array[$key])) {
103 3
            $message = sprintf('Cannot select the key %s from the given array', $reference);
104 3
            throw new ComponentException($message);
105
        }
106
107 27
        return $array[$key];
108
    }
109
110 6
    private function getValueFromObject(string $reference, $object, string $property)
111
    {
112 6
        if (empty($property) || !property_exists($object, $property)) {
113
            $message = sprintf('Cannot select the property %s from the given object', $reference);
114
            throw new ComponentException($message);
115
        }
116
117 6
        return $object->{$property};
118
    }
119
}
120