Test Failed
Pull Request — dev (#118)
by Def
09:16 queued 06:49
created

DQLQueryBuilder::defaultExpressionBuilders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\Expression\JsonExpression;
9
use Yiisoft\Db\Expression\ExpressionBuilder;
10
use Yiisoft\Db\Mysql\Builder\JsonExpressionBuilder;
11
use Yiisoft\Db\Query\DQLQueryBuilder as AbstractDQLQueryBuilder;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\DQLQueryBuilder 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...
12
13
use function array_merge;
14
use function ctype_digit;
15
16
final class DQLQueryBuilder extends AbstractDQLQueryBuilder
17
{
18
    public function buildLimit(Expression|int|null $limit, Expression|int|null $offset): string
19
    {
20
        $sql = '';
21
22
        if ($this->hasLimit($limit)) {
23
            $sql = 'LIMIT ' . (string) $limit;
24
25
            if ($this->hasOffset($offset)) {
26
                $sql .= ' OFFSET ' . (string) $offset;
27
            }
28
        } elseif ($this->hasOffset($offset)) {
29
            /**
30
             * limit is not optional in MySQL.
31
             *
32
             * http://stackoverflow.com/a/271650/1106908
33
             * http://dev.mysql.com/doc/refman/5.0/en/select.html#idm47619502796240
34
             */
35
            $sql = "LIMIT $offset, 18446744073709551615"; // 2^64-1
36
        }
37
38
        return $sql;
39
    }
40
41
    /**
42
     * Checks to see if the given limit is effective.
43
     *
44
     * @param mixed $limit the given limit.
45
     *
46
     * @return bool whether the limit is effective.
47
     */
48
    protected function hasLimit(mixed $limit): bool
49
    {
50
        /** In MySQL limit argument must be non-negative integer constant */
51
        return ctype_digit((string) $limit);
52
    }
53
54
    /**
55
     * Checks to see if the given offset is effective.
56
     *
57
     * @param mixed $offset the given offset.
58
     *
59
     * @return bool whether the offset is effective.
60
     */
61
    protected function hasOffset(mixed $offset): bool
62
    {
63
        /** In MySQL offset argument must be non-negative integer constant */
64
        $offset = (string) $offset;
65
        return ctype_digit($offset) && $offset !== '0';
66
    }
67
68
    /**
69
     * Contains array of default expression builders. Extend this method and override it, if you want to change default
70
     * expression builders for this query builder.
71
     *
72
     * See {@see ExpressionBuilder} docs for details.
73
     */
74
    protected function defaultExpressionBuilders(): array
75
    {
76
        return array_merge(
77
            parent::defaultExpressionBuilders(),
78
            [
79
                JsonExpression::class => JsonExpressionBuilder::class,
80
            ]
81
        );
82
    }
83
}
84