Completed
Push — master ( ffbb02...68e5b2 )
by smiley
02:42
created

MySQLWhereTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 91
loc 91
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C _addWhere() 74 74 15

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
 * Trait MySQLWhereTrait
4
 *
5
 * @filesource   MySQLWhereTrait.php
6
 * @created      07.06.2017
7
 * @package      chillerlan\Database\Query\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query\Dialects\MySQL;
14
15
use chillerlan\Database\Query\StatementInterface;
16
use chillerlan\Database\Query\Traits\WhereTrait;
17
18
/**
19
 * @method quote(string $str):string
20
 */
21 View Code Duplication
trait MySQLWhereTrait{
0 ignored issues
show
Duplication introduced by
This class 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...
22
	use WhereTrait;
23
24
	protected $where = [];
25
	protected $bindValues = [];
26
27
	/**
28
	 * @param        $val1
29
	 * @param null   $val2
30
	 * @param null   $operator
31
	 * @param bool   $bind
32
	 * @param string $join
33
	 *
34
	 * @return $this
35
	 */
36
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
37
		$operator = strtoupper(trim($operator));
38
		$join = strtoupper(trim($join));
39
40
		$operators = [
41
			'=', '>=', '>', '<=', '<', '<>', '!=',
42
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
43
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
44
		];
45
46
		if(in_array($operator, $operators, true)){
47
			$where = [$this->quote($val1)];
48
			$values = [];
49
50
			if(in_array($operator, ['IN', 'NOT IN'], true)){
51
52
				if(is_array($val2)){
53
					if($bind){
54
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
55
						$values = array_merge($values, $val2);
56
					}
57
					else{
58
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
59
					}
60
				}
61
				else if($val2 instanceof StatementInterface){
62
					$where[] = 'IN('.$val2->sql().')';
63
					$values = array_merge($values, $val2->bindValues());
64
				}
65
66
			}
67
			else if(in_array($operator, ['BETWEEN', 'NOT BETWEEN'], true)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
68
69
			}
70
			else{
71
				$where[] = $operator;
72
73
				if(is_null($val2)){
74
					$where[] = 'NULL';
75
				}
76
				else if(is_bool($val2)){
77
					$where[] = $val2 ? 'TRUE' : 'FALSE';
78
				}
79
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
80
					$where[] = strtoupper($val2);
81
				}
82
				else if($val2 instanceof StatementInterface){
83
					$where[] = '('.$val2->sql().')';
84
					$values = array_merge($values, $val2->bindValues());
85
				}
86
				else{
87
					if($bind){
88
						$where[] = '?';
89
						$values[] = $val2;
90
					}
91
					else{
92
						if(!empty($val2)){
93
							$where[] = $this->quote($val2);
94
						}
95
					}
96
				}
97
98
			}
99
100
			$this->bindValues = array_merge($this->bindValues, $values);
101
			$this->where[] = [
102
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
103
				'stmt' => implode(' ', $where),
104
			];
105
106
		}
107
108
		return $this;
109
	}
110
111
}
112