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 |
||
| 18 | class CollectionEngine extends BaseEngine |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Collection object |
||
| 22 | * |
||
| 23 | * @var \Illuminate\Support\Collection |
||
| 24 | */ |
||
| 25 | public $collection; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Collection object |
||
| 29 | * |
||
| 30 | * @var \Illuminate\Support\Collection |
||
| 31 | */ |
||
| 32 | public $original_collection; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * CollectionEngine constructor. |
||
| 36 | * |
||
| 37 | * @param \Illuminate\Support\Collection $collection |
||
| 38 | * @param \Yajra\Datatables\Request $request |
||
| 39 | */ |
||
| 40 | public function __construct(Collection $collection, Request $request) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Serialize collection |
||
| 50 | * |
||
| 51 | * @param mixed $collection |
||
| 52 | * @return mixed|null |
||
| 53 | */ |
||
| 54 | protected function serialize($collection) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set auto filter off and run your own filter. |
||
| 61 | * Overrides global search. |
||
| 62 | * |
||
| 63 | * @param \Closure $callback |
||
| 64 | * @param bool $globalSearch |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function filter(Closure $callback, $globalSearch = false) |
||
| 68 | { |
||
| 69 | $this->overrideGlobalSearch($callback, $this, $globalSearch); |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Append debug parameters on output. |
||
| 76 | * |
||
| 77 | * @param array $output |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function showDebugger(array $output) |
||
| 81 | { |
||
| 82 | $output["input"] = $this->request->all(); |
||
|
|
|||
| 83 | |||
| 84 | return $output; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Count total items. |
||
| 89 | * |
||
| 90 | * @return integer |
||
| 91 | */ |
||
| 92 | public function totalCount() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Count results. |
||
| 99 | * |
||
| 100 | * @return integer |
||
| 101 | */ |
||
| 102 | public function count() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Perform sorting of columns. |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function ordering() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Perform global search. |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public function filtering() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Perform multi-term search by splitting keyword into |
||
| 160 | * individual words and searches for each of them. |
||
| 161 | * |
||
| 162 | * @param string $keyword |
||
| 163 | */ |
||
| 164 | private function smartGlobalSearch($keyword) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Perform global search for the given keyword. |
||
| 175 | * |
||
| 176 | * @param string $keyword |
||
| 177 | */ |
||
| 178 | private function globalSearch($keyword) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Perform column search. |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function columnSearch() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Perform pagination. |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function paging() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get results. |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function results() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Organizes works. |
||
| 280 | * |
||
| 281 | * @param bool $mDataSupport |
||
| 282 | * @param bool $orderFirst |
||
| 283 | * @return \Illuminate\Http\JsonResponse |
||
| 284 | */ |
||
| 285 | public function make($mDataSupport = false, $orderFirst = true) |
||
| 289 | } |
||
| 290 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: