| Total Complexity | 45 |
| Total Lines | 368 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 1 | Features | 6 |
Complex classes like WhereQuery 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 WhereQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class WhereQuery extends Query { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array $available_criteria |
||
| 57 | */ |
||
| 58 | protected $available_criteria = [ |
||
| 59 | 'OR', 'AND', |
||
| 60 | 'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
||
| 61 | 'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
||
| 62 | 'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT" |
||
| 67 | * @param string $name |
||
| 68 | * @param array|null $arguments |
||
| 69 | * |
||
| 70 | * @return mixed |
||
| 71 | * @throws InvalidWhereQueryCriteriaException |
||
| 72 | * @throws MethodNotFoundException |
||
| 73 | */ |
||
| 74 | public function __call($name, $arguments) { |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Validate a given criteria |
||
| 94 | * @param $criteria |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | * @throws InvalidWhereQueryCriteriaException |
||
| 98 | */ |
||
| 99 | protected function validate_criteria($criteria) { |
||
| 100 | $criteria = strtoupper($criteria); |
||
| 101 | |||
| 102 | if (substr($criteria, 0, 6) === "CUSTOM") { |
||
| 103 | return substr($criteria, 6); |
||
| 104 | } |
||
| 105 | if(in_array($criteria, $this->available_criteria) === false) { |
||
| 106 | throw new InvalidWhereQueryCriteriaException(); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $criteria; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param mixed $criteria |
||
| 114 | * @param null $value |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | * @throws InvalidWhereQueryCriteriaException |
||
| 118 | */ |
||
| 119 | public function where($criteria, $value = null) { |
||
| 120 | if(is_array($criteria)){ |
||
| 121 | foreach($criteria as $arguments){ |
||
| 122 | if(count($arguments) == 1){ |
||
| 123 | $this->where($arguments[0]); |
||
| 124 | }elseif(count($arguments) == 2){ |
||
| 125 | $this->where($arguments[0], $arguments[1]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | }else{ |
||
| 129 | $criteria = $this->validate_criteria($criteria); |
||
| 130 | $value = $this->parse_value($value); |
||
| 131 | |||
| 132 | if($value === null || $value === ''){ |
||
| 133 | $this->query->push([$criteria]); |
||
| 134 | }else{ |
||
| 135 | $this->query->push([$criteria, $value]); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param \Closure $closure |
||
| 144 | * |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function orWhere(\Closure $closure = null) { |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param \Closure $closure |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function andWhere(\Closure $closure = null) { |
||
| 160 | $this->query->push(['AND']); |
||
| 161 | if($closure !== null) $closure($this); |
||
| 162 | |||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return WhereQuery |
||
| 168 | * @throws InvalidWhereQueryCriteriaException |
||
| 169 | */ |
||
| 170 | public function whereAll() { |
||
| 171 | return $this->where('ALL'); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return WhereQuery |
||
| 176 | * @throws InvalidWhereQueryCriteriaException |
||
| 177 | */ |
||
| 178 | public function whereAnswered() { |
||
| 179 | return $this->where('ANSWERED'); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $value |
||
| 184 | * |
||
| 185 | * @return WhereQuery |
||
| 186 | * @throws InvalidWhereQueryCriteriaException |
||
| 187 | */ |
||
| 188 | public function whereBcc($value) { |
||
| 189 | return $this->where('BCC', $value); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param mixed $value |
||
| 194 | * @return WhereQuery |
||
| 195 | * @throws InvalidWhereQueryCriteriaException |
||
| 196 | * @throws MessageSearchValidationException |
||
| 197 | */ |
||
| 198 | public function whereBefore($value) { |
||
| 199 | $date = $this->parse_date($value); |
||
| 200 | return $this->where('BEFORE', $date); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $value |
||
| 205 | * |
||
| 206 | * @return WhereQuery |
||
| 207 | * @throws InvalidWhereQueryCriteriaException |
||
| 208 | */ |
||
| 209 | public function whereBody($value) { |
||
| 210 | return $this->where('BODY', $value); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $value |
||
| 215 | * |
||
| 216 | * @return WhereQuery |
||
| 217 | * @throws InvalidWhereQueryCriteriaException |
||
| 218 | */ |
||
| 219 | public function whereCc($value) { |
||
| 220 | return $this->where('CC', $value); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return WhereQuery |
||
| 225 | * @throws InvalidWhereQueryCriteriaException |
||
| 226 | */ |
||
| 227 | public function whereDeleted() { |
||
| 228 | return $this->where('DELETED'); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $value |
||
| 233 | * |
||
| 234 | * @return WhereQuery |
||
| 235 | * @throws InvalidWhereQueryCriteriaException |
||
| 236 | */ |
||
| 237 | public function whereFlagged($value) { |
||
| 238 | return $this->where('FLAGGED', $value); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $value |
||
| 243 | * |
||
| 244 | * @return WhereQuery |
||
| 245 | * @throws InvalidWhereQueryCriteriaException |
||
| 246 | */ |
||
| 247 | public function whereFrom($value) { |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $value |
||
| 253 | * |
||
| 254 | * @return WhereQuery |
||
| 255 | * @throws InvalidWhereQueryCriteriaException |
||
| 256 | */ |
||
| 257 | public function whereKeyword($value) { |
||
| 258 | return $this->where('KEYWORD', $value); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return WhereQuery |
||
| 263 | * @throws InvalidWhereQueryCriteriaException |
||
| 264 | */ |
||
| 265 | public function whereNew() { |
||
| 266 | return $this->where('NEW'); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return WhereQuery |
||
| 271 | * @throws InvalidWhereQueryCriteriaException |
||
| 272 | */ |
||
| 273 | public function whereNot() { |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return WhereQuery |
||
| 279 | * @throws InvalidWhereQueryCriteriaException |
||
| 280 | */ |
||
| 281 | public function whereOld() { |
||
| 282 | return $this->where('OLD'); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param mixed $value |
||
| 287 | * |
||
| 288 | * @return WhereQuery |
||
| 289 | * @throws MessageSearchValidationException |
||
| 290 | * @throws InvalidWhereQueryCriteriaException |
||
| 291 | */ |
||
| 292 | public function whereOn($value) { |
||
| 293 | $date = $this->parse_date($value); |
||
| 294 | return $this->where('ON', $date); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return WhereQuery |
||
| 299 | * @throws InvalidWhereQueryCriteriaException |
||
| 300 | */ |
||
| 301 | public function whereRecent() { |
||
| 302 | return $this->where('RECENT'); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return WhereQuery |
||
| 307 | * @throws InvalidWhereQueryCriteriaException |
||
| 308 | */ |
||
| 309 | public function whereSeen() { |
||
| 310 | return $this->where('SEEN'); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param mixed $value |
||
| 315 | * |
||
| 316 | * @return WhereQuery |
||
| 317 | * @throws MessageSearchValidationException |
||
| 318 | * @throws InvalidWhereQueryCriteriaException |
||
| 319 | */ |
||
| 320 | public function whereSince($value) { |
||
| 321 | $date = $this->parse_date($value); |
||
| 322 | return $this->where('SINCE', $date); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $value |
||
| 327 | * |
||
| 328 | * @return WhereQuery |
||
| 329 | * @throws InvalidWhereQueryCriteriaException |
||
| 330 | */ |
||
| 331 | public function whereSubject($value) { |
||
| 332 | return $this->where('SUBJECT', $value); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $value |
||
| 337 | * |
||
| 338 | * @return WhereQuery |
||
| 339 | * @throws InvalidWhereQueryCriteriaException |
||
| 340 | */ |
||
| 341 | public function whereText($value) { |
||
| 342 | return $this->where('TEXT', $value); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $value |
||
| 347 | * |
||
| 348 | * @return WhereQuery |
||
| 349 | * @throws InvalidWhereQueryCriteriaException |
||
| 350 | */ |
||
| 351 | public function whereTo($value) { |
||
| 352 | return $this->where('TO', $value); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $value |
||
| 357 | * |
||
| 358 | * @return WhereQuery |
||
| 359 | * @throws InvalidWhereQueryCriteriaException |
||
| 360 | */ |
||
| 361 | public function whereUnkeyword($value) { |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return WhereQuery |
||
| 367 | * @throws InvalidWhereQueryCriteriaException |
||
| 368 | */ |
||
| 369 | public function whereUnanswered() { |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return WhereQuery |
||
| 375 | * @throws InvalidWhereQueryCriteriaException |
||
| 376 | */ |
||
| 377 | public function whereUndeleted() { |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return WhereQuery |
||
| 383 | * @throws InvalidWhereQueryCriteriaException |
||
| 384 | */ |
||
| 385 | public function whereUnflagged() { |
||
| 386 | return $this->where('UNFLAGGED'); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return WhereQuery |
||
| 391 | * @throws InvalidWhereQueryCriteriaException |
||
| 392 | */ |
||
| 393 | public function whereUnseen() { |
||
| 394 | return $this->where('UNSEEN'); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return WhereQuery |
||
| 399 | * @throws InvalidWhereQueryCriteriaException |
||
| 400 | */ |
||
| 401 | public function whereNoXSpam(){ |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return WhereQuery |
||
| 407 | * @throws InvalidWhereQueryCriteriaException |
||
| 408 | */ |
||
| 409 | public function whereIsXSpam(){ |
||
| 410 | return $this->where("X-Spam-Flag YES"); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param $country_code |
||
| 415 | * |
||
| 416 | * @return WhereQuery |
||
| 417 | * @throws InvalidWhereQueryCriteriaException |
||
| 418 | */ |
||
| 419 | public function whereLanguage($country_code){ |
||
| 421 | } |
||
| 422 | } |
||
| 423 |