GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — main ( 4a6e97...8d738a )
by Sugavanas
13:01
created

Validator::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace TS\PHPValidator;
4
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Respect\Validation\Exceptions\NestedValidationException;
7
8
class Validator
9
{
10
    /**
11
     * @var array values
12
     */
13
    protected $values = [];
14
15
    /**
16
     * @var array errors
17
     */
18
    protected $errors = [];
19
20
    /**
21
     * @var bool Use Session variable to store data
22
     * If set to true, use ValidationMiddleware to extract the error from the Session.
23
     */
24
    protected $useSession;
25
26
    /**
27
     * Validator constructor.
28
     * @param array $config
29
     */
30
    public function __construct(array $config = [])
31
    {
32
        $this->useSession = $config['useSession'] ?? false;
33
    }
34
35
    /**
36
     * @param Request|array $params
37
     * @param array $rules
38
     * @param mixed $default
39
     * @return $this
40
     * @throws \Exception
41
     */
42
    public function validate($params, array $rules, $default = null): Validator
43
    {
44
        if ($params instanceof Request) {
45
            return $this->validateRequest($params, $rules, $default);
46
        } elseif (is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
47
            return $this->validateArray($params, $rules, $default);
48
        }
49
50
        throw new \Exception('Unknown type given for $params.');
51
    }
52
53
    /**
54
     * @param Request $request
55
     * @param array $rules
56
     * @param mixed $default
57
     * @return $this
58
     */
59
    public function validateRequest(Request $request, array $rules, $default = null)
60
    {
61
        $params = $request->getParsedBody();
62
        return $this->runValidation($params, $rules, $default);
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type null and object; however, parameter $params of TS\PHPValidator\Validator::runValidation() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return $this->runValidation(/** @scrutinizer ignore-type */ $params, $rules, $default);
Loading history...
63
    }
64
65
    /**
66
     * @param array $params
67
     * @param array $rules
68
     * @param mixed $default
69
     * @return $this
70
     */
71
    public function validateArray(array $params, array $rules, $default = null)
72
    {
73
        return $this->runValidation($params, $rules, $default);
74
    }
75
76
    /**
77
     * @param array $params
78
     * @param array $rules
79
     * @param mixed $default
80
     * @return Validator
81
     */
82
    protected function runValidation(array $params, array $rules, $default = null)
83
    {
84
        foreach ($rules as $field => $rule) {
85
            try {
86
                $param = isset($params[$field]) ? $params[$field] : $default;
87
                $this->values[$field] = $param;
88
                $rule->setName(ucfirst($field))->assert($param);
89
            } catch (NestedValidationException $e) {
90
                $this->errors[$field] = $e->getMessages();
91
            }
92
        }
93
94
        if ($this->failed() && $this->useSession) {
95
            $_SESSION['TS_PHPValidator_Errors'] = $this->errors;
96
            $_SESSION['TS_PHPValidator_Values'] = $this->values;
97
        }
98
        return $this;
99
    }
100
101
    /**
102
     * Validation failed?
103
     * @return bool
104
     */
105
    public function failed(): bool
106
    {
107
        return !empty($this->errors);
108
    }
109
110
    /**
111
     * Is the validation valid.
112
     * @return bool
113
     */
114
    public function isValid(): bool
115
    {
116
        return empty($this->errors);
117
    }
118
119
    /**
120
     * @param array $errors
121
     */
122
    public function setErrors(array $errors)
123
    {
124
        $this->errors = $errors;
125
    }
126
127
    /**
128
     * Get all errors
129
     * @return array
130
     */
131
    public function getErrors(): array
132
    {
133
        return $this->errors;
134
    }
135
136
    /**
137
     * @param array $values
138
     */
139
    public function setValues(array $values)
140
    {
141
        $this->values = $values;
142
    }
143
144
145
    /**
146
     * Set value by key
147
     * @param $key
148
     * @param mixed $default
149
     */
150
    public function setValue($key, $value)
151
    {
152
        $this->values[$key] = $value;
153
    }
154
155
    /**
156
     * Get all values
157
     * @return array
158
     */
159
    public function getValues(): array
160
    {
161
        return $this->values;
162
    }
163
164
    /**
165
     * Get value by key
166
     * @param $key
167
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
168
     * @return mixed|null
169
     */
170
    public function getValue($key, $default = null)
171
    {
172
        return isset($this->values[$key]) ? $this->values[$key] : $default;
173
    }
174
}
175