| Conditions | 12 |
| Paths | 300 |
| Total Lines | 45 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 33 | public function getList(array $filters = [], string $sortKey = 'id', string $sortType = 'ASC', int $limit = self::LIMIT, $lastId = null, $firstId = null): ResultSet |
||
| 34 | { |
||
| 35 | $sortKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $sortKey)))); |
||
| 36 | |||
| 37 | $qb = $this->documentRepository->createQueryBuilder(); |
||
| 38 | $qb |
||
| 39 | ->sort($sortKey, $sortType) |
||
| 40 | ->limit($limit + 1); // Fetch one more than required for pagination. |
||
| 41 | |||
| 42 | $direction = 'DESC' === $sortType ? '<' : '>'; |
||
| 43 | $firstDirection = 'DESC' !== $sortType ? '<' : '>'; |
||
| 44 | |||
| 45 | if ($firstId) { |
||
| 46 | $sortType = ('DESC' === $sortType) ? 'ASC' : 'DESC'; |
||
| 47 | } |
||
| 48 | |||
| 49 | $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey); |
||
| 50 | $dm = $this->documentRepository->getDocumentManager(); |
||
| 51 | $columns = $dm->getClassMetadata($this->documentRepository->getClassName())->getFieldNames(); |
||
| 52 | |||
| 53 | if (!in_array($sortKey, $columns)) { |
||
| 54 | throw new \InvalidArgumentException("Sort key doesn't exist"); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($lastId) { |
||
| 58 | $qb->where($sortKey.' '.$direction.' '.$lastId); |
||
| 59 | } |
||
| 60 | if ($firstId) { |
||
| 61 | $qb->where($sortKey.' '.$firstDirection.' '.$firstId); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (is_a($this->documentRepository->getClassName(), DeletableInterface::class, true)) { |
||
| 65 | $qb->field('isDeleted')->equals(false); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($filters as $filter) { |
||
| 69 | /** @var DoctrineFilterInterface $filter */ |
||
| 70 | if ($filter instanceof OdmFilterInterface && $filter->hasData()) { |
||
| 71 | $filter->modifiyOdmQueryBuilder($qb); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $query = $qb->getQuery(); |
||
| 76 | |||
| 77 | return new ResultSet($query->toArray(), $sortKey, $sortType, $limit); |
||
| 78 | } |
||
| 118 |