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 (#933)
by Emmerson
02:37
created

ValidationException::format()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 1
nop 2
dl 0
loc 17
ccs 12
cts 12
cp 1
crap 4
rs 9.2
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\Exceptions;
15
16
use InvalidArgumentException;
17
use function in_array;
18
use function is_numeric;
19
use function Respect\Stringifier\stringify;
20
21
class ValidationException extends InvalidArgumentException implements ExceptionInterface
22
{
23
    const MODE_DEFAULT = 1;
24
    const MODE_NEGATIVE = 2;
25
    const STANDARD = 0;
26
    public static $defaultTemplates = [
27
        self::MODE_DEFAULT => [
28
            self::STANDARD => 'Data validation failed for %s',
29
        ],
30
        self::MODE_NEGATIVE => [
31
            self::STANDARD => 'Data validation failed for %s',
32
        ],
33
    ];
34
35
    protected $id = 'validation';
36
    protected $mode = self::MODE_DEFAULT;
37
    protected $name = '';
38
    protected $template = '';
39
    protected $params = [];
40
    private $customTemplate = false;
41
42 203
    public static function format($template, array $vars = [])
43
    {
44 203
        return preg_replace_callback(
45 203
            '/{{(\w+)}}/',
46 203
            function ($match) use ($vars) {
47 201
                if (!isset($vars[$match[1]])) {
48 2
                    return $match[0];
49
                }
50
51 201
                $value = $vars[$match[1]];
52 201
                if ('name' == $match[1] && is_string($value)) {
53 198
                    return $value;
54
                }
55
56 42
                return ValidationException::stringify($value);
57 203
            },
58 203
            $template
59
        );
60
    }
61
62
    /**
63
     * @param mixed $value
64
     *
65
     * @return string
66
     */
67 42
    public static function stringify($value)
68
    {
69 42
        return stringify($value);
70
    }
71
72 2
    public function __toString()
73
    {
74 2
        return $this->getMainMessage();
75
    }
76
77 153
    public function chooseTemplate()
78
    {
79 153
        return key(static::$defaultTemplates[$this->mode]);
80
    }
81
82 198
    public function configure($name, array $params = [])
83
    {
84 198
        $this->setName($name);
85 198
        $this->setId($this->guessId());
86 198
        $this->setParams($params);
87
88 198
        return $this;
89
    }
90
91 200
    public function getName()
92
    {
93 200
        return $this->name;
94
    }
95
96 15
    protected function hasName(): bool
97
    {
98 15
        $name = $this->getName();
99 15
        if (is_numeric($name)) {
100 3
            return (bool) (float) $name;
101
        }
102
103 15
        return !in_array($name, ['`FALSE`', '`NULL`', '`{ }`', '""', '']);
104
    }
105
106 9
    public function getId()
107
    {
108 9
        return $this->id;
109
    }
110
111 200
    public function getMainMessage()
112
    {
113 200
        $vars = $this->getParams();
114 200
        $vars['name'] = $this->getName();
115 200
        $template = $this->getTemplate();
116 200
        if (isset($vars['translator']) && is_callable($vars['translator'])) {
117 3
            $template = call_user_func($vars['translator'], $template);
118
        }
119
120 200
        return static::format($template, $vars);
121
    }
122
123 137
    public function getParam($name)
124
    {
125 137
        return $this->hasParam($name) ? $this->params[$name] : false;
126
    }
127
128 200
    public function getParams()
129
    {
130 200
        return $this->params;
131
    }
132
133 200
    public function getTemplate()
134
    {
135 200
        if (!empty($this->template)) {
136 160
            return $this->template;
137
        }
138
139 200
        return $this->template = $this->buildTemplate();
140
    }
141
142 137
    public function hasParam($name)
143
    {
144 137
        return isset($this->params[$name]);
145
    }
146
147 198
    public function setId($id)
148
    {
149 198
        $this->id = $id;
150
151 198
        return $this;
152
    }
153
154 198
    public function setName($name)
155
    {
156 198
        $this->name = $name;
157
158 198
        return $this;
159
    }
160
161 87
    public function setMode($mode)
162
    {
163 87
        $this->mode = $mode;
164 87
        $this->template = $this->buildTemplate();
165
166 87
        $this->buildMessage();
167
168 87
        return $this;
169
    }
170
171 3
    public function setParam($key, $value)
172
    {
173 3
        $this->params[$key] = $value;
174
175 3
        $this->buildMessage();
176
177 3
        return $this;
178
    }
179
180 198
    public function setParams(array $params)
181
    {
182 198
        foreach ($params as $key => $value) {
183 196
            $this->params[$key] = $value;
184
        }
185
186 198
        $this->buildMessage();
187
188 198
        return $this;
189
    }
190
191 113
    public function hasCustomTemplate()
192
    {
193 113
        return true === $this->customTemplate;
194
    }
195
196 10
    public function setTemplate($template)
197
    {
198 10
        $this->customTemplate = true;
199 10
        if (isset(static::$defaultTemplates[$this->mode][$template])) {
200 1
            $template = static::$defaultTemplates[$this->mode][$template];
201
        }
202 10
        $this->template = $template;
203
204 10
        $this->buildMessage();
205
206 10
        return $this;
207
    }
208
209 198
    private function buildMessage(): void
210
    {
211 198
        $this->message = $this->getMainMessage();
212 198
    }
213
214 200
    protected function buildTemplate()
215
    {
216 200
        $templateKey = $this->chooseTemplate();
217
218 200
        return static::$defaultTemplates[$this->mode][$templateKey];
219
    }
220
221 198
    public function guessId()
222
    {
223 198
        if (!empty($this->id) && 'validation' != $this->id) {
224 10
            return $this->id;
225
        }
226
227 198
        $pieces = explode('\\', get_called_class());
228 198
        $exceptionClassShortName = end($pieces);
229 198
        $ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName);
230 198
        $ruleName = lcfirst($ruleClassShortName);
231
232 198
        return $ruleName;
233
    }
234
}
235