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
04:32
created

Builder::withReturnCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 QueryBuilder|ORMQueryBuilder
45
     */
46
    protected $queryBuilder;
47
48
    /**
49
     * @var array
50
     */
51
    protected $requestParams;
52
53
    /**
54
     * @return array
55
     */
56
    public function getData()
57
    {
58
        $query = $this->getFilteredQuery();
59
        $columns = &$this->requestParams['columns'];
60
        // Order
61
        if (array_key_exists('order', $this->requestParams)) {
62
            $order = &$this->requestParams['order'];
63
            foreach ($order as $sort) {
64
                $column = &$columns[intval($sort['column'])];
65
                if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
66
                    $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
67
                }
68
                $query->addOrderBy($column[$this->columnField], $sort['dir']);
69
            }
70
        }
71
        // Offset
72
        if (array_key_exists('start', $this->requestParams)) {
73
            $query->setFirstResult(intval($this->requestParams['start']));
74
        }
75
        // Limit
76
        if (array_key_exists('length', $this->requestParams)) {
77
            $length = intval($this->requestParams['length']);
78
            if ($length > 0) {
79
                $query->setMaxResults($length);
80
            }
81
        }
82
        // Fetch
83
        if ($query instanceof ORMQueryBuilder) {
84
            if ($this->returnCollection)
85
                return $query->getQuery()->getResult();
86
            else
87
                return $query->getQuery()->getScalarResult();
88
        } else {
89
            return $query->execute()->fetchAll();
90
        }
91
    }
92
93
    /**
94
     * @return QueryBuilder|ORMQueryBuilder
95
     */
96
    public function getFilteredQuery()
97
    {
98
        $query = clone $this->queryBuilder;
99
        $columns = &$this->requestParams['columns'];
100
        $c = count($columns);
101
        // Search
102
        if (array_key_exists('search', $this->requestParams)) {
103
            if ($value = trim($this->requestParams['search']['value'])) {
104
                $orX = $query->expr()->orX();
105
                for ($i = 0; $i < $c; $i++) {
106
                    $column = &$columns[$i];
107
                    if ($column['searchable'] == 'true') {
108
                        if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
109
                            $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
110
                        }
111
                        $orX->add($query->expr()->like($column[$this->columnField], ':search'));
112
                    }
113
                }
114
                if ($orX->count() >= 1) {
115
                    $query->andWhere($orX)
116
                        ->setParameter('search', "%{$value}%");
117
                }
118
            }
119
        }
120
        // Filter
121
        for ($i = 0; $i < $c; $i++) {
122
            $column = &$columns[$i];
123
            $andX = $query->expr()->andX();
124
            if (($column['searchable'] == 'true') && ($value = trim($column['search']['value']))) {
125
                if (array_key_exists($column[$this->columnField], $this->columnAliases)) {
126
                    $column[$this->columnField] = $this->columnAliases[$column[$this->columnField]];
127
                }
128
                $operator = preg_match('~^\[(?<operator>[=!%<>]+)\].*$~', $value, $matches) ? $matches['operator'] : '=';
129
                switch ($operator) {
130
                    case '!=': // Not equals; usage: [!=]search_term
131
                        $andX->add($query->expr()->neq($column[$this->columnField], ":filter_{$i}"));
132
                        break;
133
                    case '%': // Like; usage: [%]search_term
134
                        $andX->add($query->expr()->like($column[$this->columnField], ":filter_{$i}"));
135
                        $value = "%{$value}%";
136
                        break;
137
                    case '<': // Less than; usage: [>]search_term
138
                        $andX->add($query->expr()->lt($column[$this->columnField], ":filter_{$i}"));
139
                        break;
140
                    case '>': // Greater than; usage: [<]search_term
141
                        $andX->add($query->expr()->gt($column[$this->columnField], ":filter_{$i}"));
142
                        break;
143
                    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...
144
                    default:
145
                        $andX->add($query->expr()->eq($column[$this->columnField], ":filter_{$i}"));
146
                        break;
147
                }
148
                $query->setParameter("filter_{$i}", $value);
149
            }
150
            if ($andX->count() >= 1) {
151
                $query->andWhere($andX);
152
            }
153
        }
154
        // Done
155
        return $query;
156
    }
157
158
    /**
159
     * @return int
160
     */
161
    public function getRecordsFiltered()
162
    {
163
        $query = $this->getFilteredQuery();
164
        if ($query instanceof ORMQueryBuilder) {
165
            return $query->resetDQLPart('select')
166
                ->select("COUNT({$this->indexColumn})")
167
                ->getQuery()
168
                ->getSingleScalarResult();
169
        } else {
170
            return $query->resetQueryPart('select')
171
                ->select("COUNT({$this->indexColumn})")
172
                ->execute()
173
                ->fetchColumn(0);
174
        }
175
    }
176
177
    /**
178
     * @return int
179
     */
180
    public function getRecordsTotal()
181
    {
182
        $query = clone $this->queryBuilder;
183
        if ($query instanceof ORMQueryBuilder) {
184
            return $query->resetDQLPart('select')
185
                ->select("COUNT({$this->indexColumn})")
186
                ->getQuery()
187
                ->getSingleScalarResult();
188
        } else {
189
            return $query->resetQueryPart('select')
190
                ->select("COUNT({$this->indexColumn})")
191
                ->execute()
192
                ->fetchColumn(0);
193
        }
194
    }
195
196
    /**
197
     * @return array
198
     */
199
    public function getResponse()
200
    {
201
        return array(
202
            'data' => $this->getData(),
203
            'draw' => $this->requestParams['draw'],
204
            'recordsFiltered' => $this->getRecordsFiltered(),
205
            'recordsTotal' => $this->getRecordsTotal(),
206
        );
207
    }
208
209
    /**
210
     * @param string $indexColumn
211
     * @return static
212
     */
213
    public function withIndexColumn($indexColumn)
214
    {
215
        $this->indexColumn = $indexColumn;
216
        return $this;
217
    }
218
219
    /**
220
     * @param array $columnAliases
221
     * @return static
222
     */
223
    public function withColumnAliases($columnAliases)
224
    {
225
        $this->columnAliases = $columnAliases;
226
        return $this;
227
    }
228
229
    /**
230
     * @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...
231
     * @return static
232
     */
233
    public function withReturnCollection($returnCollection)
234
    {
235
        $this->returnCollection = $returnCollection;
236
        return $this;
237
    }
238
239
    /**
240
     * @param string $columnField
241
     * @return static
242
     */
243
    public function withColumnField($columnField)
244
    {
245
        $this->columnField = $columnField;
246
        return $this;
247
    }
248
249
    /**
250
     * @param QueryBuilder|ORMQueryBuilder $queryBuilder
251
     * @return static
252
     */
253
    public function withQueryBuilder($queryBuilder)
254
    {
255
        $this->queryBuilder = $queryBuilder;
256
        return $this;
257
    }
258
259
    /**
260
     * @param array $requestParams
261
     * @return static
262
     */
263
    public function withRequestParams($requestParams)
264
    {
265
        $this->requestParams = $requestParams;
266
        return $this;
267
    }
268
}
269