| Conditions | 12 |
| Paths | 146 |
| Total Lines | 50 |
| Code Lines | 30 |
| 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 |
||
| 26 | public function getList(array $filters = [], string $sortKey = 'id', string $sortType = 'ASC', int $limit = self::LIMIT, $lastId = null): ResultSet |
||
| 27 | { |
||
| 28 | $sortKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $sortKey)))); |
||
| 29 | |||
| 30 | $parts = explode('\\', $this->entityRepository->getClassName()); |
||
| 31 | $name = end($parts); |
||
| 32 | $qb = $this->entityRepository->createQueryBuilder($name); |
||
| 33 | |||
| 34 | $direction = 'DESC' === $sortType ? '<' : '>'; |
||
| 35 | $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey); |
||
| 36 | $em = $this->entityRepository->getEntityManager(); |
||
| 37 | $columns = $em->getClassMetadata($this->entityRepository->getClassName())->getColumnNames(); |
||
| 38 | $columns = array_map(function ($colName) { |
||
| 39 | return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $colName)))); |
||
| 40 | }, $columns); |
||
| 41 | |||
| 42 | if (!in_array($sortKey, $columns)) { |
||
| 43 | throw new GeneralException("Sort key doesn't exist"); |
||
| 44 | } |
||
| 45 | |||
| 46 | $qb |
||
| 47 | ->orderBy($qb->getRootAliases()[0].'.'.$sortKey, $sortType) |
||
| 48 | ->setMaxResults($limit + 1); // Fetch one more than required for pagination. |
||
| 49 | |||
| 50 | if ($lastId) { |
||
| 51 | $qb->where($qb->getRootAliases()[0].'.'.$sortKey.' '.$direction.' :lastId'); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (is_a($this->entityRepository->getClassName(), DeletableInterface::class, true)) { |
||
| 55 | $qb->andWhere($qb->getRootAliases()[0].'.isDeleted = false'); |
||
| 56 | } |
||
| 57 | |||
| 58 | foreach ($filters as $filter) { |
||
| 59 | if ($filter instanceof DoctrineFilterInterface && $filter->hasData()) { |
||
| 60 | $filter->modifyQueryBuilder($qb); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | $query = $qb->getQuery(); |
||
| 64 | |||
| 65 | foreach ($filters as $filter) { |
||
| 66 | if ($filter instanceof DoctrineFilterInterface && $filter->hasData()) { |
||
| 67 | $filter->modifyQuery($query); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($lastId) { |
||
| 72 | $query->setParameter(':lastId', $lastId); |
||
| 73 | } |
||
| 74 | |||
| 75 | return new ResultSet($query->getResult(), $sortKey, $sortType, $limit); |
||
|
|
|||
| 76 | } |
||
| 110 |