Test Failed
Pull Request — master (#21)
by Vincent
06:04
created

Query   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 608
Duplicated Lines 0 %

Test Coverage

Coverage 85.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 68
eloc 176
c 1
b 0
f 0
dl 0
loc 608
ccs 152
cts 177
cp 0.8588
rs 2.96

39 Methods

Rating   Name   Duplication   Size   Complexity  
A min() 0 3 1
A execute() 0 9 2
A insert() 0 9 2
A executeUpdate() 0 11 2
A values() 0 8 1
A raw() 0 3 1
A replace() 0 5 1
A update() 0 10 2
A max() 0 3 1
A paginationCount() 0 17 1
A count() 0 3 1
A distinct() 0 7 1
A getBindings() 0 3 1
A quoteIdentifier() 0 3 1
A cacheKey() 0 3 1
A __construct() 0 20 2
A avg() 0 3 1
A quote() 0 3 1
A getPaginationColumns() 0 21 5
A ignore() 0 5 1
A aggregate() 0 14 2
A delete() 0 3 1
A sum() 0 3 1
A orHaving() 0 5 1
A orHavingNull() 0 3 1
A group() 0 7 2
A havingNotNull() 0 5 1
A orHavingNotNull() 0 3 1
A from() 0 17 3
A fromAlias() 0 12 1
A orHavingRaw() 0 3 1
B addCommand() 0 41 10
A __toString() 0 3 1
A toSql() 0 3 1
A addGroup() 0 7 2
B toRawSql() 0 28 7
A havingNull() 0 5 1
A having() 0 5 1
A havingRaw() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bdf\Prime\Query;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor;
8
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface;
9
use Bdf\Prime\Query\Contract\Paginable;
10
use Bdf\Prime\Query\Contract\ReadOperation;
11
use Bdf\Prime\Query\Contract\WriteOperation;
12
use Bdf\Prime\Query\Expression\Raw;
13
use Bdf\Prime\Query\Extension\EntityJoinTrait;
14
use Bdf\Prime\Query\Extension\LimitableTrait;
15
use Bdf\Prime\Query\Extension\LockableTrait;
16
use Bdf\Prime\Query\Extension\OrderableTrait;
17
use Bdf\Prime\Query\Extension\PaginableTrait;
18
use Bdf\Prime\Query\Extension\SimpleJoinTrait;
19
use Doctrine\DBAL\Query\Expression\CompositeExpression;
20
21
/**
22
 * Sql Query
23
 * 
24
 * @package Bdf\Prime\Query
0 ignored issues
show
Coding Style Documentation introduced by
@package tag is not allowed in class comment
Loading history...
25
 * 
26
 * @todo comment reset un statement (ex ecraser les orders). Prendre en compte le reset du compiler
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style Documentation introduced by
@todo tag is not allowed in class comment
Loading history...
27
 */
28
class Query extends AbstractQuery implements SqlQueryInterface, Paginable
29
{
30
    use EntityJoinTrait;
31
    use PaginableTrait;
32
    use LimitableTrait;
33
    use OrderableTrait;
34
    use SimpleJoinTrait;
35
    use LockableTrait;
36
37
    /**
38
     * Initializes a new <tt>Query</tt>.
39
     *
40
     * @param ConnectionInterface $connection The DBAL Connection.
41
     * @param PreprocessorInterface|null $preprocessor
42
     */
43 725
    public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null)
44
    {
45 725
        parent::__construct($connection, $preprocessor ?: new DefaultPreprocessor());
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
46
47 725
        $this->statements = [
48
            'ignore'     => null,
49
            'replace'    => null,
50
            'values'     => [],
51
            'columns'    => [],
52
            'distinct'   => null,
53
            'tables'     => [],
54
            'joins'      => [],
55
            'where'      => [],
56
            'groups'     => [],
57
            'having'     => [],
58
            'orders'     => [],
59
            'limit'      => null,
60
            'offset'     => null,
61
            'aggregate'  => null,
62
            'lock'       => null,
63
        ];
64 725
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 582
    public function getBindings()
70
    {
71 582
        return $this->compiler->getBindings($this);
72
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sql should have a doc-comment as per coding-style.
Loading history...
75
     * {@inheritdoc}
76
     */
77 1
    public function raw($sql)
78
    {
79 1
        return new Raw($sql);
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
83
     * {@inheritdoc}
84
     */
85
    public function quote($value, $type = null)
86
    {
87
        return $this->compiler->quote($value, $type);
0 ignored issues
show
Bug introduced by
The method quote() does not exist on Bdf\Prime\Query\Compiler\CompilerInterface. It seems like you code against a sub-type of Bdf\Prime\Query\Compiler\CompilerInterface such as Bdf\Prime\Query\Compiler\SqlCompiler. ( Ignorable by Annotation )

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

87
        return $this->compiler->/** @scrutinizer ignore-call */ quote($value, $type);
Loading history...
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
91
     * {@inheritdoc}
92
     *
93
     * @todo Utile ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
94
     */
95
    public function quoteIdentifier($column)
96
    {
97
        return $this->compiler->quoteIdentifier($this, $column);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
104 26
    public function delete()
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
105
    {
106 26
        return $this->executeUpdate(self::TYPE_DELETE);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
113 21
    public function update(array $data = [], array $types = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
114
    {
115 21
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116 11
            $this->statements['values'] = [
117 11
                'data'   => $data,
118 11
                'types'  => $types,
119
            ];
120
        }
121
        
122 21
        return $this->executeUpdate(self::TYPE_UPDATE);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
129 442
    public function insert(array $data = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
130
    {
131 442
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132 442
            $this->statements['values'] = [
133 442
                'data'   => $data,
134
            ];
135
        }
136
        
137 442
        return $this->executeUpdate(self::TYPE_INSERT);
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
141
     * {@inheritdoc}
142
     */
143 1
    public function ignore($flag = true)
144
    {
145 1
        $this->statements['ignore'] = (bool)$flag;
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
146
        
147 1
        return $this;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
154 2
    public function replace(array $values = [])
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
155
    {
156 2
        $this->statements['replace'] = true;
157
158 2
        return $this->insert($values);
159
    }
160
161
    /**
162
     * Set values to insert or update
163
     *
164
     * @todo Remonter sur une interface ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
165
     *
166
     * <code>
167
     * // Perform a INSERT INTO ... SELECT ... query
168
     * $query
169
     *     ->from('users_bck')
170
     *     ->values($connection->builder()->from('users'))
171
     *     ->insert()
172
     * ;
173
     *
174
     * // Perform a REPLACE INTO ... SELECT ... with column mapping
175
     * $query
176
     *     ->from('users_bck')
177
     *     ->values(
178
     *         $connection->builder()
179
     *             ->from('users')
180
     *             ->select([
181
     *                 'backup_name' => 'name', // Map "name" column from select table to "backup_name" column to insert table
182
     *                 'id'                     // Use "id" column without mapping other than ORM data mapping
183
     *             ])
184
     *     )
185
     *     ->replace() // Replace can also be used
186
     * ;
187
     *
188
     * // Simple UPDATE query
189
     * $query->values(['foo' => 'bar'])->update();
190
     * </code>
191
     *
192
     * @param QueryInterface|array $data The values, in form [column] => [value], or the SELECT query
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
193
     * @param array $types The binding types Do not works with INSERT INTO SELECT query
194
     *
195
     * @return $this
196
     */
197 17
    public function values($data = [], array $types = [])
198
    {
199 17
        $this->statements['values'] = [
200 17
            'data' => $data,
201 17
            'type' => $types
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: $types
Loading history...
202
        ];
203
204 17
        return $this;
205
    }
206
207
    /**
208
     * Executes this query as an update query
209
     *
210
     * @param string $type The query type
211
     *
212
     * @return int The number of updated rows
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
213
     *
214
     * @throws PrimeException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
215
     */
216 455
    protected function executeUpdate($type)
217
    {
218 455
        $this->setType($type);
219
220 455
        $nb = $this->connection->execute($this)->count();
221
222 455
        if ($nb > 0) {
223 446
            $this->clearCacheOnWrite();
224
        }
225
226 455
        return $nb;
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     *
232
     * @todo Return statement instead of array ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
233
     */
234
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
235 501
    public function execute($columns = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
236
    {
237 501
        if (!empty($columns)) {
238 52
            $this->select($columns);
239
        }
240
241 501
        $this->setType(self::TYPE_SELECT);
242
243 501
        return $this->executeCached();
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249 10
    protected function cacheKey()
250
    {
251 10
        return sha1($this->toSql().'-'.serialize($this->getBindings()));
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
258 13
    public function paginationCount($columns = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
259
    {
260 13
        $statements = $this->statements;
261
262 13
        $this->compilerState->invalidate(['columns', 'orders']);
263
264 13
        $this->statements['orders'] = [];
265 13
        $this->statements['limit'] = null;
266 13
        $this->statements['offset'] = null;
267 13
        $this->statements['aggregate'] = ['pagination', $this->getPaginationColumns($columns)];
268
269 13
        $count = (int)$this->execute()[0]['aggregate'];
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
270
271 13
        $this->compilerState->invalidate(['columns', 'orders']);
272 13
        $this->statements = $statements;
273
274 13
        return $count;
275
    }
276
277
    /**
278
     * Get the column to count for pagination
279
     * @todo Voir pour count sur PK quand une entité est liée ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
280
     *
281
     * @param array|string|null $column
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
282
     *
283
     * @return string
284
     */
285 13
    protected function getPaginationColumns($column)
286
    {
287 13
        if (!empty($column)) {
288
            return $column;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $column also could return the type array which is incompatible with the documented return type string.
Loading history...
289
        }
290
291
        // If distinct is on and no column are given, we use the current column
292 13
        if ($this->statements['distinct'] && !empty($this->statements['columns'])) {
293 3
            return $this->statements['columns'][0]['column'];
294
        }
295
296
        // If group by we use the columns of the group by
297 11
        if ($this->statements['groups']) {
298 2
            $this->statements['distinct'] = true;
299 2
            $column = $this->statements['groups'][0];
300 2
            $this->statements['groups'] = [];
301
302 2
            return $column;
303
        }
304
305 10
        return '*';
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
312 345
    public function count($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
313
    {
314 345
        return (int)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
321 2
    public function avg($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
322
    {
323 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
330 2
    public function min($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
331
    {
332 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
339 3
    public function max($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
340
    {
341 3
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
348 2
    public function sum($column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
349
    {
350 2
        return (float)$this->aggregate(__FUNCTION__, $column);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356
    #[ReadOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
357 354
    public function aggregate($function, $column = null)
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
358
    {
359 354
        $statements = $this->statements;
360
361 354
        $this->compilerState->invalidate('columns');
362
363 354
        $this->statements['aggregate'] = [$function, $column ?: '*'];
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
364
365 354
        $aggregate = $this->execute()[0]['aggregate'];
366
367 354
        $this->compilerState->invalidate('columns');
368 354
        $this->statements = $statements;
369
370 354
        return $aggregate;
371
    }
372
373
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
374
     * {@inheritdoc}
375
     */
376 18
    public function distinct($flag = true)
377
    {
378 18
        $this->compilerState->invalidate('columns');
379
        
380 18
        $this->statements['distinct'] = (bool)$flag;
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
381
        
382 18
        return $this;
383
    }
384
385
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $from should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
386
     * {@inheritdoc}
387
     */
388 694
    public function from($from, $alias = null)
389
    {
390 694
        $this->compilerState->invalidate('from');
391
392 694
        $table = [
393 694
            'table' => $from,
394 694
            'alias' => $alias,
395
        ];
396
        $key = $alias ?: $from;
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
397 694
398
        if (is_string($key)) {
399
            $this->statements['tables'][$key] = $table;
400
        } else {
401
            $this->statements['tables'][] = $table;
402
        }
403 5
404
        return $this;
405 5
    }
406
407 5
    /**
408
     * Change a FROM alias for a table (or previously defined alias)
409 5
     *
410
     * Usage:
411
     * <code>
412
     * // Change alias of the current table : FROM my_table as my_alias
413
     * $query->from('my_table')->fromAlias('my_alias');
414
     *
415
     * // Change alias of the foo table : FROM foo as my_alias, bar
416
     * $query->from('foo')->from('bar')->fromAlias('my_alias', 'foo');
417
     *
418
     * // Redefine alias of foo : FROM foo as my_alias, bar
419
     * $query->from('foo', 'f')->from('bar')->fromAlias('my_alias', 'f');
420
     * </code>
421
     *
422
     * @param string $alias The new alias name
423
     * @param string|null $table The last alias / table name. If null, will define the last table
424
     *
425
     * @return $this
426
     */
427 2
    public function fromAlias(string $alias, ?string $table = null)
428
    {
429 2
        $this->compilerState->invalidate('from');
430
431 2
        $table = $table ?? key($this->statements['tables']);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
432
433
        $this->statements['tables'][$alias] = $this->statements['tables'][$table];
434
        $this->statements['tables'][$alias]['alias'] = $alias;
435
436
        unset($this->statements['tables'][$table]);
437 3
438
        return $this;
439 3
    }
440
441 3
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
442
     * {@inheritdoc}
443
     */
444
    public function group($column)
445
    {
446
        $this->compilerState->invalidate('groups');
447 2
        
448
        $this->statements['groups'] = is_array($column) ? $column : func_get_args();
449 2
        
450
        return $this;
451 2
    }
452
453
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
454
     * {@inheritdoc}
455
     */
456
    public function addGroup($column)
457 2
    {
458
        $this->compilerState->invalidate('groups');
459 2
        
460
        $this->statements['groups'] = array_merge($this->statements['groups'], is_array($column) ? $column : func_get_args());
461 2
        
462
        return $this;
463
    }
464
465
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $operator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
466
     * {@inheritdoc}
467 1
     */
468
    public function having($column, $operator = null, $value = null)
469 1
    {
470
        $this->compilerState->invalidate('having');
471
472
        return $this->buildClause('having', $column, $operator, $value);
473
    }
474
475 1
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $operator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
476
     * {@inheritdoc}
477 1
     */
478
    public function orHaving($column, $operator = null, $value = null)
479
    {
480
        $this->compilerState->invalidate('having');
481
482
        return $this->buildClause('having', $column, $operator, $value, CompositeExpression::TYPE_OR);
483 2
    }
484
485 2
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
486
     * {@inheritdoc}
487 2
     */
488
    public function havingNull($column, $type = CompositeExpression::TYPE_AND)
489
    {
490
        $this->compilerState->invalidate('having');
491
492
        return $this->buildClause('having', $column, '=', null, $type);
493 1
    }
494
495 1
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
496
     * {@inheritdoc}
497
     */
498
    public function havingNotNull($column, $type = CompositeExpression::TYPE_AND)
499
    {
500
        $this->compilerState->invalidate('having');
501 6
502
        return $this->buildClause('having', $column, '!=', null, $type);
503
    }
504 6
505 4
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
506
     * {@inheritdoc}
507
     */
508 4
    public function orHavingNull($column)
509
    {
510 4
        return $this->havingNull($column, CompositeExpression::TYPE_OR);
511
    }
512 4
513 1
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
514 1
     * {@inheritdoc}
515
     */
516
    public function orHavingNotNull($column)
517
    {
518 1
        return $this->havingNotNull($column, CompositeExpression::TYPE_OR);
519
    }
520 3
521
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $raw should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $type should have a doc-comment as per coding-style.
Loading history...
522
     * {@inheritdoc}
523
     */
524 3
    public function havingRaw($raw, $type = CompositeExpression::TYPE_AND)
525 2
    {
526 2
        $this->compilerState->invalidate('having');
527
528 1
        return $this->buildRaw('having', $raw, $type);
529 1
    }
530 1
531
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $raw should have a doc-comment as per coding-style.
Loading history...
532
     * {@inheritdoc}
533
     */
534
    public function orHavingRaw($raw)
535
    {
536
        return $this->havingRaw($raw, CompositeExpression::TYPE_OR);
537
    }
538
539
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $command should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
540
     * {@inheritdoc}
541 6
     */
542
    public function addCommand($command, $value)
543
    {
544
        switch ($command) {
545
            case ':limit':
546
                if (is_array($value)) {
547 128
                    $this->limit($value[0], $value[1]);
548
                } else {
549 128
                    $this->limit($value);
550
                }
551
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
552
            
553
            case ':limitPage':
554
                if (is_array($value)) {
555
                    $this->limitPage($value[0], $value[1]);
556 15
                } else {
557
                    $this->limitPage($value);
558 15
                }
559 15
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
560 15
            
561
            case ':offset':
562
                $this->offset($value);
563 15
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
564 15
            
565
            case ':order':
566
                $this->order($value);
567 15
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
568
            
569
            case ':distinct':
570 15
                $this->distinct($value);
571
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
572 15
                
573
            case ':group':
574 15
                $this->group($value);
575
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
576 15
            
577 8
            case ':having':
578
                $this->having($value);
579 15
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
580
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end switch"
Loading history...
581
        
582
        return $this;
583 15
    }
584
585
    /**
586
     * {@inheritdoc}
587
     */
588
    public function toSql()
589
    {
590
        return $this->compile();
591
    }
592
593
    /**
594
     * {@inheritdoc}
595
     * @todo A reprendre: utiliser les types des bindings
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
596
     */
597
    public function toRawSql()
598
    {
599
        $keys   = [];
600
        $sql    = $this->toSql();
601
        $values = $this->compiler->getBindings($this);
602
603
        # build a regular expression for each parameter
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
604
        foreach ($values as $key => $value) {
605
            if (is_string($key)) {
606
                $keys[] = '/:' . $key . '/';
607
            } else {
608
                $keys[] = '/[?]/';
609
            }
610
            
611
            if (is_array($value)) {
612
                $values[$key] = implode(',', $this->connection->quote($value));
0 ignored issues
show
Bug introduced by
The method quote() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

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

612
                $values[$key] = implode(',', $this->connection->/** @scrutinizer ignore-call */ quote($value));
Loading history...
613
            } elseif (is_null($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
614
                $values[$key] = 'NULL';
615
            } elseif ($value instanceof \DateTimeInterface) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
616
                $values[$key] = $value->format($this->connection->platform()->grammar()->getDateTimeFormatString());
617
            } elseif (is_string($value)) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
618
                $values[$key] = $this->connection->quote($value);
619
            } else {
620
                $values[$key] = $value;
621
            }
622
        }
623
        
624
        return preg_replace($keys, $values, $sql, 1);
625
    }
626
    
627
    /**
628
     * Gets a string representation of this Query which corresponds to
629
     * the final SQL query being constructed.
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
630
     *
631
     * @return string The string representation of this Query.
632
     */
633
    public function __toString()
634
    {
635
        return $this->toSql();
636
    }
637
}
638