| Total Complexity | 43 |
| Total Lines | 228 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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(), $query1 = array()) |
||
| 25 | { |
||
| 26 | $query = array(); |
||
| 27 | if (!count($conditions)) { |
||
| 28 | return $query; |
||
| 29 | } |
||
| 30 | |||
| 31 | $firstTime = true; |
||
| 32 | $query[] = '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 | $query[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
| 42 | $firstTime = false; |
||
| 43 | } else { |
||
| 44 | $query[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if (count($query)) { |
||
| 49 | $query = array_merge($query, $query1); |
||
| 50 | } |
||
| 51 | return $query; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function buildWhereInClauseQuery($terms = array()) |
||
| 55 | { |
||
| 56 | if (is_int($terms[0])) { |
||
| 57 | $dataStr = join(',', $terms); |
||
| 58 | } elseif (is_string($terms[0])) { |
||
| 59 | $dataStr = join("', '", $terms); |
||
| 60 | $dataStr = "'".$dataStr."'"; |
||
| 61 | } else { |
||
| 62 | return null; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $dataStr; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function buildWhereRawQuery($conditions = array()) |
||
| 69 | { |
||
| 70 | $query = array(); |
||
| 71 | |||
| 72 | if (!count($conditions)) { |
||
| 73 | return $query; |
||
| 74 | } |
||
| 75 | |||
| 76 | $firstTime = false; |
||
| 77 | if ($this->whereAdded === false) { |
||
| 78 | $query[] = 'WHERE'; |
||
| 79 | $firstTime = true; |
||
| 80 | $this->whereAdded = true; |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach ($conditions as $whereRaw) { |
||
| 84 | if ($firstTime) { |
||
| 85 | $query[] = $whereRaw; |
||
| 86 | $firstTime = false; |
||
| 87 | } else { |
||
| 88 | $query[] = 'AND '.$whereRaw; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return $query; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function buildWhereInQuery($conditions = array()) |
||
| 125 | } |
||
| 126 | |||
| 127 | public function buildWhereNotInQuery($conditions = array()) |
||
| 128 | { |
||
| 129 | $query = array(); |
||
| 130 | |||
| 131 | if (!count($conditions)) { |
||
| 132 | return $query; |
||
| 133 | } |
||
| 134 | |||
| 135 | $firstTime = false; |
||
| 136 | if ($this->whereAdded === false) { |
||
| 137 | $query[] = 'WHERE'; |
||
| 138 | $firstTime = true; |
||
| 139 | $this->whereAdded = true; |
||
| 140 | } |
||
| 141 | |||
| 142 | foreach ($conditions as $whereNotIn) { |
||
| 143 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
||
| 144 | if ($dataStr === null) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($firstTime) { |
||
| 149 | $query[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 150 | $firstTime = false; |
||
| 151 | } else { |
||
| 152 | $query[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $query; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function buildWhereNullQuery($conditions = array()) |
||
| 184 | } |
||
| 185 | |||
| 186 | public function buildWhereNotNullQuery($conditions = array()) |
||
| 187 | { |
||
| 188 | $query = array(); |
||
| 189 | |||
| 190 | if (!count($conditions)) { |
||
| 191 | return $query; |
||
| 192 | } |
||
| 193 | |||
| 194 | $firstTime = false; |
||
| 195 | if ($this->whereAdded === false) { |
||
| 196 | $query[] = 'WHERE'; |
||
| 197 | $firstTime = true; |
||
| 198 | $this->whereAdded = true; |
||
| 199 | } |
||
| 200 | |||
| 201 | foreach ($conditions as $whereNotNull) { |
||
| 202 | if ($firstTime) { |
||
| 203 | $query[] = trim($whereNotNull).' IS NOT NULL'; |
||
| 204 | $firstTime = false; |
||
| 205 | } else { |
||
| 206 | $query[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | return $query; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull) |
||
| 244 | } |
||
| 245 | } |
||
| 246 |