Completed
Push — master ( cbe892...9bd3b4 )
by Jean-Christophe
02:05
created

ConditionParser   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 110
Duplicated Lines 4.55 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 5
loc 110
rs 10
c 1
b 0
f 0

12 Methods

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

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
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
	
15
	public function __construct($condition=null,$firstPart=null){
16
		$this->condition=$condition;
17
		$this->firstPart=$firstPart;
18
	}
19
	
20
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
21
		if(!is_array($keyValues)){
22
			$this->condition=$this->parseKey($keyValues, $classname);
23
		}else{
24
			if(!UArray::isAssociative($keyValues)){
25
				if(isset($classname)){
26
					$keys=OrmUtils::getKeyFields($classname);
27
					$keyValues=\array_combine($keys, $keyValues);
28
				}
29
			}
30
			$retArray=array ();
31 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...
32
				if($this->addParams($value)){
33
					$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
34
				}
35
			}
36
			$this->condition=implode($separator, $retArray);
37
		}
38
	}
39
	
40
	private function addParams($value){
41
		if(!isset($this->params[$value])){
42
			return $this->params[$value]=true;
43
		}
44
		return false;
45
	}
46
	
47
	public function addPart($condition,$value){
48
		if($this->addParams($value)){
49
			$this->parts[]=$condition;
50
		}
51
	}
52
	
53
	public function addParts($condition,$values){
54
		foreach ($values as $value){
55
			if($this->addParams($value)){
56
				$this->parts[]=$condition;
57
			}
58
		}
59
	}
60
	
61
	public function compileParts($separator=" OR "){
62
		$this->condition=implode($separator, $this->parts);
63
	}
64
	
65
	private function parseKey($keyValues,$className){
66
		$condition=$keyValues;
67
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
68
			if($this->addParams($keyValues)){
69
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
70
			}
71
		}
72
		return $condition;
73
	}
74
	
75
	/**
76
	 * @return string
77
	 */
78
	public function getCondition() {
79
		if(!isset($this->firstPart))
80
			return $this->condition;
81
			$ret=$this->firstPart;
82
			if(isset($this->condition)){
83
				$ret.=" WHERE ".$this->condition;
84
			}
85
			return $ret;
86
	}
87
	
88
	/**
89
	 * @return mixed
90
	 */
91
	public function getParams() {
92
		if(is_array($this->params))
93
			return array_keys($this->params);
94
			return;
95
	}
96
	
97
	/**
98
	 * @param string $condition
99
	 */
100
	public function setCondition($condition) {
101
		$this->condition = $condition;
102
	}
103
	
104
	/**
105
	 * @param mixed $params
106
	 */
107
	public function setParams($params) {
108
		$this->params = $params;
109
	}
110
	
111
	public function limitOne(){
112
		$limit="";
113
		if(\stripos($this->condition, " limit ")===false)
114
			$limit=" limit 1";
115
			$this->condition.=$limit;
116
	}
117
	
118
}
119
120