Select::limit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Divergence\IO\Database\Query;
4
5
class Select extends AbstractQuery
6
{
7
    public ?string $where;
8
    public ?string $having;
9
    public ?string $limit;
10
    public ?string $order;
11
    public string $expression = '*';
12
    public bool $calcFoundRows = false;
13
14 17
    public function expression(string $expression): Select
15
    {
16 17
        $this->expression = $expression;
17 17
        return $this;
18
    }
19
20 84
    public function where(string $where): Select
21
    {
22 84
        $this->where = $where;
23 84
        return $this;
24
    }
25
26 1
    public function having(string $having): Select
27
    {
28 1
        $this->having = $having;
29 1
        return $this;
30
    }
31
32 83
    public function limit(string $limit): Select
33
    {
34 83
        if (!empty($limit)) {
35 83
            $this->limit = $limit;
36
        }
37 83
        return $this;
38
    }
39
40 84
    public function order(string $order): Select
41
    {
42 84
        if (!empty($order)) {
43 14
            $this->order = $order;
44
        }
45 84
        return $this;
46
    }
47
48 13
    public function calcFoundRows(): Select
49
    {
50 13
        $this->calcFoundRows = true;
51 13
        return $this;
52
    }
53
54 94
    public function __toString(): string
55
    {
56 94
        $expression = ($this->calcFoundRows ? 'SQL_CALC_FOUND_ROWS ' : '') . $this->expression;
57
58 94
        if (isset($this->tableAlias)) {
59 17
            $from = sprintf('`%s` AS `%s`', $this->table, $this->tableAlias);
60
        } else {
61 87
            $from = sprintf('`%s`', $this->table);
62
        }
63
64 94
        $limit = isset($this->limit) ? ' LIMIT '.$this->limit : '';
65 94
        $where = isset($this->where) ? ' WHERE ('.$this->where.')' : '';
66 94
        $having = isset($this->having) ? ' HAVING ('.$this->having.')' : '';
67 94
        $order = isset($this->order) ? ' ORDER BY '.$this->order : '';
68
69 94
        return sprintf('SELECT %s FROM %s %s %s %s %s', $expression, $from, $where, $having, $order, $limit);
70
    }
71
}
72