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
Push — master ( 60e3fc...b69607 )
by Henrique
02:17
created

ValidationException   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 212
ccs 92
cts 92
cp 1
rs 9
c 0
b 0
f 0
wmc 35

23 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A getMainMessage() 0 10 3
A __toString() 0 3 1
A getParams() 0 3 1
A format() 0 17 4
A getId() 0 3 1
A setName() 0 5 1
A setMode() 0 8 1
A getTemplate() 0 7 2
A chooseTemplate() 0 3 1
A buildMessage() 0 3 1
A buildTemplate() 0 5 1
A setParams() 0 9 2
A setId() 0 5 1
A hasParam() 0 3 1
A stringify() 0 3 1
A setParam() 0 7 1
A getName() 0 3 1
A getParam() 0 3 2
A guessId() 0 12 3
A hasName() 0 8 2
A setTemplate() 0 11 2
A hasCustomTemplate() 0 3 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 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 221
    public static function format($template, array $vars = [])
43
    {
44 221
        return preg_replace_callback(
45 221
            '/{{(\w+)}}/',
46 221
            function ($match) use ($vars) {
47 219
                if (!isset($vars[$match[1]])) {
48 2
                    return $match[0];
49
                }
50
51 219
                $value = $vars[$match[1]];
52 219
                if ('name' == $match[1] && is_string($value)) {
53 216
                    return $value;
54
                }
55
56 42
                return ValidationException::stringify($value);
57 221
            },
58 221
            $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 171
    public function chooseTemplate()
78
    {
79 171
        return key(static::$defaultTemplates[$this->mode]);
80
    }
81
82 216
    public function configure($name, array $params = [])
83
    {
84 216
        $this->setName($name);
85 216
        $this->setId($this->guessId());
86 216
        $this->setParams($params);
87
88 216
        return $this;
89
    }
90
91 218
    public function getName()
92
    {
93 218
        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 218
    public function getMainMessage()
112
    {
113 218
        $vars = $this->getParams();
114 218
        $vars['name'] = $this->getName();
115 218
        $template = $this->getTemplate();
116 218
        if (isset($vars['translator']) && is_callable($vars['translator'])) {
117 3
            $template = call_user_func($vars['translator'], $template);
118
        }
119
120 218
        return static::format($template, $vars);
121
    }
122
123 143
    public function getParam($name)
124
    {
125 143
        return $this->hasParam($name) ? $this->params[$name] : false;
126
    }
127
128 218
    public function getParams()
129
    {
130 218
        return $this->params;
131
    }
132
133 218
    public function getTemplate()
134
    {
135 218
        if (!empty($this->template)) {
136 172
            return $this->template;
137
        }
138
139 218
        return $this->template = $this->buildTemplate();
140
    }
141
142 143
    public function hasParam($name)
143
    {
144 143
        return isset($this->params[$name]);
145
    }
146
147 216
    public function setId($id)
148
    {
149 216
        $this->id = $id;
150
151 216
        return $this;
152
    }
153
154 216
    public function setName($name)
155
    {
156 216
        $this->name = $name;
157
158 216
        return $this;
159
    }
160
161 91
    public function setMode($mode)
162
    {
163 91
        $this->mode = $mode;
164 91
        $this->template = $this->buildTemplate();
165
166 91
        $this->buildMessage();
167
168 91
        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 216
    public function setParams(array $params)
181
    {
182 216
        foreach ($params as $key => $value) {
183 214
            $this->params[$key] = $value;
184
        }
185
186 216
        $this->buildMessage();
187
188 216
        return $this;
189
    }
190
191 119
    public function hasCustomTemplate()
192
    {
193 119
        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 216
    private function buildMessage(): void
210
    {
211 216
        $this->message = $this->getMainMessage();
212 216
    }
213
214 218
    protected function buildTemplate()
215
    {
216 218
        $templateKey = $this->chooseTemplate();
217
218 218
        return static::$defaultTemplates[$this->mode][$templateKey];
219
    }
220
221 216
    public function guessId()
222
    {
223 216
        if (!empty($this->id) && 'validation' != $this->id) {
224 10
            return $this->id;
225
        }
226
227 216
        $pieces = explode('\\', get_called_class());
228 216
        $exceptionClassShortName = end($pieces);
229 216
        $ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName);
230 216
        $ruleName = lcfirst($ruleClassShortName);
231
232 216
        return $ruleName;
233
    }
234
}
235