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

PostgresWhereTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 79
loc 79
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
 *
4
 * @filesource   PostgresWhereTrait.php
5
 * @created      12.06.2017
6
 * @package      chillerlan\Database\Query\Dialects\Postgres
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2017 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\Database\Query\Dialects\Postgres;
13
14
use chillerlan\Database\Query\StatementInterface;
15
use chillerlan\Database\Query\Traits\WhereTrait;
16
17
/**
18
 * Trait PostgresWhereTrait
19
 */
20 View Code Duplication
trait PostgresWhereTrait{
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...
21
	use WhereTrait;
22
23
	protected function _addWhere($val1, $val2 = null, $operator = null, $bind = true, $join = 'AND'){
24
		$operator = strtoupper(trim($operator));
25
		$join = strtoupper(trim($join));
26
27
		$operators = [
28
			'=', '>=', '>', '<=', '<', '<>', '!=',
29
			'|', '&', '<<', '>>', '+', '-', '*', '/', '%', '^', '<=>', '~', '!', 'DIV', 'MOD',
30
			'IS', 'IS NOT', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'REGEXP', 'NOT REGEXP', 'BETWEEN', 'NOT BETWEEN', 'EXISTS'
31
		];
32
33
		if(in_array($operator, $operators, true)){
34
			$where = [$this->quote($val1)];
0 ignored issues
show
Bug introduced by
It seems like quote() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
			$values = [];
36
37
			if(in_array($operator, ['IN', 'NOT IN'], true)){
38
39
				if(is_array($val2)){
40
					if($bind){
41
						$where[] = 'IN('.implode(',', array_fill(0, count($val2), '?')).')';
42
						$values = array_merge($values, $val2);
43
					}
44
					else{
45
						$where[] = 'IN('.implode(',', $val2).')'; // @todo: quote
46
					}
47
				}
48
				else if($val2 instanceof StatementInterface){
49
					$where[] = 'IN('.$val2->sql().')';
50
					$values = array_merge($values, $val2->bindValues());
51
				}
52
53
			}
54
			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...
55
56
			}
57
			else{
58
				$where[] = $operator;
59
60
				if(is_null($val2)){
61
					$where[] = 'NULL';
62
				}
63
				else if(is_bool($val2)){
64
					$where[] = $val2 ? 'TRUE' : 'FALSE';
65
				}
66
				else if(in_array(strtolower($val2), ['null', 'false', 'true', 'unknown'])){
67
					$where[] = strtoupper($val2);
68
				}
69
				else if($val2 instanceof StatementInterface){
70
					$where[] = '('.$val2->sql().')';
71
					$values = array_merge($values, $val2->bindValues());
72
				}
73
				else{
74
					if($bind){
75
						$where[] = '?';
76
						$values[] = $val2;
77
					}
78
					else{
79
						if(!empty($val2)){
80
							$where[] = $this->quote($val2);
0 ignored issues
show
Bug introduced by
It seems like quote() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
						}
82
					}
83
				}
84
85
			}
86
87
			$this->bindValues = array_merge($this->bindValues, $values);
0 ignored issues
show
Bug introduced by
The property bindValues does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
88
			$this->where[] = [
89
				'join' => in_array($join, ['AND', 'OR', 'XOR']) ? $join : 'AND',
90
				'stmt' => implode(' ', $where),
91
			];
92
93
		}
94
95
		return $this;
96
	}
97
98
}
99