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 (#957)
by Henrique
02:30
created

ValidationException::getMainMessage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ValidationException::updateParams() 0 4 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 16
    public function __construct($input, string $id, array $params, callable $translator)
77
    {
78 16
        $this->input = $input;
79 16
        $this->id = $id;
80 16
        $this->params = $params;
81 16
        $this->translator = $translator;
82 16
        $this->template = $this->chooseTemplate();
83 16
        $this->message = $this->createMessage();
84 16
    }
85
86 1
    public function getId(): string
87
    {
88 1
        return $this->id;
89
    }
90
91 16
    public function getParams(): array
92
    {
93 16
        return $this->params;
94
    }
95
96 4
    public function getParam($name)
97
    {
98 4
        return $this->params[$name] ?? null;
99
    }
100
101 1
    public function updateMode(string $mode): void
102
    {
103 1
        $this->mode = $mode;
104 1
        $this->message = $this->createMessage();
105 1
    }
106
107 6
    public function updateTemplate(string $template): void
108
    {
109 6
        $this->template = $template;
110 6
        $this->message = $this->createMessage();
111 6
    }
112
113 1
    public function updateParams(array $params): void
114
    {
115 1
        $this->params = $params;
116 1
        $this->message = $this->createMessage();
117 1
    }
118
119 1
    public function hasCustomTemplate(): bool
120
    {
121 1
        return false === isset(static::$defaultTemplates[$this->mode][$this->template]);
122
    }
123
124 3
    public function __toString(): string
125
    {
126 3
        return $this->getMessage();
127
    }
128
129 16
    protected function chooseTemplate(): string
130
    {
131 16
        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...
132
    }
133
134 16
    private function createMessage(): string
135
    {
136 16
        $template = $this->createTemplate($this->mode, $this->template);
137 16
        $params = $this->getParams();
138 16
        $params['name'] = $params['name'] ?? stringify($this->input);
139 16
        $params['input'] = $this->input;
140
141 16
        return $this->format($template, $params);
142
    }
143
144 16
    private function createTemplate(string $mode, string $template): string
145
    {
146 16
        if (isset(static::$defaultTemplates[$mode][$template])) {
147 16
            $template = static::$defaultTemplates[$mode][$template];
148
        }
149
150 16
        return call_user_func($this->translator, $template);
151
    }
152
153 16
    private function format($template, array $vars = []): string
154
    {
155 16
        return preg_replace_callback(
156 16
            '/{{(\w+)}}/',
157 16
            function ($match) use ($vars) {
158 16
                if (!isset($vars[$match[1]])) {
159 1
                    return $match[0];
160
                }
161
162 16
                $value = $vars[$match[1]];
163 16
                if ('name' == $match[1] && is_string($value)) {
164 16
                    return $value;
165
                }
166
167 3
                return stringify($value);
168 16
            },
169 16
            $template
170
        );
171
    }
172
}
173