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() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get eager loads keys if eloquent. |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | private function getEagerLoads() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Perform filter column on selected field. |
||
| 163 | * |
||
| 164 | * @param mixed $query |
||
| 165 | * @param string $method |
||
| 166 | * @param mixed $parameters |
||
| 167 | * @param string $column |
||
| 168 | * @param string $keyword |
||
| 169 | */ |
||
| 170 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Build Query Builder Parameters. |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function parameterize() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add relation query on global search. |
||
| 208 | * |
||
| 209 | * @param mixed $query |
||
| 210 | * @param string $relation |
||
| 211 | * @param string $column |
||
| 212 | * @param string $keyword |
||
| 213 | */ |
||
| 214 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 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() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Perform pagination |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function paging() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get results |
||
| 394 | * |
||
| 395 | * @return array|static[] |
||
| 396 | */ |
||
| 397 | public function results() |
||
| 401 | } |
||
| 402 |