| Conditions | 9 |
| Paths | 1 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 32 | public function registerMacro() |
||
| 33 | { |
||
| 34 | $macro = function ($first, $currentCursor = null) { |
||
| 35 | $queryOrders = isset($this->query) ? collect($this->query->orders) : collect($this->orders); |
||
| 36 | $cursor = $currentCursor instanceof Cursor ? $currentCursor : Cursor::decode($currentCursor); |
||
| 37 | |||
| 38 | if ($cursor->isPresent()) { |
||
| 39 | $apply = function ($query, $queryOrders, $cursor) use (&$apply) { |
||
| 40 | $query->where(function ($query) use ($queryOrders, $cursor, $apply) { |
||
| 41 | $order = array_shift($queryOrders); |
||
| 42 | $column = Arr::get($order, 'column'); |
||
| 43 | $direction = Arr::get($order, 'direction'); |
||
| 44 | |||
| 45 | $value = $cursor->get($column); |
||
| 46 | |||
| 47 | $query->where($column, $direction === 'asc' ? '>' : '<', $value); |
||
| 48 | |||
| 49 | if (!empty($queryOrders)) { |
||
| 50 | $query->orWhere($column, $value); |
||
| 51 | $apply($query, $queryOrders, $cursor); |
||
| 52 | } |
||
| 53 | }); |
||
| 54 | }; |
||
| 55 | |||
| 56 | $apply($this, $queryOrders->toArray(), collect($cursor->getAfterCursor())); |
||
| 57 | } |
||
| 58 | |||
| 59 | $items = $this->limit($first + 1)->get(); |
||
| 60 | |||
| 61 | $hasAfter = false; |
||
| 62 | // Check if there are items |
||
| 63 | if ($items->count() === 0) { |
||
| 64 | return new CursorPaginator($items, $currentCursor, $hasAfter); |
||
| 65 | } |
||
| 66 | // Check if there is a next page |
||
| 67 | if ($items->count() > $first) { |
||
| 68 | $items->pop(); |
||
| 69 | $hasAfter = true; |
||
| 70 | } |
||
| 71 | |||
| 72 | // Get last item and calculate afterCursor |
||
| 73 | $lastItem = $items->last(); |
||
| 74 | $afterCursor = $queryOrders->mapWithKeys(function ($item) use ($lastItem) { |
||
| 75 | $column = Arr::get($item, 'column'); |
||
| 76 | $value = $lastItem->{$column}; |
||
| 77 | if ($value instanceof Carbon) { |
||
| 78 | $value = $value->toIso8601String(); |
||
| 79 | } |
||
| 80 | return [$column => $value]; |
||
| 81 | })->toArray(); |
||
| 82 | $afterCursor = new Cursor($afterCursor); |
||
| 83 | |||
| 84 | return new CursorPaginator($items, $afterCursor, $hasAfter); |
||
| 85 | }; |
||
| 86 | |||
| 87 | // Register macros |
||
| 88 | QueryBuilder::macro('cursorPaginate', $macro); |
||
| 89 | EloquentBuilder::macro('cursorPaginate', $macro); |
||
| 90 | } |
||
| 91 | |||
| 106 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: