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 | * @inheritdoc |
||
| 99 | */ |
||
| 100 | public function filtering() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get eager loads keys if eloquent. |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | private function getEagerLoads() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Perform filter column on selected field. |
||
| 158 | * |
||
| 159 | * @param mixed $query |
||
| 160 | * @param string $method |
||
| 161 | * @param mixed $parameters |
||
| 162 | * @param string $column |
||
| 163 | * @param string $keyword |
||
| 164 | */ |
||
| 165 | protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Build Query Builder Parameters. |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | protected function parameterize() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add relation query on global search. |
||
| 203 | * |
||
| 204 | * @param mixed $query |
||
| 205 | * @param string $column |
||
| 206 | * @param string $keyword |
||
| 207 | */ |
||
| 208 | protected function compileRelationSearch($query, $relation, $column, $keyword) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add a query on global search. |
||
| 221 | * |
||
| 222 | * @param mixed $query |
||
| 223 | * @param string $column |
||
| 224 | * @param string $keyword |
||
| 225 | */ |
||
| 226 | protected function compileGlobalSearch($query, $column, $keyword) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Wrap a column and cast in pgsql. |
||
| 240 | * |
||
| 241 | * @param string $column |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function castColumn($column) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | public function columnSearch() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get proper keyword to use for search. |
||
| 302 | * |
||
| 303 | * @param int $i |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | private function getSearchKeyword($i) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @inheritdoc |
||
| 317 | */ |
||
| 318 | public function ordering() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get order by column name. |
||
| 351 | * |
||
| 352 | * @param array $orderable |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | private function setupOrderColumn(array $orderable) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @inheritdoc |
||
| 370 | */ |
||
| 371 | public function paging() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get results |
||
| 379 | * |
||
| 380 | * @return array|static[] |
||
| 381 | */ |
||
| 382 | public function results() |
||
| 386 | } |
||
| 387 |