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 ( 0f49e2...4a6e97 )
by Sugavanas
07:44
created

Validator::failed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
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 $request
37
     * @param array $rules
38
     * @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...
39
     * @return $this
40
     */
41
    public function validate(Request $request, array $rules, $default = null): Validator
42
    {
43
        //$this->values = [];
44
        //$this->errors = [];
45
        $params = $request->getParsedBody();
46
        foreach ($rules as $field => $rule) {
47
            try {
48
                $param = isset($params[$field]) ? $params[$field] : $default;
49
                $this->values[$field] = $param;
50
                $rule->setName(ucfirst($field))->assert($param);
51
            } catch (NestedValidationException $e) {
52
                $this->errors[$field] = $e->getMessages();
53
            }
54
        }
55
56
        if ($this->failed() && $this->useSession) {
57
            $_SESSION['TS_PHPValidator_Errors'] = $this->errors;
58
            $_SESSION['TS_PHPValidator_Values'] = $this->values;
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Validation failed?
66
     * @return bool
67
     */
68
    public function failed(): bool
69
    {
70
        return !empty($this->errors);
71
    }
72
73
    /**
74
     * Is the validation valid.
75
     * @return bool
76
     */
77
    public function isValid(): bool
78
    {
79
        return empty($this->errors);
80
    }
81
82
    /**
83
     * @param array $errors
84
     */
85
    public function setErrors(array $errors)
86
    {
87
        $this->errors = $errors;
88
    }
89
90
    /**
91
     * Get all errors
92
     * @return array
93
     */
94
    public function getErrors(): array
95
    {
96
        return $this->errors;
97
    }
98
99
    /**
100
     * @param array $values
101
     */
102
    public function setValues(array $values)
103
    {
104
        $this->values = $values;
105
    }
106
107
108
    /**
109
     * Set value by key
110
     * @param $key
111
     * @param mixed $default
112
     */
113
    public function setValue($key, $value)
114
    {
115
        $this->values[$key] = $value;
116
    }
117
118
    /**
119
     * Get all values
120
     * @return array
121
     */
122
    public function getValues(): array
123
    {
124
        return $this->values;
125
    }
126
127
    /**
128
     * Get value by key
129
     * @param $key
130
     * @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...
131
     * @return mixed|null
132
     */
133
    public function getValue($key, $default = null)
134
    {
135
        return isset($this->values[$key]) ? $this->values[$key] : $default;
136
    }
137
}
138