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
Pull Request — master (#1034)
by Henrique
02:29
created

ValidationException::updateMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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