| Total Complexity | 43 |
| Total Lines | 229 |
| 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 |
||
| 16 | class WhereQueryBuilder extends QueryBuilder |
||
| 17 | { |
||
| 18 | private $qb; |
||
| 19 | |||
| 20 | public function __construct(){ |
||
| 21 | $this->qb = new QueryBuilder(); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function buildWhereQuery($conditions = array()) |
||
| 50 | } |
||
| 51 | |||
| 52 | public function buildWhereInClauseQuery($terms = array()) |
||
| 64 | } |
||
| 65 | |||
| 66 | public function buildWhereRawQuery($conditions = array()) |
||
| 67 | { |
||
| 68 | $query = array(); |
||
| 69 | |||
| 70 | if (!count($conditions)) { |
||
| 71 | return $query; |
||
| 72 | } |
||
| 73 | |||
| 74 | $firstTime = false; |
||
| 75 | if ($this->whereAdded === false) { |
||
| 76 | $query[] = 'WHERE'; |
||
| 77 | $firstTime = true; |
||
| 78 | $this->whereAdded = true; |
||
| 79 | } |
||
| 80 | |||
| 81 | foreach ($conditions as $whereRaw) { |
||
| 82 | if ($firstTime) { |
||
| 83 | $query[] = $whereRaw; |
||
| 84 | $firstTime = false; |
||
| 85 | } else { |
||
| 86 | $query[] = 'AND '.$whereRaw; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | return $query; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function buildWhereInQuery($conditions = array()) |
||
| 123 | } |
||
| 124 | |||
| 125 | public function buildWhereNotInQuery($conditions = array()) |
||
| 126 | { |
||
| 127 | $query = array(); |
||
| 128 | |||
| 129 | if (!count($conditions)) { |
||
| 130 | return $query; |
||
| 131 | } |
||
| 132 | |||
| 133 | $firstTime = false; |
||
| 134 | if ($this->whereAdded === false) { |
||
| 135 | $query[] = 'WHERE'; |
||
| 136 | $firstTime = true; |
||
| 137 | $this->whereAdded = true; |
||
| 138 | } |
||
| 139 | |||
| 140 | foreach ($conditions as $whereNotIn) { |
||
| 141 | $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
||
| 142 | if ($dataStr === null) { |
||
| 143 | continue; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($firstTime) { |
||
| 147 | $query[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 148 | $firstTime = false; |
||
| 149 | } else { |
||
| 150 | $query[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return $query; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function buildWhereNullQuery($conditions = array()) |
||
| 182 | } |
||
| 183 | |||
| 184 | public function buildWhereNotNullQuery($conditions = array()) |
||
| 185 | { |
||
| 186 | $query = array(); |
||
| 187 | |||
| 188 | if (!count($conditions)) { |
||
| 189 | return $query; |
||
| 190 | } |
||
| 191 | |||
| 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[] = trim($whereNotNull).' IS NOT NULL'; |
||
| 202 | $firstTime = false; |
||
| 203 | } else { |
||
| 204 | $query[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $query; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull) |
||
| 245 | } |
||
| 246 | } |
||
| 247 |