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
Pull Request — master (#957)
by Henrique
02:12
created

KeyValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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