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:
| 1 | <?php |
||
| 22 | class QueryBuilderProvider implements Provider |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var QueryBuilder The original query, as passed to us. |
||
| 26 | */ |
||
| 27 | private $originalQuery; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var QueryBuilder The query, before limits are applied |
||
| 31 | */ |
||
| 32 | private $queryBeforeLimits; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var QueryBuilder The underlying query |
||
| 36 | */ |
||
| 37 | private $query; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var QueryConfiguration |
||
| 41 | */ |
||
| 42 | private $queryConfiguration; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var callable the default global function to check if a row should be included |
||
| 46 | */ |
||
| 47 | private $defaultGlobalSearchFunction; |
||
|
|
|||
| 48 | |||
| 49 | /** |
||
| 50 | * @var callable the default global order function |
||
| 51 | */ |
||
| 52 | private $defaultGlobalOrderFunction; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array an array of callables with local search functions to check if the row should be included |
||
| 56 | */ |
||
| 57 | private $columnSearchFunction = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array an array that will hold the search functions for each column |
||
| 61 | */ |
||
| 62 | private $columnConfiguration = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * CollectionProvider constructor. |
||
| 66 | * @param QueryBuilder $query The query to base the built query on |
||
| 67 | */ |
||
| 68 | public function __construct(QueryBuilder $query) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Here the DTQueryConfiguration is passed to prepare the provider for the processing of the request. |
||
| 79 | * This will only be called when the DTProvider needs to handle the request. |
||
| 80 | * It will never be called when the DTProvider does not need to handle the request. |
||
| 81 | * |
||
| 82 | * @param QueryConfiguration $queryConfiguration |
||
| 83 | * @param ColumnConfiguration[] $columnConfiguration |
||
| 84 | * @return mixed |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function prepareForProcessing(QueryConfiguration $queryConfiguration, array $columnConfiguration) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * This method should process all configurations and prepare the underlying data for the view. It will arrange the |
||
| 106 | * data and provide the results in a DTData object. |
||
| 107 | * It will be called after {@link #prepareForProcessing} has been called and needs to return the processed data in |
||
| 108 | * a DTData object so the Composer can further handle the data. |
||
| 109 | * |
||
| 110 | * @return ResponseData The processed data |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | public function process() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Will compile the collection into the final collection where operations like search and order can be applied. |
||
| 150 | * |
||
| 151 | * @param QueryBuilder $query |
||
| 152 | * @param ColumnConfiguration[] $columnConfiguration |
||
| 153 | * @return QueryBuilder |
||
| 154 | * @throws DatatableException |
||
| 155 | */ |
||
| 156 | private function compileQuery(QueryBuilder $query, array $columnConfiguration) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * When a global (single) search has been done against data in the datatable. |
||
| 169 | * |
||
| 170 | * @param QueryBuilder $query |
||
| 171 | * @param array $columnConfiguration |
||
| 172 | * @return QueryBuilder |
||
| 173 | * @throws DatatableException |
||
| 174 | */ |
||
| 175 | private function compileGlobalQuery(QueryBuilder $query, array $columnConfiguration) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * When a global query is being performed (ie, a query against a single column) |
||
| 192 | * |
||
| 193 | * @param QueryBuilder $query |
||
| 194 | * @param ColumnConfiguration[] $columnConfiguration |
||
| 195 | * @return QueryBuilder |
||
| 196 | * @throws DatatableException |
||
| 197 | */ |
||
| 198 | private function compileColumnQuery(QueryBuilder $query, array $columnConfiguration) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param ColumnConfiguration[] $columnConfiguration |
||
| 221 | * @param string $name |
||
| 222 | * @return ColumnConfiguration |
||
| 223 | */ |
||
| 224 | private function getColumnFromName($columnConfiguration, $name) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * When a global query is being performed (ie, a query against |
||
| 234 | */ |
||
| 235 | private function compileColumnNames() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Will accept a search function that should be called for the column with the given name. |
||
| 247 | * If the function returns true, it will be accepted as search matching |
||
| 248 | * |
||
| 249 | * @param string $columnName the name of the column to pass this function to |
||
| 250 | * @param callable $searchFunction the function for the searching |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | public function searchColumn($columnName, callable $searchFunction) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Will accept a global search function for all columns. |
||
| 262 | * @param callable $searchFunction the search function to determine if a row should be included |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function search(callable $searchFunction) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Will accept a global search function for all columns. |
||
| 272 | * @param callable $orderFunction the order function to determine the order of the table |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function order(callable $orderFunction) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Will sort the query based on the given datatable query configuration. |
||
| 282 | */ |
||
| 283 | private function sortQuery() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Will limit a query hased on the start and length given |
||
| 296 | */ |
||
| 297 | private function limitQuery() |
||
| 302 | |||
| 303 | public function setupSearch() |
||
| 306 | |||
| 307 | public function setupOrder() |
||
| 310 | } |
This check marks private properties in classes that are never used. Those properties can be removed.