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
Pull Request — master (#923)
by lee
03:57
created

KeySet::assert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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