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.
Completed
Pull Request — master (#1)
by
unknown
05:40
created

Builder::withRequestParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of vaibhavpandeyvpz/doctrine-datatables package.
5
 *
6
 * (c) Vaibhav Pandey <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 */
11
12
namespace Doctrine\DataTables;
13
14
use Doctrine\DBAL\Query\QueryBuilder;
15
use Doctrine\ORM\QueryBuilder as ORMQueryBuilder;
16
17
/**
18
 * Class Builder
19
 * @package Doctrine\DataTables
20
 */
21
class Builder
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $columnAliases = array();
27
28
    /**
29
     * @var string
30
     */
31
    protected $columnField = 'data'; // or 'name'
32
33
    /**
34
     * @var string
35
     */
36
    protected $indexColumn = '*';
37
38
    /**
39
     * @var bool
40
     */
41
    protected $returnCollection = false;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $caseInsensitive = false;
47
48
    /**
49
     * @var QueryBuilder|ORMQueryBuilder
50
     */
51
    protected $queryBuilder;
52
53
    /**
54
     * @var array
55
     */
56
    protected $requestParams;
57
58
    /**
59
     * @return array
60
     */
61
    public function getData()
62
    {
63
        $query = $this->getFilteredQuery();
64
        $columns = &$this->requestParams['columns'];
65
        // Order
66
        if (array_key_exists('order', $this->requestParams)) {
67
            $order = &$this->requestParams['order'];
68
            foreach ($order as $sort) {
69
                $column = &$columns[intval($sort['column'])];
70
                if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
71
                    $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
72
                }
73
                $query->addOrderBy($column[$this->columnField], $sort['dir']);
74
            }
75
        }
76
        // Offset
77
        if (array_key_exists('start', $this->requestParams)) {
78
            $query->setFirstResult(intval($this->requestParams['start']));
79
        }
80
        // Limit
81
        if (array_key_exists('length', $this->requestParams)) {
82
            $length = intval($this->requestParams['length']);
83
            if ($length > 0) {
84
                $query->setMaxResults($length);
85
            }
86
        }
87
        // Fetch
88
        if ($query instanceof ORMQueryBuilder) {
89
            if ($this->returnCollection)
90
                return $query->getQuery()->getResult();
91
            else
92
                return $query->getQuery()->getScalarResult();
93
        } else {
94
            return $query->execute()->fetchAll();
95
        }
96
    }
97
98
    /**
99
     * @return QueryBuilder|ORMQueryBuilder
100
     */
101
    public function getFilteredQuery()
102
    {
103
        $query = clone $this->queryBuilder;
104
        $columns = &$this->requestParams['columns'];
105
        $c = count($columns);
106
        // Search
107
        if (array_key_exists('search', $this->requestParams)) {
108
            if ($value = trim($this->requestParams['search']['value'])) {
109
                $orX = $query->expr()->orX();
110
                for ($i = 0; $i < $c; $i++) {
111
                    $column = &$columns[$i];
112
                    if ($column['searchable'] == 'true') {
113
                        if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
114
                            $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
115
                        }
116
                        if ($this->caseInsensitive) {
117
                            $searchColumn = "lower(" . $column[$this->columnField] . ")";
118
                            $orX->add($query->expr()->like($searchColumn, 'lower(:search)'));
119
                        } else {
120
                            $orX->add($query->expr()->like($column[$this->columnField], ':search'));
121
                        }
122
                    }
123
                }
124
                if ($orX->count() >= 1) {
125
                    $query->andWhere($orX)
126
                        ->setParameter('search', "%{$value}%");
127
                }
128
            }
129
        }
130
        // Filter
131
        for ($i = 0; $i < $c; $i++) {
132
            $column = &$columns[$i];
133
            $andX = $query->expr()->andX();
134
            if (($column['searchable'] == 'true') && ($value = trim($column['search']['value']))) {
135
                if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
136
                    $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
137
                }
138
                $operator = preg_match('~^\[(?<operator>[=!%<>]+)\].*$~', $value, $matches) ? $matches['operator'] : '=';
139
                if ($this->caseInsensitive) {
140
                    $searchColumn = "lower(" . $column[$this->columnField] . ")";
141
                    $filter = "lower(:filter_{$i})";
142
                } else {
143
                    $searchColumn = $column[$this->columnField];
144
                    $filter = ":filter_{$i}";
145
                }
146
                switch ($operator) {
147
                    case '!=': // Not equals; usage: [!=]search_term
148
                        $andX->add($query->expr()->neq($searchColumn, $filter));
149
                        break;
150
                    case '%': // Like; usage: [%]search_term
151
                        $andX->add($query->expr()->like($searchColumn, $filter));
152
                        $value = "%{$value}%";
153
                        break;
154
                    case '<': // Less than; usage: [>]search_term
155
                        $andX->add($query->expr()->lt($searchColumn, $filter));
156
                        break;
157
                    case '>': // Greater than; usage: [<]search_term
158
                        $andX->add($query->expr()->gt($searchColumn, $filter));
159
                        break;
160
                    case '=': // Equals (default); usage: [=]search_term
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
161
                    default:
162
                        $andX->add($query->expr()->eq($searchColumn, $filter));
163
                        break;
164
                }
165
                $query->setParameter("filter_{$i}", $value);
166
            }
167
            if ($andX->count() >= 1) {
168
                $query->andWhere($andX);
169
            }
170
        }
171
        // Done
172
        return $query;
173
    }
174
175
    /**
176
     * @return int
177
     */
178
    public function getRecordsFiltered()
179
    {
180
        $query = $this->getFilteredQuery();
181
        if ($query instanceof ORMQueryBuilder) {
182
            return $query->resetDQLPart('select')
183
                ->select("COUNT({$this->indexColumn})")
184
                ->getQuery()
185
                ->getSingleScalarResult();
186
        } else {
187
            return $query->resetQueryPart('select')
188
                ->select("COUNT({$this->indexColumn})")
189
                ->execute()
190
                ->fetchColumn(0);
191
        }
192
    }
193
194
    /**
195
     * @return int
196
     */
197
    public function getRecordsTotal()
198
    {
199
        $query = clone $this->queryBuilder;
200
        if ($query instanceof ORMQueryBuilder) {
201
            return $query->resetDQLPart('select')
202
                ->select("COUNT({$this->indexColumn})")
203
                ->getQuery()
204
                ->getSingleScalarResult();
205
        } else {
206
            return $query->resetQueryPart('select')
207
                ->select("COUNT({$this->indexColumn})")
208
                ->execute()
209
                ->fetchColumn(0);
210
        }
211
    }
212
213
    /**
214
     * @return array
215
     */
216
    public function getResponse()
217
    {
218
        return array(
219
            'data' => $this->getData(),
220
            'draw' => $this->requestParams['draw'],
221
            'recordsFiltered' => $this->getRecordsFiltered(),
222
            'recordsTotal' => $this->getRecordsTotal(),
223
        );
224
    }
225
226
    /**
227
     * @param string $indexColumn
228
     * @return static
229
     */
230
    public function withIndexColumn($indexColumn)
231
    {
232
        $this->indexColumn = $indexColumn;
233
        return $this;
234
    }
235
236
    /**
237
     * @param array $columnAliases
238
     * @return static
239
     */
240
    public function withColumnAliases($columnAliases)
241
    {
242
        $this->columnAliases = $columnAliases;
243
        return $this;
244
    }
245
246
    /**
247
     * @param array $returnObjectCollection
0 ignored issues
show
Bug introduced by
There is no parameter named $returnObjectCollection. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
248
     * @return static
249
     */
250
    public function withReturnCollection($returnCollection)
251
    {
252
        $this->returnCollection = $returnCollection;
253
        return $this;
254
    }
255
256
    /**
257
     * @param bool $caseInsensitive
258
     * @return static
259
     */
260
    public function withCaseInsensitive($caseInsensitive)
261
    {
262
        $this->caseInsensitive = $caseInsensitive;
263
        return $this;
264
    }
265
266
    /**
267
     * @param string $columnField
268
     * @return static
269
     */
270
    public function withColumnField($columnField)
271
    {
272
        $this->columnField = $columnField;
273
        return $this;
274
    }
275
276
    /**
277
     * @param QueryBuilder|ORMQueryBuilder $queryBuilder
278
     * @return static
279
     */
280
    public function withQueryBuilder($queryBuilder)
281
    {
282
        $this->queryBuilder = $queryBuilder;
283
        return $this;
284
    }
285
286
    /**
287
     * @param array $requestParams
288
     * @return static
289
     */
290
    public function withRequestParams($requestParams)
291
    {
292
        $this->requestParams = $requestParams;
293
        return $this;
294
    }
295
}
296