Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 20 | class QueryBuilderEngine extends BaseEngine implements DataTableEngineContract |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 24 | * @param \Yajra\Datatables\Request $request |
||
| 25 | */ |
||
| 26 | public function __construct(Builder $builder, Request $request) |
||
| 27 | { |
||
| 28 | $this->query = $builder; |
||
| 29 | $this->init($request, $builder); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Initialize attributes. |
||
| 34 | * |
||
| 35 | * @param \Yajra\Datatables\Request $request |
||
| 36 | * @param \Illuminate\Database\Query\Builder $builder |
||
| 37 | * @param string $type |
||
| 38 | */ |
||
| 39 | protected function init($request, $builder, $type = 'builder') |
||
|
|
|||
| 40 | { |
||
| 41 | $this->request = $request; |
||
| 42 | $this->query_type = $type; |
||
| 43 | $this->columns = $builder->columns; |
||
| 44 | $this->connection = $builder->getConnection(); |
||
| 45 | $this->prefix = $this->connection->getTablePrefix(); |
||
| 46 | $this->database = $this->connection->getDriverName(); |
||
| 47 | if ($this->isDebugging()) { |
||
| 48 | $this->connection->enableQueryLog(); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | public function filter(Closure $callback) |
||
| 56 | { |
||
| 57 | $this->overrideGlobalSearch($callback, $this->query); |
||
| 58 | |||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritdoc |
||
| 64 | */ |
||
| 65 | public function make($mDataSupport = false, $orderFirst = false) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | public function totalCount() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Counts current query. |
||
| 80 | * |
||
| 81 | * @return int |
||
| 82 | */ |
||
| 83 | public function count() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Perform global search. |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function filtering() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get eager loads keys if eloquent. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | private function getEagerLoads() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Perform filter column on selected field. |
||
| 164 | * |
||
| 165 | * @param mixed $query |
||
| 166 | * @param string $method |
||
| 167 | * @param mixed $parameters |
||
| 168 | * @param string $column |
||
| 169 | * @param string $keyword |
||
| 170 | */ |
||
| 171 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Build Query Builder Parameters. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function parameterize() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add relation query on global search. |
||
| 209 | * |
||
| 210 | * @param mixed $query |
||
| 211 | * @param string $relation |
||
| 212 | * @param string $column |
||
| 213 | * @param string $keyword |
||
| 214 | */ |
||
| 215 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Add a query on global search. |
||
| 228 | * |
||
| 229 | * @param mixed $query |
||
| 230 | * @param string $column |
||
| 231 | * @param string $keyword |
||
| 232 | */ |
||
| 233 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Wrap a column and cast in pgsql. |
||
| 247 | * |
||
| 248 | * @param string $column |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function castColumn($column) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Perform column search. |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function columnSearch() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get proper keyword to use for search. |
||
| 295 | * |
||
| 296 | * @param int $i |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | private function getSearchKeyword($i) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Perform case insensitive column search. |
||
| 310 | * |
||
| 311 | * @param int $i |
||
| 312 | * @param mixed $column |
||
| 313 | * @param string $keyword |
||
| 314 | */ |
||
| 315 | protected function caseInsensitiveColumnSearch($i, $column, $keyword) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Perform regex case insensitive column search. |
||
| 326 | * |
||
| 327 | * @param mixed $column |
||
| 328 | * @param string $keyword |
||
| 329 | */ |
||
| 330 | View Code Duplication | protected function regexCaseInsensitiveColumnSearch($column, $keyword) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Perform case sensitive column search. |
||
| 341 | * |
||
| 342 | * @param int $i |
||
| 343 | * @param mixed $column |
||
| 344 | * @param string $keyword |
||
| 345 | */ |
||
| 346 | protected function caseSensitiveColumnSearch($i, $column, $keyword) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Perform regex case insensitive column search. |
||
| 357 | * @param mixed $column |
||
| 358 | * @param string $keyword |
||
| 359 | */ |
||
| 360 | View Code Duplication | protected function regexCaseSensitiveColumnSearch($column, $keyword) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @inheritdoc |
||
| 371 | */ |
||
| 372 | public function ordering() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get order by column name. |
||
| 405 | * |
||
| 406 | * @param array $orderable |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | private function setupOrderColumn(array $orderable) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @inheritdoc |
||
| 424 | */ |
||
| 425 | public function paging() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get results |
||
| 433 | * |
||
| 434 | * @return array|static[] |
||
| 435 | */ |
||
| 436 | public function results() |
||
| 440 | } |
||
| 441 |