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

WhereTrait::_addWhere()   C

Complexity

Conditions 15
Paths 27

Size

Total Lines 74
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 5.3685
c 0
b 0
f 0
cc 15
eloc 44
nc 27
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * @filesource   WhereTrait.php
5
 * @created      12.06.2017
6
 * @package      chillerlan\Database\Query\Traits
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2017 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\Database\Query\Traits;
13
14
/**
15
 * Trait WhereTrait
16
 */
17
trait WhereTrait{
18
19
	/**
20
	 * @var array
21
	 */
22
	protected $where = [];
23
24
	/**
25
	 * @param string|null $join
26
	 *
27
	 * @return $this
28
	 */
29
	protected function _openBracket($join = null){
30
		$join = strtoupper(trim($join));
31
32
		if(in_array($join, ['AND', 'OR', 'XOR'])){
33
			$this->where[] = $join;
34
		}
35
36
		$this->where[] = '(';
37
38
		return $this;
39
	}
40
41
	/**
42
	 * @return $this
43
	 */
44
	protected function _closeBracket(){
45
		$this->where[] = ')';
46
47
		return $this;
48
	}
49
50
	/**
51
	 * @return string
52
	 */
53
	protected function _getWhere():string {
54
		$where = [];
55
56
		foreach($this->where as $k => $v){
57
			$last = $this->where[$k-1] ?? false;
58
59
			if(in_array($v, ['AND', 'OR', 'XOR', '(', ')'], true)){
60
				$where[] = $v;
61
62
				continue;
63
			}
64
65
			if(!is_array($v)){
66
				continue;
67
			}
68
69
			if(!$last || $last === '('){
70
				$where[] = $v['stmt'];
71
			}
72
			else{
73
				$where[] = $v['join'].' '.$v['stmt'];
74
			}
75
76
		}
77
78
		return !empty($where) ? PHP_EOL.'WHERE '.implode(' '.PHP_EOL."\t", $where) : '';
79
	}
80
81
}
82