Completed
Push — master ( 5afe3b...44ee0c )
by Jean-Christophe
05:18
created

ConditionParser   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 169
Duplicated Lines 6.51 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 43
lcom 1
cbo 3
dl 11
loc 169
rs 8.96
c 1
b 0
f 0

16 Methods

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

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
	public function __construct($condition=null,$firstPart=null,$params=null){
17
		$this->condition=$condition;
18
		$this->firstPart=$firstPart;
19
		if(is_array($params)){
20
			$this->setParams($params);
21
		}
22
	}
23
	
24
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
25
		if(!is_array($keyValues)){
26
			$this->condition=$this->parseKey($keyValues, $classname);
27
		}else{
28 View Code Duplication
			if(!UArray::isAssociative($keyValues)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29
				if(isset($classname)){
30
					$keys=OrmUtils::getKeyFields($classname);
31
					$keyValues=\array_combine($keys, $keyValues);
32
				}
33
			}
34
			$retArray=array ();
35 View Code Duplication
			foreach ( $keyValues as $key => $value ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
				if($this->addParams($value)){
37
					$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
38
				}
39
			}
40
			$this->condition=implode($separator, $retArray);
41
		}
42
	}
43
	
44
	private function addParams($value){
45
		if(!isset($this->params[$value])){
46
			return $this->params[$value]=true;
47
		}
48
		return false;
49
	}
50
	
51
	public function addPart($condition,$value){
52
		if($this->addParams($value)){
53
			$this->parts[]=$condition;
54
			return true;
55
		}
56
		return false;
57
	}
58
	
59
	public function addParts($condition,$values){
60
		foreach ($values as $value){
61
			if($this->addParams($value)){
62
				$this->parts[]=$condition;
63
			}
64
		}
65
	}
66
	
67
	public function compileParts($separator=" OR "){
68
		if($separator==" OR " && sizeof($this->parts)>3){
69
			$parts=$this->refactorParts();
70
			$conditions=[];
71
			foreach ($parts as $part=>$values){
72
				$values[0]="SELECT ? as _id";
73
				$conditions[]=" INNER JOIN (".implode(" UNION ALL SELECT ", $values).") as _tmp ON ".$part."=_tmp._id";
74
			}
75
			$this->condition=implode(" ", $conditions);
76
		}else{
77
			$this->condition=implode($separator, $this->parts);
78
		}
79
	}
80
	
81
	private function refactorParts(){
82
		$tmp="";
0 ignored issues
show
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
		$result=[];
84
		foreach ($this->parts as $part){
85
			$part=str_replace("= ?", "", $part);
86
			$result[$part][]='?';
87
		}
88
		return $result;
89
	}
90
	
91
	private function parseKey($keyValues,$className){
92
		$condition=$keyValues;
93
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
94
			if($this->addParams($keyValues)){
95
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
96
			}
97
		}
98
		return $condition;
99
	}
100
	
101
	/**
102
	 * @return string
103
	 */
104
	public function getCondition() {
105
		if(!isset($this->firstPart)|| $this->firstPart==='')
106
			return $this->condition;
107
		$ret=$this->firstPart;
108
		if(isset($this->condition)){
109
			$ret.=" WHERE ".$this->condition;
110
		}
111
		return $ret;
112
	}
113
	
114
	/**
115
	 * @return mixed
116
	 */
117
	public function getParams() {
118
		if(is_array($this->params)){
119
			if($this->invertedParams){
120
				return array_keys($this->params);
121
			}
122
			return $this->params;
123
		}
124
		return;
125
	}
126
	
127
	/**
128
	 * @return mixed
129
	 */
130
	public function hasParam($value) {
131
		if(is_array($this->params)){
132
			if($this->invertedParams){
133
				return isset($this->params[$value]);
134
			}
135
			return array_search($value,$this->params)!==false;
136
		}
137
		return false;
138
	}
139
	
140
	public function countParts(){
141
		if(is_array($this->params))
142
			return sizeof($this->params);
143
		return 0;
144
	}
145
	
146
	/**
147
	 * @param string $condition
148
	 */
149
	public function setCondition($condition) {
150
		$this->condition = $condition;
151
		return $this;
152
	}
153
	
154
	/**
155
	 * @param mixed $params
156
	 */
157
	public function setParams($params) {
158
		$this->params = $params;
159
		$this->invertedParams=false;
160
		return $this;
161
	}
162
	
163
	public function limitOne(){
164
		$limit="";
165
		if(\stripos($this->condition, " limit ")===false){
166
			$limit=" limit 1";
167
		}
168
		$this->condition.=$limit;
169
	}
170
	
171
	public static function simple($condition,$params){
172
		$cParser=new ConditionParser($condition);
173
		$cParser->addParams($params);
174
		return $cParser;
175
	}
176
	
177
}
178
179