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 (#168)
by Simone
05:11 queued 02:38
created

QueryOptionsBuilder::ensureFilterIsValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Mado\QueryBundle\Queries\Options;
4
5
use Mado\QueryBundle\Exceptions\InvalidFiltersException;
6
7
class QueryOptionsBuilder
8
{
9
    private $entityAlias;
10
11
    public function setEntityAlias(string $entityAlias)
12
    {
13
        $this->entityAlias = $entityAlias;
14
    }
15
16
    public function builderFromRequest(Request $request = null)
0 ignored issues
show
Bug introduced by
The type Mado\QueryBundle\Queries\Options\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
    {
18
        $this->ensureEntityAliasIsDefined();
19
20
        $requestAttributes = [];
21
        foreach ($request->attributes->all() as $attributeName => $attributeValue) {
22
            $requestAttributes[$attributeName] = $request->attributes->get(
23
                $attributeName,
24
                $attributeValue
25
            );
26
        }
27
28
        $filters     = $request->query->get('filtering', []);
29
        $orFilters   = $request->query->get('filtering_or', []);
30
        $sorting     = $request->query->get('sorting', []);
31
        $printing    = $request->query->get('printing', []);
32
        $rel         = $request->query->get('rel', '');
33
        $page        = $request->query->get('page', '');
34
        $select      = $request->query->get('select', $this->getEntityAlias());
0 ignored issues
show
Bug introduced by
The method getEntityAlias() does not exist on Mado\QueryBundle\Queries...ons\QueryOptionsBuilder. ( Ignorable by Annotation )

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

34
        $select      = $request->query->get('select', $this->/** @scrutinizer ignore-call */ getEntityAlias());

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...
35
        $filtering   = $request->query->get('filtering', '');
36
        $limit       = $request->query->get('limit', '');
37
38
        $filterOrCorrected = [];
39
40
        $count = 0;
41
        foreach ($orFilters as $key => $filter) {
42
            if (is_array($filter)) {
43
                foreach ($filter as $keyInternal => $internal) {
44
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
45
                    $count += 1;
46
                }
47
            } else {
48
                $filterOrCorrected[$key] = $filter;
49
            }
50
        }
51
52
        $requestProperties = [
53
            'filtering'   => $filtering,
54
            'orFiltering' => $filterOrCorrected,
55
            'limit'       => $limit,
56
            'page'        => $page,
57
            'filters'     => $filters,
58
            'orFilters'   => $filterOrCorrected,
59
            'sorting'     => $sorting,
60
            'rel'         => $rel,
61
            'printing'    => $printing,
62
            'select'      => $select,
63
        ];
64
65
        $options = array_merge(
66
            $requestAttributes,
67
            $requestProperties
68
        );
69
70
        return QueryBuilderOptions::fromArray($options);
0 ignored issues
show
Bug introduced by
The type Mado\QueryBundle\Queries...ons\QueryBuilderOptions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
    }
72
73
    public function ensureEntityAliasIsDefined()
74
    {
75
        if (!$this->entityAlias) {
76
            throw new \RuntimeException(
77
                'Oops! Entity alias is missing'
78
            );
79
        }
80
    }
81
82
    public function buildFromRequestAndCustomFilter(Request $request, $filter)
83
    {
84
        $this->ensureEntityAliasIsDefined();
85
86
        $filters   = $request->query->get('filtering', []);
87
        $orFilters = $request->query->get('filtering_or', []);
88
        $sorting   = $request->query->get('sorting', []);
89
        $printing  = $request->query->get('printing', []);
90
        $rel       = $request->query->get('rel', '');
91
        $page      = $request->query->get('page', '');
92
        $select    = $request->query->get('select', $this->getEntityAlias());
93
        $filtering = $request->query->get('filtering', '');
94
        $limit     = $request->query->get('limit', '');
95
        $justCount = $request->query->get('justCount', 'false');
96
97
        $this->ensureFilterIsValid($filters);
98
99
        $filters = array_merge($filters, $filter);
100
101
        $filterOrCorrected = [];
102
103
        $count = 0;
104
        foreach ($orFilters as $key => $filterValue) {
105
            if (is_array($filterValue)) {
106
                foreach ($filterValue as $keyInternal => $internal) {
107
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
108
                    $count += 1;
109
                }
110
            } else {
111
                $filterOrCorrected[$key] = $filterValue;
112
            }
113
        }
114
115
        return QueryBuilderOptions::fromArray([
116
            '_route'        => $request->attributes->get('_route'),
117
            '_route_params' => $request->attributes->get('_route_params', []),
118
            'id'            => $request->attributes->get('id'),
119
            'filtering'     => $filtering,
120
            'limit'         => $limit,
121
            'page'          => $page,
122
            'filters'       => $filters,
123
            'orFilters'     => $filterOrCorrected,
124
            'sorting'       => $sorting,
125
            'rel'           => $rel,
126
            'printing'      => $printing,
127
            'select'        => $select,
128
            'justCount'     => $justCount,
129
        ]);
130
    }
131
132
    private function ensureFilterIsValid($filters)
133
    {
134
        if (!is_array($filters)) {
135
            throw new InvalidFiltersException(
136
                "Wrong query string exception: "
137
                . var_export($filters, true) . "\n\n"
138
                . "Please check query string should be something like "
139
                . "http://<host>:<port>/?filtering[<field>|<operator>]=<value>"
140
            );
141
        }
142
    }
143
144
    public function buildForOrFilter(Request $request, $orFilter)
145
    {
146
        $this->ensureEntityAliasIsDefined();
147
148
        $filters   = $request->query->get('filtering', []);
149
        $orFilters = $request->query->get('filtering_or', []);
150
        $sorting   = $request->query->get('sorting', []);
151
        $printing  = $request->query->get('printing', []);
152
        $rel       = $request->query->get('rel', '');
153
        $page      = $request->query->get('page', '');
154
        $select    = $request->query->get('select', $this->getEntityAlias());
155
        $filtering = $request->query->get('filtering', '');
156
        $limit     = $request->query->get('limit', '');
157
158
        $orFilters = array_merge($orFilters, $orFilter);
159
160
        $filterOrCorrected = [];
161
162
        $count = 0;
163
        foreach ($orFilters as $key => $filter) {
164
            if (is_array($filter)) {
165
                foreach ($filter as $keyInternal => $internal) {
166
                    $filterOrCorrected[$keyInternal . '|' . $count] = $internal;
167
                    $count += 1;
168
                }
169
            } else {
170
                $filterOrCorrected[$key] = $filter;
171
            }
172
        }
173
174
        return QueryBuilderOptions::fromArray([
175
            '_route'        => $request->attributes->get('_route'),
176
            '_route_params' => $request->attributes->get('_route_params', []),
177
            'id'            => $request->attributes->get('id'),
178
            'filtering'     => $filtering,
179
            'limit'         => $limit,
180
            'page'          => $page,
181
            'filters'       => $filters,
182
            'orFilters'     => $filterOrCorrected,
183
            'sorting'       => $sorting,
184
            'rel'           => $rel,
185
            'printing'      => $printing,
186
            'select'        => $select,
187
        ]);
188
    }
189
}
190