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.

EnumRequest::transformEnums()   C
last analyzed

Complexity

Conditions 12
Paths 1

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 27
c 5
b 1
f 0
dl 0
loc 54
rs 6.9666
cc 12
nc 1
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Spatie\Enum\Laravel\Http;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Spatie\Enum\Enum;
8
9
/**
10
 * @internal This class is only used to get mixed into \Illuminate\Http\Request
11
 *
12
 * @mixin \Illuminate\Http\Request
13
 */
14
final class EnumRequest
15
{
16
    public const REQUEST_ROUTE = 'route';
17
    public const REQUEST_QUERY = 'query';
18
    public const REQUEST_REQUEST = 'request';
19
20
    public function transformEnums(): Closure
21
    {
22
        return function (array $transformations): void {
23
            if (isset($transformations[EnumRequest::REQUEST_ROUTE])) {
24
                $route = $this->route();
0 ignored issues
show
Bug introduced by
The method route() does not exist on Spatie\Enum\Laravel\Http\EnumRequest. ( Ignorable by Annotation )

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

24
                /** @scrutinizer ignore-call */ 
25
                $route = $this->route();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
                /** @var string|Enum $enumClass */
27
                foreach ($transformations[EnumRequest::REQUEST_ROUTE] as $key => $enumClass) {
28
                    if (! $route->hasParameter($key)) {
29
                        continue;
30
                    }
31
32
                    $route->setParameter(
33
                        $key,
34
                        $enumClass::make($route->parameter($key))
35
                    );
36
                }
37
            }
38
39
            if (isset($transformations[EnumRequest::REQUEST_QUERY])) {
40
                /** @var string|Enum $enumClass */
41
                foreach ($transformations[EnumRequest::REQUEST_QUERY] as $key => $enumClass) {
42
                    if (! $this->query->has($key)) {
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist on Spatie\Enum\Laravel\Http\EnumRequest. Did you maybe forget to declare it?
Loading history...
43
                        continue;
44
                    }
45
46
                    $this->query->set(
47
                        $key,
48
                        $enumClass::make($this->query->get($key))
49
                    );
50
                }
51
            }
52
53
            if (isset($transformations[EnumRequest::REQUEST_REQUEST])) {
54
                /** @var string|Enum $enumClass */
55
                foreach ($transformations[EnumRequest::REQUEST_REQUEST] as $key => $enumClass) {
56
                    if (! $this->request->has($key)) {
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on Spatie\Enum\Laravel\Http\EnumRequest. Did you maybe forget to declare it?
Loading history...
57
                        continue;
58
                    }
59
60
                    $this->request->set(
61
                        $key,
62
                        $enumClass::make($this->request->get($key))
63
                    );
64
                }
65
            }
66
67
            /** @var string|Enum $enumClass */
68
            foreach (Arr::except($transformations, [EnumRequest::REQUEST_ROUTE, EnumRequest::REQUEST_QUERY, EnumRequest::REQUEST_REQUEST]) as $key => $enumClass) {
69
                if (! isset($this[$key])) {
70
                    continue;
71
                }
72
73
                $this[$key] = $enumClass::make($this[$key]);
74
            }
75
        };
76
    }
77
}
78