| Total Complexity | 48 |
| Total Lines | 227 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WhereQueryBuilder 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 WhereQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WhereQueryBuilder extends QueryBuilder |
||
| 15 | { |
||
| 16 | private $qb; |
||
| 17 | |||
| 18 | public function __construct(){ |
||
| 19 | $this->qb = new QueryBuilder(); |
||
| 20 | } |
||
| 21 | |||
| 22 | protected function prepareArrayForWhere($bindKey, $bindVal = null){ |
||
| 23 | $ar = $conditionAr = array(); |
||
| 24 | // expecting a string like 'status = :status' |
||
| 25 | if ($this->checkWherePrepareUsed($bindKey)) { |
||
| 26 | $conditionAr = preg_split('/:/', $bindKey); |
||
| 27 | } |
||
| 28 | |||
| 29 | // taking the second part just after : |
||
| 30 | if (is_array($conditionAr) && count($conditionAr)) { |
||
| 31 | $ar[':'.$conditionAr[1]] = $bindVal; |
||
| 32 | } |
||
| 33 | |||
| 34 | return $ar; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function parseWhereQuery($whereQuery = []) |
||
| 38 | { |
||
| 39 | $ar = array(); |
||
| 40 | |||
| 41 | foreach ($whereQuery as $where) { |
||
| 42 | if (is_array($where[1])) { |
||
| 43 | foreach ($where[1] as $key => $value) { |
||
| 44 | $ar[':'.$key] = $value; |
||
| 45 | } |
||
| 46 | } elseif ($where[1] != '') { |
||
| 47 | $arNext = $this->prepareArrayForWhere($where[0], $where[1]); |
||
| 48 | if (count($arNext)) { |
||
| 49 | $ar = array_merge($ar, $arNext); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return $ar; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function checkWherePrepareUsed($condition) |
||
| 58 | { |
||
| 59 | return preg_match('/:+[a-zA-Z_]/', $condition); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function buildWhereQuery($conditions = array()) |
||
| 63 | { |
||
| 64 | $query = array(); |
||
| 65 | |||
| 66 | if (count($conditions)) { |
||
| 67 | $firstTime = true; |
||
| 68 | $query[] = 'WHERE'; |
||
| 69 | $this->whereAdded = true; |
||
| 70 | |||
| 71 | foreach ($conditions as $where) { |
||
| 72 | $sign = '='; |
||
| 73 | if(count($where)==3) { |
||
| 74 | $sign = $where[1]; |
||
| 75 | } |
||
| 76 | if ($firstTime) { |
||
| 77 | $query[] = $this->qb->quote($where[0]).' '.$sign.' '.end($where); |
||
| 78 | $firstTime = false; |
||
| 79 | } else { |
||
| 80 | $query[] = 'AND '.$this->qb->quote($where[0]).' '.$sign.' '.end($where); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $query; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function buildWhereInClauseQuery($terms = array()) |
||
| 89 | { |
||
| 90 | if (is_int($terms[0])) { |
||
| 91 | $dataStr = join(',', $terms); |
||
| 92 | } elseif (is_string($terms[0])) { |
||
| 93 | $dataStr = join("', '", $terms); |
||
| 94 | $dataStr = "'".$dataStr."'"; |
||
| 95 | } else { |
||
| 96 | return null; |
||
| 97 | } |
||
| 98 | |||
| 99 | return $dataStr; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function buildWhereInQuery($conditions = array()) |
||
| 130 | } |
||
| 131 | |||
| 132 | public function buildWhereNotInQuery($conditions = array()) |
||
| 133 | { |
||
| 134 | $query = array(); |
||
| 135 | |||
| 136 | if (count($conditions)) { |
||
| 137 | $firstTime = false; |
||
| 138 | if ($this->whereAdded === false) { |
||
| 139 | $query[] = 'WHERE'; |
||
| 140 | $firstTime = true; |
||
| 141 | $this->whereAdded = true; |
||
| 142 | } |
||
| 143 | |||
| 144 | foreach ($conditions as $whereNotIn) { |
||
| 145 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
||
| 146 | if ($dataStr === null) { |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($firstTime) { |
||
| 151 | $query[] = $whereNotIn[0].' NOT IN ('.$dataStr.')'; |
||
| 152 | $firstTime = false; |
||
| 153 | } else { |
||
| 154 | $query[] = 'AND '.$whereNotIn[0].' NOT IN ('.$dataStr.')'; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $query; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function buildWhereNullQuery($conditions = array()) |
||
| 185 | } |
||
| 186 | |||
| 187 | public function buildWhereNotNullQuery($conditions = array()) |
||
| 188 | { |
||
| 189 | $query = array(); |
||
| 190 | |||
| 191 | if (count($conditions)) { |
||
| 192 | $firstTime = false; |
||
| 193 | if ($this->whereAdded === false) { |
||
| 194 | $query[] = 'WHERE'; |
||
| 195 | $firstTime = true; |
||
| 196 | $this->whereAdded = true; |
||
| 197 | } |
||
| 198 | |||
| 199 | foreach ($conditions as $whereNotNull) { |
||
| 200 | if ($firstTime) { |
||
| 201 | $query[] = $whereNotNull.' IS NOT NULL'; |
||
| 202 | $firstTime = false; |
||
| 203 | } else { |
||
| 204 | $query[] = 'AND '.$whereNotNull.' IS NOT NULL'; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $query; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function buildAllWhereQuery($where, $whereIn, $whereNotIn, $whereNull, $whereNotNull) |
||
| 241 | } |
||
| 242 | } |
||
| 243 |