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.

ValidatesRequests::validateWithBag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 5
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Milkmeowo\Framework\Base\Validation;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\UrlGenerator;
8
use Dingo\Api\Exception\ResourceException;
9
use Illuminate\Contracts\Validation\Factory;
10
use Illuminate\Contracts\Validation\Validator;
11
use Illuminate\Validation\ValidationException;
12
13
trait ValidatesRequests
14
{
15
    /**
16
     * The default error bag.
17
     *
18
     * @var string
19
     */
20
    protected $validatesRequestErrorBag;
21
22
    /**
23
     * Run the validation routine against the given validator.
24
     *
25
     * @param  \Illuminate\Contracts\Validation\Validator|array  $validator
26
     * @param  \Illuminate\Http\Request|null  $request
27
     * @return void
28
     */
29
    public function validateWith($validator, Request $request = null)
30
    {
31
        $request = $request ?: app('request');
32
33
        if (is_array($validator)) {
34
            $validator = $this->getValidationFactory()->make($request->all(), $validator);
35
        }
36
37
        if ($validator->fails()) {
38
            $this->throwValidationException($request, $validator);
39
        }
40
    }
41
42
    /**
43
     * Validate the given request with the given rules.
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @param  array  $rules
47
     * @param  array  $messages
48
     * @param  array  $customAttributes
49
     * @return void
50
     */
51 View Code Duplication
    public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
54
55
        if ($validator->fails()) {
56
            $this->throwValidationException($request, $validator);
57
        }
58
    }
59
60
    /**
61
     * Validate the given data with the given rules.
62
     *
63
     * @param  \Illuminate\Http\Request $request
64
     * @param array $data
65
     * @param  array $rules
66
     * @param  array $messages
67
     * @param  array $customAttributes
68
     */
69 View Code Duplication
    public function validateData(Request $request, array $data, array $rules, array $messages = [], array $customAttributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $validator = $this->getValidationFactory()->make($data, $rules, $messages, $customAttributes);
72
73
        if ($validator->fails()) {
74
            $this->throwValidationException($request, $validator);
75
        }
76
    }
77
78
    /**
79
     * Validate the given request with the given rules.
80
     *
81
     * @param  string  $errorBag
82
     * @param  \Illuminate\Http\Request  $request
83
     * @param  array  $rules
84
     * @param  array  $messages
85
     * @param  array  $customAttributes
86
     * @return void
87
     *
88
     * @throws \Illuminate\Validation\ValidationException
89
     */
90
    public function validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = [])
91
    {
92
        $this->withErrorBag($errorBag, function () use ($request, $rules, $messages, $customAttributes) {
93
            $this->validate($request, $rules, $messages, $customAttributes);
94
        });
95
    }
96
97
    /**
98
     * Throw the failed validation exception.
99
     *
100
     * @param  \Illuminate\Http\Request  $request
101
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
102
     * @return void
103
     *
104
     * @throws \Illuminate\Validation\ValidationException
105
     */
106
    protected function throwValidationException(Request $request, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        throw new ResourceException($validator->errors()->first(), $validator->errors());
109
//        throw new ValidationException($validator, $this->buildFailedValidationResponse(
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
//            $request, $this->formatValidationErrors($validator)
111
//        ));
112
    }
113
114
    /**
115
     * Create the response for when a request fails validation.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     * @param  array  $errors
119
     * @return \Symfony\Component\HttpFoundation\Response
120
     */
121
    protected function buildFailedValidationResponse(Request $request, array $errors)
122
    {
123
        if ($request->expectsJson()) {
124
            return new JsonResponse($errors, 422);
125
        }
126
127
        return redirect()->to($this->getRedirectUrl())
128
                        ->withInput($request->input())
0 ignored issues
show
Bug introduced by
It seems like $request->input() targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type string; however, Illuminate\Http\RedirectResponse::withInput() does only seem to accept null|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129
                        ->withErrors($errors, $this->errorBag());
130
    }
131
132
    /**
133
     * Format the validation errors to be returned.
134
     *
135
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
136
     * @return array
137
     */
138
    protected function formatValidationErrors(Validator $validator)
139
    {
140
        return $validator->errors()->getMessages();
141
    }
142
143
    /**
144
     * Get the URL we should redirect to.
145
     *
146
     * @return string
147
     */
148
    protected function getRedirectUrl()
149
    {
150
        return app(UrlGenerator::class)->previous();
151
    }
152
153
    /**
154
     * Get a validation factory instance.
155
     *
156
     * @return \Illuminate\Contracts\Validation\Factory
157
     */
158
    protected function getValidationFactory()
159
    {
160
        return app(Factory::class);
161
    }
162
163
    /**
164
     * Execute a Closure within with a given error bag set as the default bag.
165
     *
166
     * @param  string  $errorBag
167
     * @param  callable  $callback
168
     * @return void
169
     */
170
    protected function withErrorBag($errorBag, callable $callback)
171
    {
172
        $this->validatesRequestErrorBag = $errorBag;
173
174
        call_user_func($callback);
175
176
        $this->validatesRequestErrorBag = null;
177
    }
178
179
    /**
180
     * Get the key to be used for the view error bag.
181
     *
182
     * @return string
183
     */
184
    protected function errorBag()
185
    {
186
        return $this->validatesRequestErrorBag ?: 'default';
187
    }
188
}
189