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 (#894)
by Henrique
15:12
created

ValidationException   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 0
dl 0
loc 201
ccs 85
cts 85
cp 1
rs 9.6
c 0
b 0
f 0

22 Methods

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