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

KeyValue::getRule()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
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
namespace Respect\Validation\Rules;
13
14
use Respect\Validation\Exceptions\ComponentException;
15
use Respect\Validation\Exceptions\ValidationException;
16
use Respect\Validation\Validator;
17
18
class KeyValue extends AbstractRule
19
{
20
    public $comparedKey;
21
    public $ruleName;
22
    public $baseKey;
23
24 11
    public function __construct($comparedKey, $ruleName, $baseKey)
25
    {
26 11
        $this->comparedKey = $comparedKey;
27 11
        $this->ruleName = $ruleName;
28 11
        $this->baseKey = $baseKey;
29 11
    }
30
31 10
    private function getRule($input)
32
    {
33 10
        if (!isset($input[$this->comparedKey])) {
34 1
            throw $this->reportError($this->comparedKey);
35
        }
36
37 9
        if (!isset($input[$this->baseKey])) {
38 1
            throw $this->reportError($this->baseKey);
39
        }
40
41
        try {
42 8
            $rule = Validator::__callStatic($this->ruleName, [$input[$this->baseKey]]);
43 6
            $rule->setName($this->comparedKey);
44 2
        } catch (ComponentException $exception) {
45 2
            throw $this->reportError($input, ['component' => true]);
46
        }
47
48 6
        return $rule;
49
    }
50
51
    private function overwriteExceptionParams(ValidationException $exception)
52
    {
53
        $params = [];
54
        foreach ($exception->getParams() as $key => $value) {
55
            if (in_array($key, ['template', 'translator'])) {
56
                continue;
57
            }
58
59
            $params[$key] = $this->baseKey;
60
        }
61
62
        $exception->configure($this->comparedKey, $params);
63
64
        return $exception;
65
    }
66
67 3
    public function assert($input)
68
    {
69 3
        $rule = $this->getRule($input);
70
71
        try {
72 2
            $rule->assert($input[$this->comparedKey]);
73 1
        } catch (ValidationException $exception) {
74
            throw $this->overwriteExceptionParams($exception);
75
        }
76
77 1
        return true;
78
    }
79
80 2
    public function check($input)
81
    {
82 2
        $rule = $this->getRule($input);
83
84
        try {
85 2
            $rule->check($input[$this->comparedKey]);
86 1
        } catch (ValidationException $exception) {
87
            throw $this->overwriteExceptionParams($exception);
88
        }
89
90 1
        return true;
91
    }
92
93 5
    public function validate($input)
94
    {
95
        try {
96 5
            $rule = $this->getRule($input);
97 3
        } catch (ValidationException $e) {
98 3
            return false;
99
        }
100
101 2
        return $rule->validate($input[$this->comparedKey]);
102
    }
103
}
104