| Total Complexity | 42 | 
| Total Lines | 338 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 25 | class QueryBuilder  | 
            ||
| 26 | { | 
            ||
| 27 | /**  | 
            ||
| 28 | * @var array  | 
            ||
| 29 | */  | 
            ||
| 30 | private $criteria;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @var array  | 
            ||
| 34 | */  | 
            ||
| 35 | private $sortedBy;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @var array  | 
            ||
| 39 | */  | 
            ||
| 40 | private $limit;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * @var array  | 
            ||
| 44 | */  | 
            ||
| 45 | private $join;  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * @var array  | 
            ||
| 49 | */  | 
            ||
| 50 | private $array;  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * QueryBuilder constructor.  | 
            ||
| 54 | *  | 
            ||
| 55 | * @param array $array  | 
            ||
| 56 | *  | 
            ||
| 57 | * @throws NotConsistentDataException  | 
            ||
| 58 | */  | 
            ||
| 59 | public function __construct(array $array)  | 
            ||
| 62 | }  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * @param array $array  | 
            ||
| 66 | *  | 
            ||
| 67 | * @return static  | 
            ||
| 68 | * @throws NotConsistentDataException  | 
            ||
| 69 | */  | 
            ||
| 70 | public static function create(array $array)  | 
            ||
| 71 |     { | 
            ||
| 72 | return new static($array);  | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * @param array $array  | 
            ||
| 77 | *  | 
            ||
| 78 | * @return array  | 
            ||
| 79 | * @throws NotConsistentDataException  | 
            ||
| 80 | */  | 
            ||
| 81 | private function setArray(array $array)  | 
            ||
| 82 |     { | 
            ||
| 83 |         if (empty($array)) { | 
            ||
| 84 | return [];  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 |         if (false === ConsistencyChecker::isValid($array)) { | 
            ||
| 88 |             throw new NotConsistentDataException('Array provided has no consistent data.'); | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 | $this->array = $array;  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * @param $element  | 
            ||
| 96 | * @param null $key  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 97 | * @throws NotConsistentDataException  | 
            ||
| 98 | */  | 
            ||
| 99 | public function addElement($element, $key = null)  | 
            ||
| 100 |     { | 
            ||
| 101 |         if (false === ConsistencyChecker::isElementValid($element, $this->array)) { | 
            ||
| 102 |             throw new NotConsistentDataException('Element provided has no consistent data.'); | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | $this->array[$key] = $element;  | 
            ||
| 106 | }  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * @param $key  | 
            ||
| 110 | * @throws NotExistingElementException  | 
            ||
| 111 | */  | 
            ||
| 112 | public function removeElement($key)  | 
            ||
| 113 |     { | 
            ||
| 114 |         if(!isset($this->array[$key])){ | 
            ||
| 115 |             throw new NotExistingElementException(sprintf('Element with key %s does not exists.', $key)); | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 | unset($this->array[$key]);  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | /**  | 
            ||
| 122 | * @param $key  | 
            ||
| 123 | * @param $value  | 
            ||
| 124 | * @param string $operator  | 
            ||
| 125 | * @param null $dateFormat  | 
            ||
| 126 | * @return $this  | 
            ||
| 127 | * @throws NotValidCriterionOperatorException  | 
            ||
| 128 | */  | 
            ||
| 129 | public function addCriterion($key, $value, $operator = '=', $dateFormat = null)  | 
            ||
| 130 |     { | 
            ||
| 131 |         if (!$this->isAValidCriterionOperator($operator)) { | 
            ||
| 132 | throw new NotValidCriterionOperatorException($operator.' is not a valid operator.');  | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | $this->criteria[] = [  | 
            ||
| 136 | 'key' => $key,  | 
            ||
| 137 | 'value' => $value,  | 
            ||
| 138 | 'operator' => $operator,  | 
            ||
| 139 | 'date_format' => $dateFormat  | 
            ||
| 140 | ];  | 
            ||
| 141 | |||
| 142 | return $this;  | 
            ||
| 143 | }  | 
            ||
| 144 | |||
| 145 | /**  | 
            ||
| 146 | * @param $operator  | 
            ||
| 147 | * @return bool  | 
            ||
| 148 | */  | 
            ||
| 149 | private function isAValidCriterionOperator($operator)  | 
            ||
| 150 |     { | 
            ||
| 151 | return in_array($operator, array_keys(CriterionFilter::$operatorsMap));  | 
            ||
| 152 | }  | 
            ||
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * @param $key  | 
            ||
| 156 | * @param string $operator  | 
            ||
| 157 | *  | 
            ||
| 158 | * @return $this  | 
            ||
| 159 | *  | 
            ||
| 160 | * @throws NotValidSortingOperatorException  | 
            ||
| 161 | */  | 
            ||
| 162 | public function sortedBy($key, $operator = 'ASC', $format = null)  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * @param $operator  | 
            ||
| 179 | * @return bool  | 
            ||
| 180 | */  | 
            ||
| 181 | private function isAValidSortingOperator($operator)  | 
            ||
| 182 |     { | 
            ||
| 183 | return in_array($operator, SortingFilter::$operatorsMap);  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | /**  | 
            ||
| 187 | * @param $offset  | 
            ||
| 188 | * @param $length  | 
            ||
| 189 | * @return $this  | 
            ||
| 190 | * @throws NotValidLimitsOfArrayException  | 
            ||
| 191 | */  | 
            ||
| 192 | public function limit($offset, $length)  | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 | * @param $array  | 
            ||
| 220 | * @param $arrayName  | 
            ||
| 221 | * @param $parentKey  | 
            ||
| 222 | * @param $foreignKey  | 
            ||
| 223 | *  | 
            ||
| 224 | * @return $this  | 
            ||
| 225 | */  | 
            ||
| 226 | public function join($array, $arrayName, $parentKey, $foreignKey)  | 
            ||
| 227 |     { | 
            ||
| 228 | $this->join[] = [  | 
            ||
| 229 | 'array' => $array,  | 
            ||
| 230 | 'arrayName' => $arrayName,  | 
            ||
| 231 | 'parentKey' => $parentKey,  | 
            ||
| 232 | 'foreignKey' => $foreignKey,  | 
            ||
| 233 | ];  | 
            ||
| 234 | |||
| 235 | return $this;  | 
            ||
| 236 | }  | 
            ||
| 237 | |||
| 238 | /**  | 
            ||
| 239 | * @return array  | 
            ||
| 240 | */  | 
            ||
| 241 | public function getResults()  | 
            ||
| 242 |     { | 
            ||
| 243 |         if(empty($this->array)){ | 
            ||
| 244 | return [];  | 
            ||
| 245 | }  | 
            ||
| 246 | |||
| 247 | $results = $this->applySortingFilter($this->applyLimitFilter($this->applyCriteriaFilter($this->applyJoinFilter())));  | 
            ||
| 248 | |||
| 249 | return array_map([ArrayHelper::class, 'convertToPlainArray'], $results);  | 
            ||
| 250 | }  | 
            ||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * @return array  | 
            ||
| 254 | */  | 
            ||
| 255 | public function getResult($n)  | 
            ||
| 256 |     { | 
            ||
| 257 | return $this->getResults()[$n-1] ?: [];  | 
            ||
| 258 | }  | 
            ||
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * @return array  | 
            ||
| 262 | */  | 
            ||
| 263 | public function getFirstResult()  | 
            ||
| 264 |     { | 
            ||
| 265 | return $this->getResults()[0] ?: [];  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * @return array  | 
            ||
| 270 | */  | 
            ||
| 271 | public function getLastResult()  | 
            ||
| 272 |     { | 
            ||
| 273 | $count = count($this->getResults());  | 
            ||
| 274 | |||
| 275 | return $this->getResults()[$count-1] ?: [];  | 
            ||
| 276 | }  | 
            ||
| 277 | |||
| 278 | /**  | 
            ||
| 279 | * @return array  | 
            ||
| 280 | */  | 
            ||
| 281 | public function getShuffledResults()  | 
            ||
| 282 |     { | 
            ||
| 283 | $shuffledArray = [];  | 
            ||
| 284 | $keys = array_keys($this->getResults());  | 
            ||
| 285 | shuffle($keys);  | 
            ||
| 286 | |||
| 287 |         foreach ($keys as $key) { | 
            ||
| 288 | $shuffledArray[$key] = $this->getResults()[$key];  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | return $shuffledArray;  | 
            ||
| 292 | }  | 
            ||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * @param array $array  | 
            ||
| 296 | *  | 
            ||
| 297 | * @return array  | 
            ||
| 298 | */  | 
            ||
| 299 | private function applySortingFilter(array $array)  | 
            ||
| 300 |     { | 
            ||
| 301 | return SortingFilter::filter($array, $this->sortedBy);  | 
            ||
| 302 | }  | 
            ||
| 303 | |||
| 304 | /**  | 
            ||
| 305 | * @param array $array  | 
            ||
| 306 | *  | 
            ||
| 307 | * @return array  | 
            ||
| 308 | */  | 
            ||
| 309 | private function applyLimitFilter(array $array)  | 
            ||
| 310 |     { | 
            ||
| 311 | return LimitFilter::filter($array, $this->limit);  | 
            ||
| 312 | }  | 
            ||
| 313 | |||
| 314 | /**  | 
            ||
| 315 | * @return array  | 
            ||
| 316 | */  | 
            ||
| 317 | private function applyJoinFilter()  | 
            ||
| 318 |     { | 
            ||
| 319 | return JoinFilter::filter($this->array, $this->join);  | 
            ||
| 320 | }  | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * @return array  | 
            ||
| 324 | */  | 
            ||
| 325 | private function applyCriteriaFilter(array $array)  | 
            ||
| 326 |     { | 
            ||
| 327 |         if (empty($this->criteria) or count($this->criteria) === 0) { | 
            ||
| 328 | return $array;  | 
            ||
| 329 | }  | 
            ||
| 330 | |||
| 331 |         foreach ($this->criteria as $criterion) { | 
            ||
| 332 | $results = array_filter(  | 
            ||
| 333 |                 (isset($results)) ? $results : $array, function ($element) use ($criterion) { | 
            ||
| 334 | return CriterionFilter::filter($criterion, $element);  | 
            ||
| 335 | }  | 
            ||
| 336 | );  | 
            ||
| 337 | |||
| 338 |             $results = array_map(function ($result) use ($criterion) { | 
            ||
| 339 | $key = explode(Constants::ALIAS_DELIMITER, $criterion['key']);  | 
            ||
| 340 |                 if (count($key) > 1) { | 
            ||
| 341 | $oldkey = explode(Constants::ARRAY_SEPARATOR, $key[0]);  | 
            ||
| 342 | $newkey = $key[1];  | 
            ||
| 343 | |||
| 344 | $result = ArrayHelper::convertToPlainArray($result);  | 
            ||
| 345 | $oldResult = $result[$oldkey[0]];  | 
            ||
| 346 | |||
| 347 | $result[$newkey] = (count($oldkey) > 1) ? ArrayHelper::getValueFromNestedArray($oldkey, $oldResult) : $oldResult;  | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | return $result;  | 
            ||
| 351 | }, $results);  | 
            ||
| 352 | }  | 
            ||
| 353 | |||
| 354 | return $results;  | 
            ||
| 355 | }  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * @return int  | 
            ||
| 359 | */  | 
            ||
| 360 | public function getCount()  | 
            ||
| 363 | }  | 
            ||
| 364 | }  | 
            ||
| 365 |