| 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 |
||
| 151 | private function buildPagination(array $filter, PersistentCollection $collection, Criteria $criteria): array |
||
| 152 | { |
||
| 153 | $first = 0; |
||
| 154 | $after = 0; |
||
| 155 | $last = 0; |
||
| 156 | $before = 0; |
||
| 157 | $offset = 0; |
||
| 158 | |||
| 159 | // Pagination |
||
| 160 | foreach ($filter as $field => $value) { |
||
| 161 | switch ($field) { |
||
| 162 | case '_first': |
||
| 163 | $first = $value; |
||
| 164 | break; |
||
| 165 | case '_after': |
||
| 166 | $after = (int) base64_decode($value, true) + 1; |
||
| 167 | break; |
||
| 168 | case '_last': |
||
| 169 | $last = $value; |
||
| 170 | break; |
||
| 171 | case '_before': |
||
| 172 | $before = (int) base64_decode($value, true); |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | $limit = $this->config->getLimit(); |
||
| 178 | $adjustedLimit = $first ?: $last ?: $limit; |
||
| 179 | if ($adjustedLimit < $limit) { |
||
| 180 | $limit = $adjustedLimit; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($after) { |
||
| 184 | $offset = $after; |
||
| 185 | } elseif ($before) { |
||
| 186 | $offset = $before - $limit; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($offset < 0) { |
||
| 190 | $limit += $offset; |
||
| 191 | $offset = 0; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Get total count from collection then match |
||
| 195 | $itemCount = count($collection->matching($criteria)); |
||
| 196 | |||
| 197 | if ($last && ! $before) { |
||
| 198 | $offset = $itemCount - $last; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($offset) { |
||
| 202 | $criteria->setFirstResult($offset); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($limit) { |
||
| 206 | $criteria->setMaxResults($limit); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Fetch slice of collection |
||
| 210 | $items = $collection->matching($criteria); |
||
| 211 | |||
| 212 | $edges = []; |
||
| 213 | $index = 0; |
||
| 214 | $lastCursor = base64_encode((string) 0); |
||
| 215 | $firstCursor = null; |
||
| 216 | foreach ($items as $result) { |
||
| 217 | $cursor = base64_encode((string) ($index + $offset)); |
||
| 218 | |||
| 219 | $edges[] = [ |
||
| 220 | 'node' => $result, |
||
| 221 | 'cursor' => $cursor, |
||
| 222 | ]; |
||
| 223 | |||
| 224 | $lastCursor = $cursor; |
||
| 225 | if (! $firstCursor) { |
||
| 226 | $firstCursor = $cursor; |
||
| 227 | } |
||
| 228 | |||
| 229 | $index++; |
||
| 230 | } |
||
| 231 | |||
| 232 | $endCursor = $itemCount ? $itemCount - 1 : 0; |
||
| 233 | $startCursor = base64_encode((string) 0); |
||
| 234 | $endCursor = base64_encode((string) $endCursor); |
||
| 235 | |||
| 236 | // Return entities |
||
| 237 | return [ |
||
| 238 | 'edges' => $edges, |
||
| 239 | 'totalCount' => $itemCount, |
||
| 240 | 'pageInfo' => [ |
||
| 241 | 'endCursor' => $endCursor, |
||
| 242 | 'startCursor' => $startCursor, |
||
| 243 | 'hasNextPage' => $endCursor !== $lastCursor, |
||
| 244 | 'hasPreviousPage' => $firstCursor !== null && $startCursor !== $firstCursor, |
||
| 245 | ], |
||
| 249 |