Passed
Push — master ( 70ce04...9471d7 )
by Jean-Christophe
03:03
created

ConditionParser   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 167
ccs 75
cts 96
cp 0.7813
rs 8.8798
c 0
b 0
f 0
wmc 44

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setParams() 0 4 1
A setCondition() 0 3 1
A compileParts() 0 11 4
A addParams() 0 5 2
A addPart() 0 6 2
A getCondition() 0 8 4
A countParts() 0 4 2
A addParts() 0 4 3
A __construct() 0 5 2
A getParams() 0 8 3
A refactorParts() 0 7 2
A hasParam() 0 8 3
A parseKey() 0 8 5
A simple() 0 4 1
A limitOne() 0 6 2
B addKeyValues() 0 19 7

How to fix   Complexity   

Complex Class

Complex classes like ConditionParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConditionParser, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use Ubiquity\utils\base\UArray;
6
use Ubiquity\orm\OrmUtils;
7
use Ubiquity\db\SqlUtils;
8
9
class ConditionParser {
10
	private $firstPart;
11
	private $condition;
12
	private $parts=[];
13
	private $params;
14
	private $invertedParams=true;
15
	
16 7
	public function __construct($condition=null,$firstPart=null,$params=null){
17 7
		$this->condition=$condition;
18 7
		$this->firstPart=$firstPart;
19 7
		if(is_array($params)){
20
			$this->setParams($params);
21
		}
22 7
	}
23
	
24 4
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
25 4
		if(!is_array($keyValues)){
26 4
			$this->condition=$this->parseKey($keyValues, $classname);
27
		}else{
28 1
			if(!UArray::isAssociative($keyValues)){
29
				if(isset($classname)){
30
					$keys=OrmUtils::getKeyFields($classname);
31
					if(is_array($keys)){
32
						$keyValues=\array_combine($keys, $keyValues);
33
					}
34
				}
35
			}
36 1
			$retArray=array ();
37 1
			foreach ( $keyValues as $key => $value ) {
38 1
				if($this->addParams($value)){
39 1
					$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
40
				}
41
			}
42 1
			$this->condition=implode($separator, $retArray);
43
		}
44 4
	}
45
	
46 6
	private function addParams($value){
47 6
		if(!isset($this->params[$value])){
48 6
			return $this->params[$value]=true;
49
		}
50 3
		return false;
51
	}
52
	
53 4
	public function addPart($condition,$value){
54 4
		if($this->addParams($value)){
55 4
			$this->parts[]=$condition;
56 4
			return true;
57
		}
58
		return false;
59
	}
60
	
61 4
	public function addParts($condition,$values){
62 4
		foreach ($values as $value){
63 4
			if($this->addParams($value)){
64 4
				$this->parts[]=$condition;
65
			}
66
		}
67 4
	}
68
	
69 4
	public function compileParts($separator=" OR "){
70 4
		if($separator==" OR " && sizeof($this->parts)>3){
71 3
			$parts=$this->refactorParts();
72 3
			$conditions=[];
73 3
			foreach ($parts as $part=>$values){
74 3
				$values[0]="SELECT ? as _id";
75 3
				$conditions[]=" INNER JOIN (".implode(" UNION ALL SELECT ", $values).") as _tmp ON ".$part."=_tmp._id";
76
			}
77 3
			$this->condition=implode(" ", $conditions);
78
		}else{
79 4
			$this->condition=implode($separator, $this->parts);
80
		}
81 4
	}
82
	
83 3
	private function refactorParts(){
84 3
		$result=[];
85 3
		foreach ($this->parts as $part){
86 3
			$part=str_replace("= ?", "", $part);
87 3
			$result[$part][]='?';
88
		}
89 3
		return $result;
90
	}
91
	
92 4
	private function parseKey($keyValues,$className){
93 4
		$condition=$keyValues;
94 4
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
95
			if($this->addParams($keyValues)){
96
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
97
			}
98
		}
99 4
		return $condition;
100
	}
101
	
102
	/**
103
	 * @return string
104
	 */
105 7
	public function getCondition() {
106 7
		if(!isset($this->firstPart)|| $this->firstPart==='')
107 7
			return $this->condition;
108
		$ret=$this->firstPart;
109
		if(isset($this->condition)){
110
			$ret.=" WHERE ".$this->condition;
111
		}
112
		return $ret;
113
	}
114
	
115
	/**
116
	 * @return mixed
117
	 */
118 7
	public function getParams() {
119 7
		if(is_array($this->params)){
120 6
			if($this->invertedParams){
121 6
				return array_keys($this->params);
122
			}
123
			return $this->params;
124
		}
125 7
		return;
126
	}
127
	
128
	/**
129
	 * @return mixed
130
	 */
131 4
	public function hasParam($value) {
132 4
		if(is_array($this->params)){
133 3
			if($this->invertedParams){
134 3
				return isset($this->params[$value]);
135
			}
136
			return array_search($value,$this->params)!==false;
137
		}
138 4
		return false;
139
	}
140
	
141 4
	public function countParts(){
142 4
		if(is_array($this->params))
143 2
			return sizeof($this->params);
144 4
		return 0;
145
	}
146
	
147
	/**
148
	 * @param string $condition
149
	 */
150
	public function setCondition($condition) {
151
		$this->condition = $condition;
152
		return $this;
153
	}
154
	
155
	/**
156
	 * @param mixed $params
157
	 */
158
	public function setParams($params) {
159
		$this->params = $params;
160
		$this->invertedParams=false;
161
		return $this;
162
	}
163
	
164 4
	public function limitOne(){
165 4
		$limit="";
166 4
		if(\stripos($this->condition, " limit ")===false){
167 4
			$limit=" limit 1";
168
		}
169 4
		$this->condition.=$limit;
170 4
	}
171
	
172 2
	public static function simple($condition,$params){
173 2
		$cParser=new ConditionParser($condition);
174 2
		$cParser->addParams($params);
175 2
		return $cParser;
176
	}
177
	
178
}
179
180