| Total Complexity | 44 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 | public function buildWhereQuery($conditions = array(), $query = array()) |
||
| 25 | { |
||
| 26 | $whereQuery = array(); |
||
| 27 | if (!count($conditions)) { |
||
| 28 | return $whereQuery; |
||
| 29 | } |
||
| 30 | |||
| 31 | $firstTime = true; |
||
| 32 | $whereQuery[] = 'WHERE'; |
||
| 33 | $this->whereAdded = true; |
||
| 34 | |||
| 35 | foreach ($conditions as $where) { |
||
| 36 | $sign = '='; |
||
| 37 | if(count($where)==3) { |
||
| 38 | $sign = $where[1]; |
||
| 39 | } |
||
| 40 | if ($firstTime) { |
||
| 41 | $whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
| 42 | $firstTime = false; |
||
| 43 | } else { |
||
| 44 | $whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if (count($whereQuery)) { |
||
| 49 | $query = array_merge($query, $whereQuery); |
||
| 50 | } |
||
| 51 | return $query; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function buildWhereInClauseQuery($terms = array()) |
||
| 66 | } |
||
| 67 | |||
| 68 | public function buildWhereRawQuery($conditions = array(), $query = array()) |
||
| 96 | } |
||
| 97 | |||
| 98 | public function buildWhereInQuery($conditions = array()) |
||
| 128 | } |
||
| 129 | |||
| 130 | public function buildWhereNotInQuery($conditions = array()) |
||
| 131 | { |
||
| 132 | $whereNotInQuery = array(); |
||
| 133 | |||
| 134 | if (!count($conditions)) { |
||
| 135 | return $whereNotInQuery; |
||
| 136 | } |
||
| 137 | |||
| 138 | $firstTime = false; |
||
| 139 | if ($this->whereAdded === false) { |
||
| 140 | $whereNotInQuery[] = 'WHERE'; |
||
| 141 | $firstTime = true; |
||
| 142 | $this->whereAdded = true; |
||
| 143 | } |
||
| 144 | |||
| 145 | foreach ($conditions as $whereNotIn) { |
||
| 146 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
||
| 147 | if ($dataStr === null) { |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($firstTime) { |
||
| 152 | $whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 153 | $firstTime = false; |
||
| 154 | } else { |
||
| 155 | $whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $whereNotInQuery; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function buildWhereNullQuery($conditions = array(), $query = array()) |
||
| 189 | } |
||
| 190 | |||
| 191 | public function buildWhereNotNullQuery($conditions = array(), $query = array()) |
||
| 192 | { |
||
| 193 | $whereNotNullQuery = array(); |
||
| 194 | |||
| 195 | if (!count($conditions)) { |
||
| 196 | return $whereNotNullQuery; |
||
| 197 | } |
||
| 198 | |||
| 199 | $firstTime = false; |
||
| 200 | if ($this->whereAdded === false) { |
||
| 201 | $whereNotNullQuery[] = 'WHERE'; |
||
| 202 | $firstTime = true; |
||
| 203 | $this->whereAdded = true; |
||
| 204 | } |
||
| 205 | |||
| 206 | foreach ($conditions as $whereNotNull) { |
||
| 207 | if ($firstTime) { |
||
| 208 | $whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; |
||
| 209 | $firstTime = false; |
||
| 210 | } else { |
||
| 211 | $whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | if (count($whereNotNullQuery)) { |
||
| 215 | $query = array_merge($query, $whereNotNullQuery); |
||
| 216 | } |
||
| 217 | return $query; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array()) |
||
| 240 | } |
||
| 241 | } |
||
| 242 |