Completed
Push — master ( 95300d...353cbe )
by Michael
05:45 queued 02:48
created

Query   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 203
Duplicated Lines 13.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.83%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 21
c 4
b 0
f 3
lcom 1
cbo 2
dl 27
loc 203
ccs 69
cts 72
cp 0.9583
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __toString() 0 4 1
A select() 0 6 1
A insert() 0 6 1
A update() 0 6 1
A delete() 0 6 1
A where() 0 13 2
A whereIn() 14 14 2
A groupBy() 0 6 1
A orderBy() 0 6 1
A limit() 0 7 1
A offset() 0 7 1
B execute() 0 22 5
A replaceValuesWithBindings() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\Query;
12
13
/**
14
 * The query class.
15
 *
16
 * @since 1.0.0
17
 */
18
class Query implements QueryInterface
19
{
20
	/* @var \PDO The PDO. */
21
	private $pdo;
22
23
	/* @var array The bindings. */
24
	private $bindings;
25
26
	/* @var QueryBuilder The query builder. */
27
	private $queryBuilder;
28
29
	/**
30
	 * Construct a query object with the given pdo and table.
31
	 *
32
	 * @param \PDO $pdo
33
	 * @param string $table
34
	 */
35 15
	public function __construct(\PDO $pdo, $table)
36
	{
37 15
		$this->pdo = $pdo;
38 15
		$this->bindings = [];
39 15
		$this->queryBuilder = new QueryBuilder($table);
40 15
	}
41
42
	/**
43
	 * Returns a string representation of the query object.
44
	 *
45
	 * @return string a string representation of the query object.
46
	 */
47 15
	public function __toString()
48
	{
49 15
		return $this->queryBuilder->__toString();
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 11
	public function select($columns = ['*'])
56
	{
57 11
		$this->queryBuilder->select($columns);
58
59 11
		return $this;
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65 1
	public function insert(array $values)
66
	{
67 1
		$this->queryBuilder->insert($this->replaceValuesWithBindings($values));
68
69 1
		return $this;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75 1
	public function update(array $values)
76
	{
77 1
		$this->queryBuilder->update($this->replaceValuesWithBindings($values));
78
79 1
		return $this;
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85 1
	public function delete()
86
	{
87 1
		$this->queryBuilder->delete();
88
89 1
		return $this;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95 5
	public function where($column, $operator, $value)
96
	{
97 5
		$binding = sprintf(':%s', $column);
98
99 5
		if ($operator == 'IN') {
100 1
			$this->whereIn($column, $value);
101 1
		} else {
102 4
			$this->bindings[$binding] = $value;
103 4
			$this->queryBuilder->where($column, $operator, $binding);
104
		}
105
106 5
		return $this;
107
	}
108
109
	/**
110
	 * Set an additional where in condition.
111
	 *
112
	 * @param string $column
113
	 * @param mixed $values
114
	 * @return $this
115
	 */
116 1 View Code Duplication
	private function whereIn($column, $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
	{
118 1
		$bindings = [];
119
120 1
		foreach ($values as $key => $value) {
121 1
			$bindings[] = $binding = sprintf(':%s%s', $column, $key);
122
123 1
			$this->bindings[$binding] = $value;
124 1
		}
125
126 1
		$this->queryBuilder->where($column, 'IN', $bindings);
127
128 1
		return $this;
129
	}
130
131
	/**
132
	 * {@inheritdoc}
133
	 */
134 1
	public function groupBy($column)
135
	{
136 1
		$this->queryBuilder->groupBy($column);
137
138 1
		return $this;
139
	}
140
141
	/**
142
	 * {@inheritdoc}
143
	 */
144 1
	public function orderBy($column, $order = null)
145
	{
146 1
		$this->queryBuilder->orderBy($column, $order);
147
148 1
		return $this;
149
	}
150
151
	/**
152
	 * {@inheritdoc}
153
	 */
154 2
	public function limit($limit)
155
	{
156 2
		$this->bindings[':limit'] = (int) $limit;
157 2
		$this->queryBuilder->limit(':limit');
158
159 2
		return $this;
160
	}
161
162
	/**
163
	 * {@inheritdoc}
164
	 */
165 1
	public function offset($offset)
166
	{
167 1
		$this->bindings[':offset'] = (int) $offset;
168 1
		$this->queryBuilder->offset(':offset');
169
170 1
		return $this;
171
	}
172
173
	/**
174
	 * Returns the result of the executed prepared query.
175
	 *
176
	 * @return QueryResult the result of the executed prepared query.
177
	 */
178 4
	public function execute()
179
	{
180 4
		$pdoStatement = $this->pdo->prepare((string) $this);
181
182 4
		foreach ($this->bindings as $key => $value) {
183 1
			if (is_bool($value)) {
184
				$type = \PDO::PARAM_BOOL;
185 1
			} elseif (is_null($value)) {
186
				$type = \PDO::PARAM_NULL;
187 1
			} elseif (is_int($value)) {
188 1
				$type = \PDO::PARAM_INT;
189 1
			} else {
190
				$type = \PDO::PARAM_STR;
191
			}
192
193 1
			$pdoStatement->bindValue($key, $value, $type);
194 4
		}
195
196 4
		$pdoStatement->execute();
197
198 4
		return new QueryResult($pdoStatement);
199
	}
200
201
	/**
202
	 * Returns the values array with bindings instead of values.
203
	 *
204
	 * @param array $values
205
	 * @return array the values array with bindings instead of values.
206
	 */
207 2 View Code Duplication
	private function replaceValuesWithBindings(array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
	{
209 2
		$result = [];
210
211 2
		foreach ($values as $key => $value) {
212 2
			$binding = sprintf(':%s', $key);
213
214 2
			$this->bindings[$binding] = $value;
215 2
			$result[$key] = $binding;
216 2
		}
217
218 2
		return $result;
219
	}
220
}
221