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
Push — master ( 60e3fc...b69607 )
by Henrique
02:17
created

KeySet::getKeyRule()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 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 Respect\Validation\Exceptions\ComponentException;
17
use Respect\Validation\Validatable;
18
use function array_key_exists;
19
use function array_map;
20
use function count;
21
use function current;
22
use function is_array;
23
24
/**
25
 * Validates a keys in a defined structure.
26
 *
27
 * @author Henrique Moody <[email protected]>
28
 * @author Emmerson Siqueira <[email protected]>
29
 */
30
final class KeySet extends AbstractWrapper
31
{
32
    /**
33
     * @var array
34
     */
35
    private $keys;
36
37
    /**
38
     * @var Key[]
39
     */
40
    private $keyRules;
41
42
    /**
43
     * Initializes the rule.
44
     *
45
     * @param Validatable[] ...$validatables
46
     */
47 16
    public function __construct(Validatable ...$validatables)
48
    {
49 16
        $this->keyRules = array_map([$this, 'getKeyRule'], $validatables);
50 13
        $this->keys = array_map([$this, 'getKeyReference'], $this->keyRules);
51
52 13
        parent::__construct(new AllOf(...$this->keyRules));
53 13
    }
54
55
    /**
56
     * @param Validatable $validatable
57
     *
58
     * @throws ComponentException
59
     *
60
     * @return Key
61
     */
62 16
    private function getKeyRule(Validatable $validatable): Key
63
    {
64 16
        if ($validatable instanceof Key) {
65 13
            return $validatable;
66
        }
67
68 4
        if (!$validatable instanceof AllOf
69 4
            || 1 !== count($validatable->getRules())) {
70 3
            throw new ComponentException('KeySet rule accepts only Key rules');
71
        }
72
73 2
        return $this->getKeyRule(current($validatable->getRules()));
74
    }
75
76
    /**
77
     * @param Key $rule
78
     *
79
     * @return mixed
80
     */
81 13
    private function getKeyReference(Key $rule)
82
    {
83 13
        return $rule->reference;
84
    }
85
86
    /**
87
     * @param array $input
88
     *
89
     * @return bool
90
     */
91 10
    private function hasValidStructure($input)
92
    {
93 10
        if (!is_array($input)) {
0 ignored issues
show
introduced by
The condition ! is_array($input) can never be true.
Loading history...
94 4
            return false;
95
        }
96
97 6
        foreach ($this->keyRules as $keyRule) {
98 6
            if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) {
99 4
                return false;
100
            }
101
102 3
            unset($input[$keyRule->reference]);
103
        }
104
105 2
        return 0 == count($input);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 5
    public function assert($input): void
112
    {
113 5
        if (!$this->hasValidStructure($input)) {
114 5
            throw $this->reportError($input);
115
        }
116
117
        parent::assert($input);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function check($input): void
124
    {
125 1
        if (!$this->hasValidStructure($input)) {
126 1
            throw $this->reportError($input);
127
        }
128
129
        parent::check($input);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 4
    public function validate($input): bool
136
    {
137 4
        if (!$this->hasValidStructure($input)) {
138 3
            return false;
139
        }
140
141 1
        return parent::validate($input);
142
    }
143
}
144