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
Pull Request — master (#170)
by
unknown
02:50
created

FiltersOnHttpContext::fromRequest()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 52
rs 9.36
c 0
b 0
f 0
cc 4
nc 3
nop 1

How to fix   Long Method   

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 Mado\QueryBundle\Filters;
4
5
6
use Mado\QueryBundle\Exceptions\InvalidFiltersException;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class FiltersOnHttpContext extends Filters
10
{
11
    /**
12
     * @param Request $request
13
     *
14
     * @return FiltersOnHttpContext
15
     * @throws InvalidFiltersException
16
     */
17
    public static function fromRequest(Request $request): FiltersOnHttpContext
18
    {
19
        $filters = new self();
20
21
        $id          = $request->attributes->get('id');
22
        $route       = $request->attributes->get('_route');
23
        $routeParams = $request->attributes->get('_route_params', []);
24
25
        $rel         = $request->query->get('rel', '');
26
        $select      = $request->query->get('select', '');
27
        $printing    = $request->query->get('printing', []);
28
29
        $andFilters  = $request->query->get('filtering', []);
30
        $orFilters   = $request->query->get('filtering_or', []);
31
        $sorting     = $request->query->get('sorting', []);
32
33
        $page        = $request->query->get('page', '');
34
        $limit       = $request->query->get('limit', '');
35
36
        $filters->ensureFilterIsValid($printing);
37
        $filters->ensureFilterIsValid($andFilters);
38
        $filters->ensureFilterIsValid($orFilters);
39
        $filters->ensureFilterIsValid($sorting);
40
41
        // FIXME: what is it for? Can be removed?
42
        $filterOrCorrected = [];
43
        foreach ($orFilters as $key => $filterValue) {
44
            if (is_array($filterValue)) {
45
                foreach ($filterValue as $keyInternal => $internal) {
46
                    $filterOrCorrected[$keyInternal . '|' . $filters->getDiscriminator()] = $internal;
47
                }
48
            } else {
49
                $filterOrCorrected[$key] = $filterValue;
50
            }
51
        }
52
53
        $filters->setId($id);
54
        $filters->setRoute($route);
55
        $filters->setRouteParams($routeParams);
56
57
        $filters->setRel($rel);
58
        $filters->setSelect($select);
59
        $filters->setPrinting($printing);
60
61
        $filters->setAndFilters($andFilters);
62
        $filters->setOrFilters($filterOrCorrected);
63
        $filters->setSorting($sorting);
64
65
        $filters->setPage($page);
66
        $filters->setLimit($limit);
67
68
        return $filters;
69
    }
70
71
    /**
72
     * @param $filters
73
     *
74
     * @throws InvalidFiltersException
75
     */
76
    private function ensureFilterIsValid($filters)
77
    {
78
        if (!is_array($filters)) {
79
80
            $message = "Wrong query string exception: ";
81
            $message .= var_export($filters, true) . "\n";
82
            $message .= "Please check query string should be something like " .
83
                "http://127.0.0.1:8000/?filtering[status]=todo";
84
85
            throw new InvalidFiltersException($message);
86
        }
87
    }
88
}
89