| Conditions | 20 |
| Paths | 6912 |
| Total Lines | 94 |
| Code Lines | 62 |
| 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 |
||
| 118 | private function buildPagination(array $pagination, PersistentCollection $collection, Criteria $criteria): array |
||
| 119 | { |
||
| 120 | $first = 0; |
||
| 121 | $after = 0; |
||
| 122 | $last = 0; |
||
| 123 | $before = 0; |
||
| 124 | $offset = 0; |
||
| 125 | |||
| 126 | // Pagination |
||
| 127 | foreach ($pagination as $field => $value) { |
||
| 128 | switch ($field) { |
||
| 129 | case 'first': |
||
| 130 | $first = $value; |
||
| 131 | break; |
||
| 132 | case 'after': |
||
| 133 | $after = (int) base64_decode($value, true) + 1; |
||
| 134 | break; |
||
| 135 | case 'last': |
||
| 136 | $last = $value; |
||
| 137 | break; |
||
| 138 | case 'before': |
||
| 139 | $before = (int) base64_decode($value, true); |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $limit = $this->config->getLimit(); |
||
| 145 | $adjustedLimit = $first ?: $last ?: $limit; |
||
| 146 | if ($adjustedLimit < $limit) { |
||
| 147 | $limit = $adjustedLimit; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($after) { |
||
| 151 | $offset = $after; |
||
| 152 | } elseif ($before) { |
||
| 153 | $offset = $before - $limit; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($offset < 0) { |
||
| 157 | $limit += $offset; |
||
| 158 | $offset = 0; |
||
| 159 | } |
||
| 160 | |||
| 161 | // Get total count from collection then match |
||
| 162 | $itemCount = count($collection->matching($criteria)); |
||
| 163 | |||
| 164 | if ($last && ! $before) { |
||
| 165 | $offset = $itemCount - $last; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($offset) { |
||
| 169 | $criteria->setFirstResult($offset); |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($limit) { |
||
| 173 | $criteria->setMaxResults($limit); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Fetch slice of collection |
||
| 177 | $items = $collection->matching($criteria); |
||
| 178 | |||
| 179 | $edges = []; |
||
| 180 | $index = 0; |
||
| 181 | $lastCursor = base64_encode((string) 0); |
||
| 182 | $firstCursor = null; |
||
| 183 | foreach ($items as $result) { |
||
| 184 | $cursor = base64_encode((string) ($index + $offset)); |
||
| 185 | |||
| 186 | $edges[] = [ |
||
| 187 | 'node' => $result, |
||
| 188 | 'cursor' => $cursor, |
||
| 189 | ]; |
||
| 190 | |||
| 191 | $lastCursor = $cursor; |
||
| 192 | if (! $firstCursor) { |
||
| 193 | $firstCursor = $cursor; |
||
| 194 | } |
||
| 195 | |||
| 196 | $index++; |
||
| 197 | } |
||
| 198 | |||
| 199 | $endCursor = $itemCount ? $itemCount - 1 : 0; |
||
| 200 | $startCursor = base64_encode((string) 0); |
||
| 201 | $endCursor = base64_encode((string) $endCursor); |
||
| 202 | |||
| 203 | // Return entities |
||
| 204 | return [ |
||
| 205 | 'edges' => $edges, |
||
| 206 | 'totalCount' => $itemCount, |
||
| 207 | 'pageInfo' => [ |
||
| 208 | 'endCursor' => $endCursor, |
||
| 209 | 'startCursor' => $startCursor, |
||
| 210 | 'hasNextPage' => $endCursor !== $lastCursor, |
||
| 211 | 'hasPreviousPage' => $firstCursor !== null && $startCursor !== $firstCursor, |
||
| 212 | ], |
||
| 216 |