| Total Complexity | 40 | 
| Total Lines | 218 | 
| Duplicated Lines | 0 % | 
| Changes | 4 | ||
| Bugs | 3 | Features | 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 | ||
| 16 | class WhereQueryBuilder extends QueryBuilder | ||
| 17 | { | ||
| 18 | private $qb; | ||
| 19 | |||
| 20 |     public function __construct(){ | ||
| 22 | } | ||
| 23 | |||
| 24 |     private function emptyConditionsResponse($conditions = array()){ | ||
| 25 |         if (!count($conditions)) { | ||
| 26 | return array(); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | private function buildWhereInClauseQuery($terms = array()) | ||
| 31 |     { | ||
| 32 |         if (is_int($terms[0])) { | ||
| 33 |             $dataStr = join(',', $terms); | ||
| 34 |         } elseif (is_string($terms[0])) { | ||
| 35 |             $dataStr = join("', '", $terms); | ||
| 36 | $dataStr = "'".$dataStr."'"; | ||
| 37 |         } else { | ||
| 38 | return null; | ||
| 39 | } | ||
| 40 | |||
| 41 | return $dataStr; | ||
| 42 | } | ||
| 43 | |||
| 44 | public function buildWhereQuery($conditions = array(), $query = array()) | ||
| 45 |     { | ||
| 46 | $whereQuery = array(); | ||
| 47 | $this->emptyConditionsResponse($conditions); | ||
| 48 | |||
| 49 | $firstTime = true; | ||
| 50 | $whereQuery[] = 'WHERE'; | ||
| 51 | $this->whereAdded = true; | ||
| 52 | |||
| 53 |         foreach ($conditions as $where) { | ||
| 54 | $sign = '='; | ||
| 55 |             if(count($where)==3) { | ||
| 56 | $sign = $where[1]; | ||
| 57 | } | ||
| 58 |             if ($firstTime) { | ||
| 59 | $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); | ||
| 60 | $firstTime = false; | ||
| 61 |             } else { | ||
| 62 | $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 |         if (count($whereQuery)) { | ||
| 67 | $query = array_merge($query, $whereQuery); | ||
| 68 | } | ||
| 69 | return $query; | ||
| 70 | } | ||
| 71 | |||
| 72 | public function buildWhereRawQuery($conditions = array(), $query = array()) | ||
| 73 |     { | ||
| 74 | $whereRawQuery = array(); | ||
| 75 | |||
| 76 | $this->emptyConditionsResponse($conditions); | ||
| 77 | |||
| 78 | $firstTime = false; | ||
| 79 |         if ($this->whereAdded === false) { | ||
| 80 | $whereRawQuery[] = 'WHERE'; | ||
| 81 | $firstTime = true; | ||
| 82 | $this->whereAdded = true; | ||
| 83 | } | ||
| 84 | |||
| 85 |         foreach ($conditions as $whereRaw) { | ||
| 86 |             if ($firstTime) { | ||
| 87 | $whereRawQuery[] = $whereRaw; | ||
| 88 | $firstTime = false; | ||
| 89 |             } else { | ||
| 90 | $whereRawQuery[] = 'AND '.$whereRaw; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 |         if (count($whereRawQuery)) { | ||
| 95 | $query = array_merge($query, $whereRawQuery); | ||
| 96 | } | ||
| 97 | return $query; | ||
| 98 | } | ||
| 99 | |||
| 100 | public function buildWhereInQuery($conditions = array()) | ||
| 128 | } | ||
| 129 | |||
| 130 | public function buildWhereNotInQuery($conditions = array()) | ||
| 131 |     { | ||
| 132 | $whereNotInQuery = array(); | ||
| 133 | |||
| 134 | $this->emptyConditionsResponse($conditions); | ||
| 135 | |||
| 136 | $firstTime = false; | ||
| 137 |         if ($this->whereAdded === false) { | ||
| 138 | $whereNotInQuery[] = 'WHERE'; | ||
| 139 | $firstTime = true; | ||
| 140 | $this->whereAdded = true; | ||
| 141 | } | ||
| 142 | |||
| 143 |         foreach ($conditions as $whereNotIn) { | ||
| 144 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); | ||
| 145 |             if ($dataStr === null) { | ||
| 146 | continue; | ||
| 147 | } | ||
| 148 | |||
| 149 |             if ($firstTime) { | ||
| 150 |                 $whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; | ||
| 151 | $firstTime = false; | ||
| 152 |             } else { | ||
| 153 |                 $whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | return $whereNotInQuery; | ||
| 158 | } | ||
| 159 | |||
| 160 | public function buildWhereNullQuery($conditions = array(), $query = array()) | ||
| 185 | } | ||
| 186 | |||
| 187 | public function buildWhereNotNullQuery($conditions = array(), $query = array()) | ||
| 188 |     { | ||
| 189 | $whereNotNullQuery = array(); | ||
| 190 | |||
| 191 | $this->emptyConditionsResponse($conditions); | ||
| 192 | |||
| 193 | $firstTime = false; | ||
| 194 |         if ($this->whereAdded === false) { | ||
| 195 | $whereNotNullQuery[] = 'WHERE'; | ||
| 196 | $firstTime = true; | ||
| 197 | $this->whereAdded = true; | ||
| 198 | } | ||
| 199 | |||
| 200 |         foreach ($conditions as $whereNotNull) { | ||
| 201 |             if ($firstTime) { | ||
| 202 | $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; | ||
| 203 | $firstTime = false; | ||
| 204 |             } else { | ||
| 205 | $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; | ||
| 206 | } | ||
| 207 | } | ||
| 208 |         if (count($whereNotNullQuery)) { | ||
| 209 | $query = array_merge($query, $whereNotNullQuery); | ||
| 210 | } | ||
| 211 | return $query; | ||
| 212 | } | ||
| 213 | |||
| 214 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array()) | ||
| 234 | } | ||
| 235 | } | ||
| 236 |