Complex classes like QueryBuilderEngine 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 QueryBuilderEngine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class QueryBuilderEngine extends BaseEngine |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 23 | * @param \Yajra\Datatables\Request $request |
||
| 24 | */ |
||
| 25 | public function __construct(Builder $builder, Request $request) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Initialize attributes. |
||
| 33 | * |
||
| 34 | * @param \Yajra\Datatables\Request $request |
||
| 35 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 36 | * @param string $type |
||
| 37 | */ |
||
| 38 | protected function init($request, $builder, $type = 'builder') |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | public function filter(Closure $callback) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @inheritdoc |
||
| 63 | */ |
||
| 64 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public function totalCount() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Counts current query. |
||
| 79 | * |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function count() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Perform global search. |
||
| 98 | * |
||
| 99 | * @return void |
||
| 100 | */ |
||
| 101 | public function filtering() |
||
| 102 | { |
||
| 103 | $this->query->where( |
||
| 104 | function ($query) { |
||
| 105 | $keyword = $this->setupKeyword($this->request->keyword()); |
||
| 106 | foreach ($this->request->searchableColumnIndex() as $index) { |
||
| 107 | $columnName = $this->getColumnName($index); |
||
| 108 | |||
| 109 | if (isset($this->columnDef['filter'][$columnName])) { |
||
| 110 | $method = Helper::getOrMethod($this->columnDef['filter'][$columnName]['method']); |
||
| 111 | $parameters = $this->columnDef['filter'][$columnName]['parameters']; |
||
| 112 | $this->compileColumnQuery( |
||
| 113 | $this->getQueryBuilder($query), |
||
| 114 | $method, |
||
| 115 | $parameters, |
||
| 116 | $columnName, |
||
| 117 | $keyword |
||
| 118 | ); |
||
| 119 | } else { |
||
| 120 | if (count(explode('.', $columnName)) > 1) { |
||
| 121 | $eagerLoads = $this->getEagerLoads(); |
||
| 122 | $parts = explode('.', $columnName); |
||
| 123 | $relationColumn = array_pop($parts); |
||
| 124 | $relation = implode('.', $parts); |
||
| 125 | if (in_array($relation, $eagerLoads)) { |
||
| 126 | $this->compileRelationSearch( |
||
| 127 | $this->getQueryBuilder($query), |
||
| 128 | $relation, |
||
| 129 | $relationColumn, |
||
| 130 | $keyword |
||
| 131 | ); |
||
| 132 | } else { |
||
| 133 | $this->compileGlobalSearch($this->getQueryBuilder($query), $columnName, $keyword); |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $this->compileGlobalSearch($this->getQueryBuilder($query), $columnName, $keyword); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->isFilterApplied = true; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Perform filter column on selected field. |
||
| 148 | * |
||
| 149 | * @param mixed $query |
||
| 150 | * @param string $method |
||
| 151 | * @param mixed $parameters |
||
| 152 | * @param string $column |
||
| 153 | * @param string $keyword |
||
| 154 | */ |
||
| 155 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 156 | { |
||
| 157 | if (method_exists($query, $method) |
||
| 158 | && count($parameters) <= with(new \ReflectionMethod($query, $method))->getNumberOfParameters() |
||
| 159 | ) { |
||
| 160 | if (Str::contains(Str::lower($method), 'raw') |
||
| 161 | || Str::contains(Str::lower($method), 'exists') |
||
| 162 | ) { |
||
| 163 | call_user_func_array( |
||
| 164 | [$query, $method], |
||
| 165 | $this->parameterize($parameters, $keyword) |
||
| 166 | ); |
||
| 167 | } else { |
||
| 168 | call_user_func_array( |
||
| 169 | [$query, $method], |
||
| 170 | $this->parameterize($column, $parameters, $keyword) |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Build Query Builder Parameters. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function parameterize() |
||
| 182 | { |
||
| 183 | $args = func_get_args(); |
||
| 184 | $keyword = count($args) > 2 ? $args[2] : $args[1]; |
||
| 185 | $parameters = Helper::buildParameters($args); |
||
| 186 | $parameters = Helper::replacePatternWithKeyword($parameters, $keyword, '$1'); |
||
| 187 | |||
| 188 | return $parameters; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get eager loads keys if eloquent. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function getEagerLoads() |
||
| 197 | { |
||
| 198 | if ($this->query_type == 'eloquent') { |
||
| 199 | return array_keys($this->query->getEagerLoads()); |
||
| 200 | } |
||
| 201 | |||
| 202 | return []; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Add relation query on global search. |
||
| 207 | * |
||
| 208 | * @param mixed $query |
||
| 209 | * @param string $relation |
||
| 210 | * @param string $column |
||
| 211 | * @param string $keyword |
||
| 212 | */ |
||
| 213 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 214 | { |
||
| 215 | $myQuery = clone $this->query; |
||
| 216 | $myQuery->orWhereHas($relation, function ($q) use ($column, $keyword, $query) { |
||
| 217 | $sql = $q->select($this->connection->raw('count(1)')) |
||
| 218 | ->where($column, 'like', $keyword) |
||
| 219 | ->toSql(); |
||
| 220 | $sql = "($sql) >= 1"; |
||
| 221 | $query->orWhereRaw($sql, [$keyword]); |
||
| 222 | }); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add a query on global search. |
||
| 227 | * |
||
| 228 | * @param mixed $query |
||
| 229 | * @param string $column |
||
| 230 | * @param string $keyword |
||
| 231 | */ |
||
| 232 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Wrap a column and cast in pgsql. |
||
| 246 | * |
||
| 247 | * @param string $column |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function castColumn($column) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Perform column search. |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | public function columnSearch() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get proper keyword to use for search. |
||
| 294 | * |
||
| 295 | * @param int $i |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | private function getSearchKeyword($i) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Compile queries for column search. |
||
| 309 | * |
||
| 310 | * @param int $i |
||
| 311 | * @param mixed $column |
||
| 312 | * @param string $keyword |
||
| 313 | * @param bool $caseSensitive |
||
| 314 | */ |
||
| 315 | protected function compileColumnSearch($i, $column, $keyword, $caseSensitive = true) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Compile regex query column search. |
||
| 328 | * |
||
| 329 | * @param mixed $column |
||
| 330 | * @param string $keyword |
||
| 331 | * @param bool $caseSensitive |
||
| 332 | */ |
||
| 333 | protected function regexColumnSearch($column, $keyword, $caseSensitive = true) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check if the current sql language is based on oracle syntax. |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | protected function isOracleSql() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Perform sorting of columns. |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | public function ordering() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Join eager loaded relation and get the related column name. |
||
| 398 | * |
||
| 399 | * @param string $relation |
||
| 400 | * @param string $relationColumn |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function joinEagerLoadedColumn($relation, $relationColumn) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Perform pagination |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function paging() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get results |
||
| 436 | * |
||
| 437 | * @return array|static[] |
||
| 438 | */ |
||
| 439 | public function results() |
||
| 443 | } |
||
| 444 |