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
02:45
created

ValidationException::updateMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Exceptions;
15
16
use InvalidArgumentException;
17
use function array_key_exists;
18
use function call_user_func;
19
use function Respect\Stringifier\stringify;
20
21
/**
22
 * Default exception class for rule validations.
23
 *
24
 * @author Alexandre Gomes Gaigalas <[email protected]>
25
 * @author Henrique Moody <[email protected]>
26
 */
27
class ValidationException extends InvalidArgumentException implements ExceptionInterface
28
{
29
    public const MODE_DEFAULT = 'default';
30
    public const MODE_NEGATIVE = 'negative';
31
    public const STANDARD = 'standard';
32
33
    /**
34
     * Contains the default templates for exception message.
35
     *
36
     * @var array
37
     */
38
    public static $defaultTemplates = [
39
        self::MODE_DEFAULT => [
40
            self::STANDARD => 'Data validation failed for {{name}}',
41
        ],
42 199
        self::MODE_NEGATIVE => [
43
            self::STANDARD => 'Data validation failed for {{name}}',
44 199
        ],
45 199
    ];
46 199
47 197
    /**
48 2
     * @var mixed
49
     */
50
    private $input;
51 197
52 197
    /**
53 194
     * @var string
54
     */
55
    private $id;
56 42
57 199
    /**
58 199
     * @var string
59
     */
60
    private $mode = self::MODE_DEFAULT;
61
62
    /**
63
     * @var array
64
     */
65
    private $params = [];
66
67 42
    /**
68
     * @var callable
69 42
     */
70
    private $translator;
71
72 2
    /**
73
     * @var string
74 2
     */
75
    private $template;
76
77 151
    public function __construct($input, string $id, array $params, callable $translator)
78
    {
79 151
        $this->input = $input;
80
        $this->id = $id;
81
        $this->params = $params;
82 194
        $this->translator = $translator;
83
        $this->template = $this->chooseTemplate();
84 194
        $this->message = $this->createMessage();
85 194
    }
86 194
87
    public function updateMode(string $mode): void
88 194
    {
89
        $this->mode = $mode;
90
        $this->message = $this->createMessage();
91 196
    }
92
93 196
    public function updateTemplate(string $template): void
94
    {
95
        $this->template = $template;
96 13
        $this->message = $this->createMessage();
97
    }
98 13
99 13
    public function updateParams(array $params): void
100 3
    {
101
        $this->params = $params;
102
        $this->message = $this->createMessage();
103 13
    }
104
105
    public function getId(): string
106 9
    {
107
        return $this->id;
108 9
    }
109
110
    public function hasParam($name): bool
111 196
    {
112
        return array_key_exists($name, $this->params);
113 196
    }
114 196
115 196
    public function getParam($name)
116 196
    {
117 3
        return $this->params[$name] ?? null;
118
    }
119
120 196
    public function getParams(): array
121
    {
122
        return $this->params;
123 137
    }
124
125 137
    public function __toString(): string
126
    {
127
        return $this->getMessage();
128 196
    }
129
130 196
    public function chooseTemplate()
131
    {
132
        return key(static::$defaultTemplates[$this->mode]);
133 196
    }
134
135 196
    protected function hasCustomTemplate(): bool
136 156
    {
137
        return false === isset(static::$defaultTemplates[$this->mode][$this->template]);
138
    }
139 196
140
    private function createMessage(): string
141
    {
142 137
        $template = $this->createTemplate($this->mode, $this->template);
143
        $params = $this->getParams();
144 137
        $params['name'] = $params['name'] ?? stringify($this->input);
145
        $params['input'] = $this->input;
146
147 194
        return $this->format($template, $params);
148
    }
149 194
150
    private function createTemplate(string $mode, string $template): string
151 194
    {
152
        if (isset(static::$defaultTemplates[$mode][$template])) {
153
            $template = static::$defaultTemplates[$mode][$template];
154 194
        }
155
156 194
        return call_user_func($this->translator, $template);
157
    }
158 194
159
    private function format($template, array $vars = []): string
160
    {
161 87
        return preg_replace_callback(
162
            '/{{(\w+)}}/',
163 87
            function ($match) use ($vars) {
164 87
                if (!isset($vars[$match[1]])) {
165
                    return $match[0];
166 87
                }
167
168 87
                $value = $vars[$match[1]];
169
                if ('name' == $match[1] && is_string($value)) {
170
                    return $value;
171 3
                }
172
173 3
                return stringify($value);
174
            },
175 3
            $template
176
        );
177 3
    }
178
}
179