Skip to content

Commit dbac245

Browse files
committed
Removed some form of code duplication shown by scrutinizer
1 parent 9afa8b4 commit dbac245

File tree

9 files changed

+98
-94
lines changed

9 files changed

+98
-94
lines changed

src/Builder/Insert.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,19 @@
55
use UnexpectedValueException;
66

77
class Insert extends InsertUpdateStatement {
8-
/**
9-
* @var array
10-
*/
8+
/** @var array */
119
private $fields = array();
12-
/**
13-
* @var array
14-
*/
10+
/** @var array */
1511
private $update = array();
16-
/**
17-
* @var string
18-
*/
12+
/** @var string */
1913
private $table = null;
20-
/**
21-
* @var string
22-
*/
14+
/** @var string */
2315
private $keyField = null;
24-
/**
25-
* @var bool
26-
*/
16+
/** @var bool */
2717
private $ignore = false;
28-
/**
29-
* @var Select
30-
*/
18+
/** @var Select */
3119
private $from = null;
32-
/**
33-
* @var callable
34-
*/
20+
/** @var callable */
3521
private $tableFields = null;
3622

3723
/**
@@ -77,12 +63,7 @@ public function setKey($field) {
7763
* @return $this
7864
*/
7965
public function add($field, $value) {
80-
if ($this->isFieldNameValid($field)) {
81-
throw new UnexpectedValueException('Field name is invalid');
82-
}
83-
$sqlField = $field;
84-
$sqlValue = $this->db()->quote($value);
85-
$this->fields[$sqlField] = $sqlValue;
66+
$this->fields = $this->addTo($this->fields, $field, $value);
8667
return $this;
8768
}
8869

@@ -93,12 +74,7 @@ public function add($field, $value) {
9374
* @return $this
9475
*/
9576
public function update($field, $value) {
96-
if ($this->isFieldNameValid($field)) {
97-
throw new UnexpectedValueException('Field name is invalid');
98-
}
99-
$sqlField = $field;
100-
$sqlValue = $this->db()->quote($value);
101-
$this->update[$sqlField] = $sqlValue;
77+
$this->update = $this->addTo($this->update, $field, $value);
10278
return $this;
10379
}
10480

@@ -148,13 +124,9 @@ public function addOrUpdateExpr($str) {
148124
* @return $this
149125
*/
150126
public function addAll(array $data, array $mask = null) {
151-
if($mask !== null) {
152-
$data = array_intersect_key($data, array_combine($mask, $mask));
153-
}
154-
$data = $this->clearValues($data);
155-
foreach ($data as $field => $value) {
127+
$this->addAllTo($data, $mask, function ($field, $value) {
156128
$this->add($field, $value);
157-
}
129+
});
158130
return $this;
159131
}
160132

@@ -164,15 +136,11 @@ public function addAll(array $data, array $mask = null) {
164136
* @return $this
165137
*/
166138
public function updateAll(array $data, array $mask = null) {
167-
if($mask !== null) {
168-
$data = array_intersect_key($data, array_combine($mask, $mask));
169-
}
170-
$data = $this->clearValues($data);
171-
foreach ($data as $field => $value) {
172-
if ($field != $this->keyField) {
139+
$this->addAllTo($data, $mask, function ($field, $value) {
140+
if ($field !== $this->keyField) {
173141
$this->update($field, $value);
174142
}
175-
}
143+
});
176144
return $this;
177145
}
178146

@@ -234,6 +202,38 @@ public function __toString() {
234202
return $query;
235203
}
236204

205+
/**
206+
* @param array $fields
207+
* @param string $field
208+
* @param bool|int|float|string $value
209+
* @return array
210+
*/
211+
private function addTo($fields, $field, $value) {
212+
if ($this->isFieldNameValid($field)) {
213+
throw new UnexpectedValueException('Field name is invalid');
214+
}
215+
$sqlField = $field;
216+
$sqlValue = $this->db()->quote($value);
217+
$fields[$sqlField] = $sqlValue;
218+
return $fields;
219+
}
220+
221+
/**
222+
* @param array $data
223+
* @param array $mask
224+
* @param callable $fn
225+
* @return $this
226+
*/
227+
private function addAllTo($data, $mask, $fn) {
228+
if($mask !== null) {
229+
$data = array_intersect_key($data, array_combine($mask, $mask));
230+
}
231+
$data = $this->clearValues($data);
232+
foreach ($data as $field => $value) {
233+
call_user_func($fn, $field, $value);
234+
}
235+
}
236+
237237
/**
238238
* @return string
239239
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Internal;
3+
4+
use Kir\MySQL\Database;
5+
6+
final class ConditionBuilder {
7+
/**
8+
* @param Database $db
9+
* @param string $query
10+
* @param array $conditions
11+
* @param string $token
12+
* @return string
13+
*/
14+
public static function build(Database $db, $query, array $conditions, $token) {
15+
if(!count($conditions)) {
16+
return $query;
17+
}
18+
$query .= "{$token}\n";
19+
$arr = array();
20+
foreach($conditions as $condition) {
21+
list($expression, $arguments) = $condition;
22+
$expr = $db->quoteExpression($expression, $arguments);
23+
$arr[] = "\t({$expr})";
24+
}
25+
$query .= join("\n\tAND\n", $arr);
26+
return $query."\n";
27+
}
28+
}

src/Builder/QueryStatement.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public function execute(array $params = []) {
4343
}
4444

4545
/**
46-
* @param int $fetchStyle
4746
* @return array
4847
*/
49-
public function fetchAll($fetchStyle = PDO::FETCH_ASSOC) {
48+
public function fetchAll() {
5049
$args = func_get_args();
5150
return call_user_func_array([$this->statement, 'fetchAll'], $args);
5251
}

src/Builder/RunnableSelect.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@
44
/**
55
*/
66
class RunnableSelect extends Select {
7-
/**
8-
* @var array
9-
*/
7+
/** @var array */
108
private $values = array();
11-
12-
/**
13-
* @var bool
14-
*/
9+
/** @var bool */
1510
private $preserveTypes;
16-
17-
/**
18-
* @var bool
19-
*/
11+
/** @var int */
2012
private $foundRows = 0;
2113

2214
/**

src/Builder/Traits/GroupByBuilder.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ trait GroupByBuilder {
1010
private $groupBy = array();
1111

1212
/**
13-
* @param string $expression
1413
* @return $this
1514
*/
16-
public function groupBy($expression) {
15+
public function groupBy() {
1716
foreach(func_get_args() as $expression) {
1817
if(is_array($expression)) {
1918
if(!count($expression)) {
@@ -23,7 +22,7 @@ public function groupBy($expression) {
2322
$expression[0],
2423
array_slice($expression, 1)
2524
);
26-
$expression = call_user_func_array(array($this->db(), 'quoteExpression'), $arguments);
25+
$expression = $this->quoteExpr($arguments);
2726
}
2827
$this->groupBy[] = $expression;
2928
}
@@ -45,4 +44,12 @@ protected function buildGroups($query) {
4544
}
4645
return $query.join(",\n", $arr)."\n";
4746
}
47+
48+
/**
49+
* @param array $arguments
50+
* @return mixed
51+
*/
52+
protected function quoteExpr(array $arguments) {
53+
return call_user_func_array(array($this->db(), 'quoteExpression'), $arguments);
54+
}
4855
}

src/Builder/Traits/HavingBuilder.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
namespace Kir\MySQL\Builder\Traits;
33

4+
use Kir\MySQL\Builder\Internal\ConditionBuilder;
5+
46
trait HavingBuilder {
57
use AbstractDB;
68

7-
/**
8-
* @var array
9-
*/
9+
/** @var array */
1010
private $having = array();
1111

1212
/**
@@ -23,17 +23,6 @@ public function having($expression) {
2323
* @return string
2424
*/
2525
protected function buildHavingConditions($query) {
26-
if(!count($this->having)) {
27-
return $query;
28-
}
29-
$query .= "HAVING\n";
30-
$arr = array();
31-
foreach($this->having as $condition) {
32-
list($expression, $arguments) = $condition;
33-
$expr = $this->db()->quoteExpression($expression, $arguments);
34-
$arr[] = "\t({$expr})";
35-
}
36-
$query .= join("\n\tAND\n", $arr);
37-
return $query."\n";
26+
return ConditionBuilder::build($this->db(), $query, $this->having, 'HAVING');
3827
}
3928
}

src/Builder/Traits/WhereBuilder.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
namespace Kir\MySQL\Builder\Traits;
33

4+
use Kir\MySQL\Builder\Internal\ConditionBuilder;
5+
46
trait WhereBuilder {
57
use AbstractDB;
68

7-
/**
8-
* @var array
9-
*/
9+
/** @var array */
1010
private $where = array();
1111

1212
/**
@@ -24,17 +24,6 @@ public function where($expression, $param = null) {
2424
* @return string
2525
*/
2626
protected function buildWhereConditions($query) {
27-
if(!count($this->where)) {
28-
return $query;
29-
}
30-
$query .= "WHERE\n";
31-
$arr = array();
32-
foreach($this->where as $condition) {
33-
list($expression, $arguments) = $condition;
34-
$expr = $this->db()->quoteExpression($expression, $arguments);
35-
$arr[] = "\t({$expr})";
36-
}
37-
$query .= join("\n\tAND\n", $arr);
38-
return $query."\n";
27+
return ConditionBuilder::build($this->db(), $query, $this->where, 'WHERE');
3928
}
4029
}

src/QueryLogger/ClosureQueryLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ClosureQueryLogger implements QueryLogger {
88
/**
99
* @param callable $fn
1010
*/
11-
function __construct($fn) {
11+
public function __construct($fn) {
1212
$this->fn = $fn;
1313
}
1414

src/QueryLogger/LoggerInterfaceQueryLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LoggerInterfaceQueryLogger implements QueryLogger {
1010
/**
1111
* @param LoggerInterface $logger
1212
*/
13-
function __construct(LoggerInterface $logger) {
13+
public function __construct(LoggerInterface $logger) {
1414
$this->logger = $logger;
1515
}
1616

0 commit comments

Comments
 (0)