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 ( 7d4223 )
by Henrique
05:27
created

KeyNested::hasReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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
18
{
19
    public function hasReference($input)
20
    {
21
        try {
22
            $this->getReferenceValue($input);
23
        } catch (ComponentException $cex) {
24
            return false;
25
        }
26
27
        return true;
28
    }
29
30
    private function getReferencePieces()
31
    {
32
        return explode('.', rtrim($this->reference, '.'));
0 ignored issues
show
Bug introduced by
The property reference does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
    }
34
35
    private function getValueFromArray($array, $key)
36
    {
37
        if (!array_key_exists($key, $array)) {
38
            $message = sprintf('Cannot select the key %s from the given array', $this->reference);
39
            throw new ComponentException($message);
40
        }
41
42
        return $array[$key];
43
    }
44
45
    private function getValueFromObject($object, $property)
46
    {
47
        if (empty($property) || !property_exists($object, $property)) {
48
            $message = sprintf('Cannot select the property %s from the given object', $this->reference);
49
            throw new ComponentException($message);
50
        }
51
52
        return $object->{$property};
53
    }
54
55
    private function getValue($value, $key)
56
    {
57
        if (is_array($value) || $value instanceof ArrayAccess) {
58
            return $this->getValueFromArray($value, $key);
59
        }
60
61
        if (is_object($value)) {
62
            return $this->getValueFromObject($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
    public function getReferenceValue($input)
70
    {
71
        if (is_scalar($input)) {
72
            $message = sprintf('Cannot select the %s in the given data', $this->reference);
73
            throw new ComponentException($message);
74
        }
75
76
        $keys = $this->getReferencePieces();
77
        $value = $input;
78
        while (!is_null($key = array_shift($keys))) {
79
            $value = $this->getValue($value, $key);
80
        }
81
82
        return $value;
83
    }
84
}
85