| Conditions | 7 |
| Paths | 15 |
| Total Lines | 59 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 23 | public function paginate(mixed $target, array $params = [], array $settings = []): PaginatedInterface |
||
| 24 | { |
||
| 25 | $query = null; |
||
| 26 | if ($target instanceof QueryInterface) { |
||
| 27 | $query = $target; |
||
| 28 | $target = $query->getRepository(); |
||
| 29 | if ($target === null) { |
||
| 30 | throw new Exception('No repository set for query.'); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | if ($query instanceof Query) { |
||
| 35 | throw new InvalidArgumentException(Query::class . ' cannot be paginated by ' . __METHOD__ . '()'); |
||
| 36 | } |
||
| 37 | |||
| 38 | $alias = $target->getAlias(); |
||
| 39 | $defaults = $this->getDefaults($alias, $settings); |
||
| 40 | |||
| 41 | $validSettings = [ |
||
| 42 | ...array_keys($this->_defaultConfig), |
||
| 43 | 'order', |
||
| 44 | 'forward', |
||
| 45 | 'backward', |
||
| 46 | 'exclusive', |
||
| 47 | 'inclusive', |
||
| 48 | 'seekable', |
||
| 49 | 'unseekable', |
||
| 50 | 'cursor', |
||
| 51 | ]; |
||
| 52 | $extraSettings = array_diff_key($defaults, array_flip($validSettings)); |
||
| 53 | if ($extraSettings) { |
||
| 54 | triggerWarning( |
||
| 55 | 'Passing query options as paginator settings is no longer supported.' |
||
| 56 | . ' Use a custom finder through the `finder` config or pass a Query instance to paginate().' |
||
| 57 | . ' Extra keys found are: `' . implode('`, `', array_keys($extraSettings)) . '`.' |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | $options = $this->mergeOptions($params, $defaults); |
||
| 62 | $options = $this->validateSort($target, $options); |
||
| 63 | $options = $this->checkLimit($options); |
||
| 64 | |||
| 65 | $options += ['cursor' => [], 'scope' => null]; |
||
| 66 | |||
| 67 | if ($query === null) { |
||
| 68 | $args = []; |
||
| 69 | $type = $options['finder'] ?? 'all'; |
||
| 70 | if (is_array($type)) { |
||
| 71 | $args = (array)current($type); |
||
| 72 | $type = key($type); |
||
| 73 | } |
||
| 74 | $query = $target->find($type, ...$args); |
||
| 75 | } |
||
| 76 | |||
| 77 | $query = Query::fromQuery($query->applyOptions($options)); |
||
| 78 | $query->fromArray($options); |
||
| 79 | $query->cursor($options['cursor']); |
||
| 80 | |||
| 81 | return $query->paginate(); |
||
| 82 | } |
||
| 84 |