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 (#910)
by Henrique
02:24
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 2
                continue;
59
            }
60
61 2
            $params[$key] = $this->baseKey;
62
        }
63
64 2
        $exception->configure($this->comparedKey, $params);
65
66 2
        return $exception;
67
    }
68
69 2
    public function assert($input): void
70
    {
71 2
        $rule = $this->getRule($input);
72
73
        try {
74 1
            $rule->assert($input[$this->comparedKey]);
75 1
        } catch (ValidationException $exception) {
76 1
            throw $this->overwriteExceptionParams($exception);
77
        }
78
    }
79
80 1
    public function check($input): void
81
    {
82 1
        $rule = $this->getRule($input);
83
84
        try {
85 1
            $rule->check($input[$this->comparedKey]);
86 1
        } catch (ValidationException $exception) {
87 1
            throw $this->overwriteExceptionParams($exception);
88
        }
89
    }
90
91 5
    public function validate($input): bool
92
    {
93
        try {
94 5
            $rule = $this->getRule($input);
95 3
        } catch (ValidationException $e) {
96 3
            return false;
97
        }
98
99 2
        return $rule->validate($input[$this->comparedKey]);
100
    }
101
}
102