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:29
created

ValidationException   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 149
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __toString() 0 3 1
A updateMode() 0 4 1
A createTemplate() 0 7 2
A format() 0 17 4
A getParams() 0 3 1
A createMessage() 0 8 1
A getId() 0 3 1
A chooseTemplate() 0 3 1
A updateTemplate() 0 4 1
A updateParams() 0 4 1
A hasParam() 0 3 1
A getParam() 0 3 1
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 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
        self::MODE_NEGATIVE => [
43
            self::STANDARD => 'Data validation failed for {{name}}',
44
        ],
45
    ];
46
47
    /**
48
     * @var mixed
49
     */
50
    private $input;
51
52
    /**
53
     * @var string
54
     */
55
    private $id;
56
57
    /**
58
     * @var string
59
     */
60
    private $mode = self::MODE_DEFAULT;
61
62
    /**
63
     * @var array
64
     */
65
    private $params = [];
66
67
    /**
68
     * @var callable
69
     */
70
    private $translator;
71
72
    /**
73
     * @var string
74
     */
75
    private $template;
76
77
    public function __construct($input, string $id, array $params, callable $translator)
78
    {
79
        $this->input = $input;
80
        $this->id = $id;
81
        $this->params = $params;
82
        $this->translator = $translator;
83
        $this->template = $this->chooseTemplate();
84
        $this->message = $this->createMessage();
85
    }
86
87
    public function updateMode(string $mode): void
88
    {
89
        $this->mode = $mode;
90
        $this->message = $this->createMessage();
91
    }
92
93
    public function updateTemplate(string $template): void
94
    {
95
        $this->template = $template;
96
        $this->message = $this->createMessage();
97
    }
98
99
    public function updateParams(array $params): void
100
    {
101
        $this->params = $params;
102
        $this->message = $this->createMessage();
103
    }
104
105
    public function getId(): string
106
    {
107
        return $this->id;
108
    }
109
110
    public function hasParam($name): bool
111
    {
112
        return array_key_exists($name, $this->params);
113
    }
114
115
    public function getParam($name)
116
    {
117
        return $this->params[$name] ?? null;
118
    }
119
120
    public function getParams(): array
121
    {
122
        return $this->params;
123
    }
124
125
    public function __toString(): string
126
    {
127
        return $this->getMessage();
128
    }
129
130
    public function chooseTemplate(): string
131
    {
132
        return key(static::$defaultTemplates[$this->mode]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key(static::defaultTemplates[$this->mode]) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
133
    }
134
135
    protected function hasCustomTemplate(): bool
136
    {
137
        return false === isset(static::$defaultTemplates[$this->mode][$this->template]);
138
    }
139
140
    private function createMessage(): string
141
    {
142
        $template = $this->createTemplate($this->mode, $this->template);
143
        $params = $this->getParams();
144
        $params['name'] = $params['name'] ?? stringify($this->input);
145
        $params['input'] = $this->input;
146
147
        return $this->format($template, $params);
148
    }
149
150
    private function createTemplate(string $mode, string $template): string
151
    {
152
        if (isset(static::$defaultTemplates[$mode][$template])) {
153
            $template = static::$defaultTemplates[$mode][$template];
154
        }
155
156
        return call_user_func($this->translator, $template);
157
    }
158
159
    private function format($template, array $vars = []): string
160
    {
161
        return preg_replace_callback(
162
            '/{{(\w+)}}/',
163
            function ($match) use ($vars) {
164
                if (!isset($vars[$match[1]])) {
165
                    return $match[0];
166
                }
167
168
                $value = $vars[$match[1]];
169
                if ('name' == $match[1] && is_string($value)) {
170
                    return $value;
171
                }
172
173
                return stringify($value);
174
            },
175
            $template
176
        );
177
    }
178
}
179