| Total Complexity | 40 | 
| Total Lines | 329 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
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  | 
            ||
| 51 | class WhereQuery extends Query { | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * @var array $available_criteria  | 
            ||
| 55 | */  | 
            ||
| 56 | protected $available_criteria = [  | 
            ||
| 57 | 'OR', 'AND',  | 
            ||
| 58 | 'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD',  | 
            ||
| 59 | 'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO',  | 
            ||
| 60 | 'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN'  | 
            ||
| 61 | ];  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Magic method in order to allow alias usage of all "where" methods  | 
            ||
| 65 | * @param string $name  | 
            ||
| 66 | * @param array|null $arguments  | 
            ||
| 67 | *  | 
            ||
| 68 | * @return mixed  | 
            ||
| 69 | * @throws MethodNotFoundException  | 
            ||
| 70 | */  | 
            ||
| 71 |     public function __call($name, $arguments) { | 
            ||
| 72 | $method = 'where'.ucfirst($name);  | 
            ||
| 73 |         if(method_exists($this, $method) === true){ | 
            ||
| 74 | return call_user_func_array([$this, $method], $arguments);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 75 | }  | 
            ||
| 76 | |||
| 77 | throw new MethodNotFoundException();  | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * Validate a given criteria  | 
            ||
| 82 | * @param $criteria  | 
            ||
| 83 | *  | 
            ||
| 84 | * @return string  | 
            ||
| 85 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 86 | */  | 
            ||
| 87 |     protected function validate_criteria($criteria) { | 
            ||
| 88 | $criteria = strtoupper($criteria);  | 
            ||
| 89 | |||
| 90 |         if(in_array($criteria, $this->available_criteria) === false) { | 
            ||
| 91 | throw new InvalidWhereQueryCriteriaException();  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | return $criteria;  | 
            ||
| 95 | }  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * @param mixed $criteria  | 
            ||
| 99 | * @param null $value  | 
            ||
| 100 | *  | 
            ||
| 101 | * @return $this  | 
            ||
| 102 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 103 | */  | 
            ||
| 104 |     public function where($criteria, $value = null) { | 
            ||
| 105 |         if(is_array($criteria)){ | 
            ||
| 106 |             foreach($criteria as $arguments){ | 
            ||
| 107 |                 if(count($arguments) == 1){ | 
            ||
| 108 | $this->where($arguments[0]);  | 
            ||
| 109 |                 }elseif(count($arguments) == 2){ | 
            ||
| 110 | $this->where($arguments[0], $arguments[1]);  | 
            ||
| 111 | }  | 
            ||
| 112 | }  | 
            ||
| 113 |         }else{ | 
            ||
| 114 | $criteria = $this->validate_criteria($criteria);  | 
            ||
| 115 | $value = $this->parse_value($value);  | 
            ||
| 116 | |||
| 117 |             if($value === null || $value === ''){ | 
            ||
| 118 | $this->query->push([$criteria]);  | 
            ||
| 119 |             }else{ | 
            ||
| 120 | $this->query->push([$criteria, $value]);  | 
            ||
| 121 | }  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | return $this;  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * @param \Closure $closure  | 
            ||
| 129 | *  | 
            ||
| 130 | * @return $this  | 
            ||
| 131 | */  | 
            ||
| 132 |     public function orWhere(\Closure $closure = null) { | 
            ||
| 137 | }  | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * @param \Closure $closure  | 
            ||
| 141 | *  | 
            ||
| 142 | * @return $this  | 
            ||
| 143 | */  | 
            ||
| 144 |     public function andWhere(\Closure $closure = null) { | 
            ||
| 145 | $this->query->push(['AND']);  | 
            ||
| 146 | if($closure !== null) $closure($this);  | 
            ||
| 147 | |||
| 148 | return $this;  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * @return WhereQuery  | 
            ||
| 153 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 154 | */  | 
            ||
| 155 |     public function whereAll() { | 
            ||
| 156 |         return $this->where('ALL'); | 
            ||
| 157 | }  | 
            ||
| 158 | |||
| 159 | /**  | 
            ||
| 160 | * @return WhereQuery  | 
            ||
| 161 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 162 | */  | 
            ||
| 163 |     public function whereAnswered() { | 
            ||
| 164 |         return $this->where('ANSWERED'); | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * @param string $value  | 
            ||
| 169 | *  | 
            ||
| 170 | * @return WhereQuery  | 
            ||
| 171 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 172 | */  | 
            ||
| 173 |     public function whereBcc($value) { | 
            ||
| 174 |         return $this->where('BCC', $value); | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * @param mixed $value  | 
            ||
| 179 | * @return WhereQuery  | 
            ||
| 180 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 181 | * @throws MessageSearchValidationException  | 
            ||
| 182 | */  | 
            ||
| 183 |     public function whereBefore($value) { | 
            ||
| 184 | $date = $this->parse_date($value);  | 
            ||
| 185 |         return $this->where('BEFORE', $date); | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | /**  | 
            ||
| 189 | * @param string $value  | 
            ||
| 190 | *  | 
            ||
| 191 | * @return WhereQuery  | 
            ||
| 192 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 193 | */  | 
            ||
| 194 |     public function whereBody($value) { | 
            ||
| 195 |         return $this->where('BODY', $value); | 
            ||
| 196 | }  | 
            ||
| 197 | |||
| 198 | /**  | 
            ||
| 199 | * @param string $value  | 
            ||
| 200 | *  | 
            ||
| 201 | * @return WhereQuery  | 
            ||
| 202 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 203 | */  | 
            ||
| 204 |     public function whereCc($value) { | 
            ||
| 205 |         return $this->where('CC', $value); | 
            ||
| 206 | }  | 
            ||
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * @return WhereQuery  | 
            ||
| 210 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 211 | */  | 
            ||
| 212 |     public function whereDeleted() { | 
            ||
| 213 |         return $this->where('DELETED'); | 
            ||
| 214 | }  | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * @param string $value  | 
            ||
| 218 | *  | 
            ||
| 219 | * @return WhereQuery  | 
            ||
| 220 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 221 | */  | 
            ||
| 222 |     public function whereFlagged($value) { | 
            ||
| 223 |         return $this->where('FLAGGED', $value); | 
            ||
| 224 | }  | 
            ||
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * @param string $value  | 
            ||
| 228 | *  | 
            ||
| 229 | * @return WhereQuery  | 
            ||
| 230 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 231 | */  | 
            ||
| 232 |     public function whereFrom($value) { | 
            ||
| 234 | }  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * @param string $value  | 
            ||
| 238 | *  | 
            ||
| 239 | * @return WhereQuery  | 
            ||
| 240 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 241 | */  | 
            ||
| 242 |     public function whereKeyword($value) { | 
            ||
| 243 |         return $this->where('KEYWORD', $value); | 
            ||
| 244 | }  | 
            ||
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * @return WhereQuery  | 
            ||
| 248 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 249 | */  | 
            ||
| 250 |     public function whereNew() { | 
            ||
| 252 | }  | 
            ||
| 253 | |||
| 254 | /**  | 
            ||
| 255 | * @return WhereQuery  | 
            ||
| 256 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 257 | */  | 
            ||
| 258 |     public function whereNot() { | 
            ||
| 259 |         return $this->where('NOT'); | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * @return WhereQuery  | 
            ||
| 264 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 265 | */  | 
            ||
| 266 |     public function whereOld() { | 
            ||
| 267 |         return $this->where('OLD'); | 
            ||
| 268 | }  | 
            ||
| 269 | |||
| 270 | /**  | 
            ||
| 271 | * @param mixed $value  | 
            ||
| 272 | *  | 
            ||
| 273 | * @return WhereQuery  | 
            ||
| 274 | * @throws MessageSearchValidationException  | 
            ||
| 275 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 276 | */  | 
            ||
| 277 |     public function whereOn($value) { | 
            ||
| 278 | $date = $this->parse_date($value);  | 
            ||
| 279 |         return $this->where('ON', $date); | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * @return WhereQuery  | 
            ||
| 284 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 285 | */  | 
            ||
| 286 |     public function whereRecent() { | 
            ||
| 287 |         return $this->where('RECENT'); | 
            ||
| 288 | }  | 
            ||
| 289 | |||
| 290 | /**  | 
            ||
| 291 | * @return WhereQuery  | 
            ||
| 292 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 293 | */  | 
            ||
| 294 |     public function whereSeen() { | 
            ||
| 295 |         return $this->where('SEEN'); | 
            ||
| 296 | }  | 
            ||
| 297 | |||
| 298 | /**  | 
            ||
| 299 | * @param mixed $value  | 
            ||
| 300 | *  | 
            ||
| 301 | * @return WhereQuery  | 
            ||
| 302 | * @throws MessageSearchValidationException  | 
            ||
| 303 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 304 | */  | 
            ||
| 305 |     public function whereSince($value) { | 
            ||
| 306 | $date = $this->parse_date($value);  | 
            ||
| 307 |         return $this->where('SINCE', $date); | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * @param string $value  | 
            ||
| 312 | *  | 
            ||
| 313 | * @return WhereQuery  | 
            ||
| 314 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 315 | */  | 
            ||
| 316 |     public function whereSubject($value) { | 
            ||
| 317 |         return $this->where('SUBJECT', $value); | 
            ||
| 318 | }  | 
            ||
| 319 | |||
| 320 | /**  | 
            ||
| 321 | * @param string $value  | 
            ||
| 322 | *  | 
            ||
| 323 | * @return WhereQuery  | 
            ||
| 324 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 325 | */  | 
            ||
| 326 |     public function whereText($value) { | 
            ||
| 327 |         return $this->where('TEXT', $value); | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 | * @param string $value  | 
            ||
| 332 | *  | 
            ||
| 333 | * @return WhereQuery  | 
            ||
| 334 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 335 | */  | 
            ||
| 336 |     public function whereTo($value) { | 
            ||
| 337 |         return $this->where('TO', $value); | 
            ||
| 338 | }  | 
            ||
| 339 | |||
| 340 | /**  | 
            ||
| 341 | * @param string $value  | 
            ||
| 342 | *  | 
            ||
| 343 | * @return WhereQuery  | 
            ||
| 344 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 345 | */  | 
            ||
| 346 |     public function whereUnkeyword($value) { | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * @return WhereQuery  | 
            ||
| 352 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 353 | */  | 
            ||
| 354 |     public function whereUnanswered() { | 
            ||
| 356 | }  | 
            ||
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * @return WhereQuery  | 
            ||
| 360 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 361 | */  | 
            ||
| 362 |     public function whereUndeleted() { | 
            ||
| 364 | }  | 
            ||
| 365 | |||
| 366 | /**  | 
            ||
| 367 | * @return WhereQuery  | 
            ||
| 368 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 369 | */  | 
            ||
| 370 |     public function whereUnflagged() { | 
            ||
| 372 | }  | 
            ||
| 373 | |||
| 374 | /**  | 
            ||
| 375 | * @return WhereQuery  | 
            ||
| 376 | * @throws InvalidWhereQueryCriteriaException  | 
            ||
| 377 | */  | 
            ||
| 378 |     public function whereUnseen() { | 
            ||
| 380 | }  | 
            ||
| 381 | }  |