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 (#957)
by Henrique
03:26
created

KeyValue::getRule()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 10
cp 0.6
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 5
nop 1
crap 5.024
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 5
    public function __construct($comparedKey, $ruleName, $baseKey)
27
    {
28 5
        $this->comparedKey = $comparedKey;
29 5
        $this->ruleName = $ruleName;
30 5
        $this->baseKey = $baseKey;
31 5
    }
32
33 5
    private function getRule($input)
34
    {
35 5
        if (!isset($input[$this->comparedKey])) {
36
            throw $this->reportError($this->comparedKey);
37
        }
38
39 5
        if (!isset($input[$this->baseKey])) {
40
            throw $this->reportError($this->baseKey);
41
        }
42
43
        try {
44 5
            $rule = Validator::__callStatic($this->ruleName, [$input[$this->baseKey]]);
45 5
            $rule->setName($this->comparedKey);
46
        } catch (ComponentException $exception) {
47
            throw $this->reportError($input, ['component' => true]);
48
        }
49
50 5
        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 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 2
            $rule->assert($input[$this->comparedKey]);
76 1
        } catch (ValidationException $exception) {
77 1
            throw $this->overwriteExceptionParams($exception);
78
        }
79 1
    }
80
81 2
    public function check($input): void
82
    {
83 2
        $rule = $this->getRule($input);
84
85
        try {
86 2
            $rule->check($input[$this->comparedKey]);
87 1
        } catch (ValidationException $exception) {
88 1
            throw $this->overwriteExceptionParams($exception);
89
        }
90 1
    }
91
92 2
    public function validate($input): bool
93
    {
94
        try {
95 2
            $rule = $this->getRule($input);
96
        } catch (ValidationException $e) {
97
            return false;
98
        }
99
100 2
        return $rule->validate($input[$this->comparedKey]);
101
    }
102
}
103