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 (#1232)
by Henrique
09:38
created

KeyValue::reportError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Factory;
19
use Respect\Validation\Validatable;
20
use function array_keys;
21
use function in_array;
22
23
/**
24
 * @author Henrique Moody <[email protected]>
25
 */
26
final class KeyValue extends AbstractRule
27
{
28
    /**
29
     * @var int|string
30
     */
31
    private $comparedKey;
32
33
    /**
34
     * @var string
35
     */
36
    private $ruleName;
37
38
    /**
39
     * @var int|string
40
     */
41
    private $baseKey;
42
43
    /**
44
     * @param int|string $comparedKey
45
     * @param int|string $baseKey
46
     */
47 14
    public function __construct($comparedKey, string $ruleName, $baseKey)
48
    {
49 14
        $this->comparedKey = $comparedKey;
50 14
        $this->ruleName = $ruleName;
51 14
        $this->baseKey = $baseKey;
52 14
    }
53
54
    /**
55
     * @param mixed $input
56
     */
57 13
    private function getRule($input): Validatable
58
    {
59 13
        if (!isset($input[$this->comparedKey])) {
60 1
            throw parent::reportError($this->comparedKey);
61
        }
62
63 12
        if (!isset($input[$this->baseKey])) {
64 1
            throw parent::reportError($this->baseKey);
65
        }
66
67
        try {
68 11
            $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]);
69 9
            $rule->setName($this->comparedKey);
70 2
        } catch (ComponentException $exception) {
71 2
            throw parent::reportError($input, ['component' => true]);
72
        }
73
74 9
        return $rule;
75
    }
76
77 4
    private function overwriteExceptionParams(ValidationException $exception): ValidationException
78
    {
79 4
        $params = [];
80 4
        foreach (array_keys($exception->getParams()) as $key) {
81 4
            if (in_array($key, ['template', 'translator'])) {
82
                continue;
83
            }
84
85 4
            $params[$key] = $this->baseKey;
86
        }
87 4
        $params['name'] = $this->comparedKey;
88
89 4
        $exception->updateParams($params);
90
91 4
        return $exception;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 4
    public function assert($input): void
98
    {
99 4
        $rule = $this->getRule($input);
100
101
        try {
102 3
            $rule->assert($input[$this->comparedKey]);
103 2
        } catch (ValidationException $exception) {
104 2
            throw $this->overwriteExceptionParams($exception);
105
        }
106 1
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 3
    public function check($input): void
112
    {
113 3
        $rule = $this->getRule($input);
114
115
        try {
116 3
            $rule->check($input[$this->comparedKey]);
117 2
        } catch (ValidationException $exception) {
118 2
            throw $this->overwriteExceptionParams($exception);
119
        }
120 1
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 7
    public function validate($input): bool
126
    {
127
        try {
128 7
            $rule = $this->getRule($input);
129 3
        } catch (ValidationException $e) {
130 3
            return false;
131
        }
132
133 4
        return $rule->validate($input[$this->comparedKey]);
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    public function reportError($input, array $extraParams = []): ValidationException
140
    {
141
        try {
142
            return $this->overwriteExceptionParams($this->getRule($input)->reportError($input));
143
        } catch (ValidationException $exception) {
144
            return $this->overwriteExceptionParams($exception);
145
        }
146
    }
147
}
148