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 (#678)
by Henrique
04:27
created

KeySet::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 Respect\Validation\Exceptions\ComponentException;
15
use Respect\Validation\Exceptions\KeySetException;
16
use Respect\Validation\Validatable;
17
18
/**
19
 * Validates a keys in a defined structure.
20
 *
21
 * @author Henrique Moody <[email protected]>
22
 */
23
class KeySet
24
{
25
    /**
26
     * @param AllOf $rule
27
     *
28
     * @return Validatable
29
     */
30
    private function filterAllOf(AllOf $rule)
31
    {
32
        $rules = $rule->getRules();
0 ignored issues
show
Bug introduced by
The method getRules() does not seem to exist on object<Respect\Validation\Rules\AllOf>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
        if (count($rules) != 1) {
34
            throw new ComponentException('AllOf rule must have only one Key rule');
35
        }
36
37
        return current($rules);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function addRule($rule, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        if ($rule instanceof AllOf) {
46
            $rule = $this->filterAllOf($rule);
47
        }
48
49
        if (!$rule instanceof Key) {
50
            throw new ComponentException('KeySet rule accepts only Key rules');
51
        }
52
53
        $this->appendRule($rule);
0 ignored issues
show
Bug introduced by
The method appendRule() does not seem to exist on object<Respect\Validation\Rules\KeySet>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
55
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function addRules(array $rules)
62
    {
63
        foreach ($rules as $rule) {
64
            $this->addRule($rule);
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getKeys()
74
    {
75
        $keys = [];
76
        foreach ($this->getRules() as $keyRule) {
0 ignored issues
show
Bug introduced by
The method getRules() does not seem to exist on object<Respect\Validation\Rules\KeySet>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            $keys[] = $keyRule->reference;
78
        }
79
80
        return $keys;
81
    }
82
83
    /**
84
     * @param array $input
85
     *
86
     * @return bool
87
     */
88
    private function hasValidStructure($input)
89
    {
90
        if (!is_array($input)) {
91
            return false;
92
        }
93
94
        foreach ($this->getRules() as $keyRule) {
0 ignored issues
show
Bug introduced by
The method getRules() does not seem to exist on object<Respect\Validation\Rules\KeySet>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) {
96
                return false;
97
            }
98
99
            unset($input[$keyRule->reference]);
100
        }
101
102
        return count($input) == 0;
103
    }
104
105
    /**
106
     * @throws KeySetException
107
     */
108
    private function checkKeys($input)
109
    {
110
        if (!$this->hasValidStructure($input)) {
111
            $params = ['keys' => $this->getKeys()];
112
            $exception = $this->reportError($input, $params);
0 ignored issues
show
Bug introduced by
The method reportError() does not seem to exist on object<Respect\Validation\Rules\KeySet>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
            throw $exception;
115
        }
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function assert($input)
122
    {
123
        $this->checkKeys($input);
124
125
        return parent::assert($input);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function check($input)
132
    {
133
        $this->checkKeys($input);
134
135
        return parent::check($input);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function validate($input)
142
    {
143
        if (!$this->hasValidStructure($input)) {
144
            return false;
145
        }
146
147
        return parent::validate($input);
148
    }
149
}
150