Completed
Push — master ( 32110c...bc25d3 )
by Jean-Christophe
02:12
created

ConditionParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 82
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 10
loc 82
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B addKeyValues() 10 18 5
A addPart() 0 4 1
A compileParts() 0 3 1
A parseKey() 0 8 4
A getCondition() 0 3 1
A getParams() 0 3 1
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 $condition;
11
	private $parts=[];
12
	private $params;
13
	
14
	public function __construct($condition=null){
15
		$this->condition=$condition;
16
	}
17
	
18
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
19
		if(!is_array($keyValues)){
20
			$this->condition=$this->parseKey($keyValues, $classname);
21
		}else{
22 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...
23
				if(isset($classname)){
24
					$keys=OrmUtils::getKeyFields($classname);
25
					$keyValues=\array_combine($keys, $keyValues);
26
				}
27
			}
28
			$retArray=array ();
29 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...
30
				$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
31
				$this->params[]=$value;
32
			}
33
			$this->condition=implode($separator, $retArray);
34
		}
35
	}
36
	
37
	public function addPart($condition,$value){
38
		$this->parts[]=$condition;
39
		$this->params[]=$value;
40
	}
41
	
42
	public function compileParts($separator=" OR "){
43
		$this->condition=implode($separator, $this->parts);
44
	}
45
	
46
	private function parseKey($keyValues,$className){
47
		$condition=$keyValues;
48
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
49
			$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
50
			$this->params[]=$keyValues;
51
		}
52
		return $condition;
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	public function getCondition() {
59
		return $this->condition;
60
	}
61
62
	/**
63
	 * @return mixed
64
	 */
65
	public function getParams() {
66
		return $this->params;
67
	}
68
69
	/**
70
	 * @param string $condition
71
	 */
72
	public function setCondition($condition) {
73
		$this->condition = $condition;
74
	}
75
76
	/**
77
	 * @param mixed $params
78
	 */
79
	public function setParams($params) {
80
		$this->params = $params;
81
	}
82
	
83
	public function limitOne(){
84
		$limit="";
85
		if(\stripos($this->condition, " limit ")===false)
86
			$limit=" limit 1";
87
		$this->condition.=$limit;
88
	}
89
90
}
91
92