Passed
Push — main ( 87c1e5...f29b76 )
by William
01:42
created

Query   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 44
c 11
b 0
f 0
dl 0
loc 117
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 4 1
A selectRaw() 0 5 1
A get() 0 12 2
A order() 0 5 1
A groupBy() 0 5 1
A having() 0 5 1
A first() 0 16 3
A offset() 0 4 1
A count() 0 12 2
A limit() 0 4 1
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
class Query extends Base
6
{
7
8
    /**
9
     * @param string $columns
10
     * @return $this
11
     */
12
    public function selectRaw(string $columns = "*", array $params = []): self
13
    {
14
        $this->setBindings($params, 'select');
15
        $this->columns = array_merge($this->columns, explode(',', $columns));
16
        return $this;
17
    }
18
19
    public function select(array $columns = ['*']): self
20
    {
21
        $this->columns = array_merge($this->columns, $columns);
22
        return $this;
23
    }
24
25
    /**
26
     * @param string $columnOrder
27
     */
28
    public function order(string $columnOrder): self
29
    {
30
        $this->order = "ORDER BY ?";
31
        $this->setBindings([$columnOrder], 'order');
32
        return $this;
33
    }
34
35
    /**
36
     * @param array $group
37
     * @return DB
0 ignored issues
show
Bug introduced by
The type Willry\QueryBuilder\DB 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...
38
     */
39
    public function groupBy(string $group): self
40
    {
41
42
        $this->groupBy = "GROUP BY {$group}";
43
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Willry\QueryBuilder\Query which is incompatible with the documented return type Willry\QueryBuilder\DB.
Loading history...
44
    }
45
46
    /**
47
     * @param string $having
48
     * @return DB
49
     */
50
    public function having(string $having, array $params = []): self
51
    {
52
        $this->having = "HAVING {$having}";
53
        $this->setBindings($params, 'having');
54
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Willry\QueryBuilder\Query which is incompatible with the documented return type Willry\QueryBuilder\DB.
Loading history...
55
    }
56
57
    /**
58
     * @param int $limit
59
     */
60
    public function limit(int $limit): static
61
    {
62
        $this->limit = "LIMIT $limit";
63
        return $this;
64
    }
65
66
    /**
67
     * @param int $offset
68
     * @return DB
69
     */
70
    public function offset(int $offset): static
71
    {
72
        $this->offset = "OFFSET $offset";
73
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Willry\QueryBuilder\Query which is incompatible with the documented return type Willry\QueryBuilder\DB.
Loading history...
74
    }
75
76
    public function get(): ?array
77
    {
78
        $this->mountQuery();
79
80
        try {
81
            $stmt = $this->db->prepare($this->query);
82
            QueryHelpers::bind($stmt, $this->flatBindings());
83
            $stmt->execute();
84
85
            return $stmt->fetchAll(\PDO::FETCH_CLASS);
86
        } catch (\PDOException $exception) {
87
            $this->handleError($exception);
88
        }
89
    }
90
91
    public function first(): ?\stdClass
92
    {
93
        $this->mountQuery();
94
95
        try {
96
            $stmt = $this->db->prepare($this->query);
97
            QueryHelpers::bind($stmt, $this->flatBindings());
98
            $stmt->execute();
99
100
            if (!$stmt->rowCount()) {
101
                return null;
102
            }
103
104
            return $stmt->fetchObject();
105
        } catch (\PDOException $exception) {
106
            $this->handleError($exception);
107
        }
108
    }
109
110
    public function count(): ?int
111
    {
112
        $this->mountQuery();
113
114
        try {
115
            $stmt = $this->db->prepare($this->query);
116
            QueryHelpers::bind($stmt, $this->flatBindings());
117
            $stmt->execute();
118
119
            return $stmt->rowCount();
120
        } catch (\PDOException $exception) {
121
            $this->handleError($exception);
122
        }
123
    }
124
}
125