| Total Complexity | 44 |
| Total Lines | 167 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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){ |
||
| 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 | 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 | $retArray=array (); |
||
| 37 | foreach ( $keyValues as $key => $value ) { |
||
| 38 | if($this->addParams($value)){ |
||
| 39 | $retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?"; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | $this->condition=implode($separator, $retArray); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | private function addParams($value){ |
||
| 47 | if(!isset($this->params[$value])){ |
||
| 48 | return $this->params[$value]=true; |
||
| 49 | } |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function addPart($condition,$value){ |
||
| 54 | if($this->addParams($value)){ |
||
| 55 | $this->parts[]=$condition; |
||
| 56 | return true; |
||
| 57 | } |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function addParts($condition,$values){ |
||
| 62 | foreach ($values as $value){ |
||
| 63 | if($this->addParams($value)){ |
||
| 64 | $this->parts[]=$condition; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | public function compileParts($separator=" OR "){ |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | private function refactorParts(){ |
||
| 84 | $result=[]; |
||
| 85 | foreach ($this->parts as $part){ |
||
| 86 | $part=str_replace("= ?", "", $part); |
||
| 87 | $result[$part][]='?'; |
||
| 88 | } |
||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | |||
| 92 | private function parseKey($keyValues,$className){ |
||
| 93 | $condition=$keyValues; |
||
| 94 | 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 | return $condition; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getCondition() { |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function getParams() { |
||
| 119 | if(is_array($this->params)){ |
||
| 120 | if($this->invertedParams){ |
||
| 121 | return array_keys($this->params); |
||
| 122 | } |
||
| 123 | return $this->params; |
||
| 124 | } |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | public function hasParam($value) { |
||
| 132 | if(is_array($this->params)){ |
||
| 133 | if($this->invertedParams){ |
||
| 134 | return isset($this->params[$value]); |
||
| 135 | } |
||
| 136 | return array_search($value,$this->params)!==false; |
||
| 137 | } |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function countParts(){ |
||
| 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 | public function limitOne(){ |
||
| 165 | $limit=""; |
||
| 166 | if(\stripos($this->condition, " limit ")===false){ |
||
| 167 | $limit=" limit 1"; |
||
| 168 | } |
||
| 169 | $this->condition.=$limit; |
||
| 170 | } |
||
| 171 | |||
| 172 | public static function simple($condition,$params){ |
||
| 176 | } |
||
| 177 | |||
| 178 | } |
||
| 179 | |||
| 180 |