Conditions | 19 |
Paths | 13836 |
Total Lines | 67 |
Code Lines | 38 |
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 |
||
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->createQueryBuilder(); |
||
38 | |||
39 | $direction = 'DESC' === $sortType ? '<' : '>'; |
||
40 | $firstDirection = 'DESC' === $sortType ? '>' : '<'; |
||
41 | if ($firstId) { |
||
42 | $sortType = ('DESC' === $sortType) ? 'ASC' : 'DESC'; |
||
43 | } |
||
44 | |||
45 | $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey); |
||
46 | $em = $this->entityRepository->getEntityManager(); |
||
47 | $columns = $em->getClassMetadata($this->entityRepository->getClassName())->getColumnNames(); |
||
48 | $columns = array_map(function ($colName) { |
||
49 | return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $colName)))); |
||
50 | }, $columns); |
||
51 | |||
52 | if (!in_array($sortKey, $columns)) { |
||
53 | throw new GeneralException("Sort key doesn't exist"); |
||
54 | } |
||
55 | |||
56 | $qb->orderBy($qb->getRootAliases()[0].'.'.$sortKey, $sortType); |
||
57 | |||
58 | if ($limit > 0) { |
||
59 | $qb->setMaxResults($limit + 1); // Fetch one more than required for pagination. |
||
60 | } |
||
61 | |||
62 | if ($lastId) { |
||
63 | $qb->where($qb->getRootAliases()[0].'.'.$sortKey.' '.$direction.' :lastId'); |
||
64 | } |
||
65 | if ($firstId) { |
||
66 | $qb->where($qb->getRootAliases()[0].'.'.$sortKey.' '.$firstDirection.' :firstId'); |
||
67 | } |
||
68 | |||
69 | if (is_a($this->entityRepository->getClassName(), DeletableInterface::class, true)) { |
||
70 | $qb->andWhere($qb->getRootAliases()[0].'.isDeleted = false'); |
||
71 | } |
||
72 | |||
73 | foreach ($filters as $filter) { |
||
74 | if ($filter instanceof DoctrineFilterInterface && $filter->hasData()) { |
||
75 | $filter->modifyQueryBuilder($qb); |
||
76 | } |
||
77 | } |
||
78 | $query = $qb->getQuery(); |
||
79 | |||
80 | foreach ($filters as $filter) { |
||
81 | if ($filter instanceof DoctrineFilterInterface && $filter->hasData()) { |
||
82 | $filter->modifyQuery($query); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if ($lastId) { |
||
87 | $query->setParameter(':lastId', $lastId); |
||
88 | } |
||
89 | if ($firstId) { |
||
90 | $query->setParameter(':firstId', $firstId); |
||
91 | } |
||
92 | |||
93 | $results = $query->getResult(); |
||
94 | |||
95 | if ($firstId) { |
||
96 | $results = array_reverse($results); |
||
|
|||
97 | } |
||
98 | |||
99 | return new ResultSet($results, $sortKey, $sortType, $limit); |
||
100 | } |
||
143 |