| Conditions | 10 |
| Paths | 128 |
| Total Lines | 44 |
| 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 |
||
| 18 | public static function generate(int $limit, string $queryString, object $table, string $statement, ?array $attributes = \null): array |
||
| 19 | { |
||
| 20 | $globals = new Globals; |
||
| 21 | |||
| 22 | // retrieves the page number |
||
| 23 | preg_match('/\d*$/', $globals->getSERVER('QUERY_STRING'), $page); |
||
|
|
|||
| 24 | |||
| 25 | $rows = \lcfirst($table->table . 's'); |
||
| 26 | |||
| 27 | $nbRows = $table->countRows($statement, $attributes); |
||
| 28 | $nbPages = \ceil($nbRows / $limit); |
||
| 29 | $page = !empty($page[0]) && $page[0] <= $nbPages ? $page[0] : 1; |
||
| 30 | |||
| 31 | // add the offest to the base SQL statement |
||
| 32 | $$rows = $table->offset($statement, $attributes, $limit, $page); |
||
| 33 | // manages the disabling of the previous and following btn |
||
| 34 | $previous = $page <= 1 ? ' disabled' : \null; |
||
| 35 | $next = $page >= $nbPages ? ' disabled' : \null; |
||
| 36 | |||
| 37 | \ob_start(); |
||
| 38 | ?> |
||
| 39 | <nav class="paging" aria-label="Page navigation"> |
||
| 40 | <ul class="pagination"> |
||
| 41 | <?php if (empty($previous)) : ?> |
||
| 42 | <li class="page-item<?= $previous ?>"> |
||
| 43 | <a class="page-link" href="?p=<?= $queryString; ?><?= '.' . ($page - 1); ?>">«</a> |
||
| 44 | </li> |
||
| 45 | <?php endif; ?> |
||
| 46 | <?php for ($i = 1; $i <= $nbPages; $i++) : ?> |
||
| 47 | <li class="page-item <?= $i == $page ? ' active' : null; ?>" <?= $i == $page ? ' arria-current="page"' : null ?>> |
||
| 48 | <a class="page-link" href="?p=<?= $queryString; ?><?= '.' . $i; ?>"><?= $i; ?></a> |
||
| 49 | </li> |
||
| 50 | <?php endfor; ?> |
||
| 51 | <?php if (empty($next)) : ?> |
||
| 52 | <li class="page-item<?= $next ?>"> |
||
| 53 | <a class="page-link" href="?p=<?= $queryString; ?><?= '.' . ($page + 1); ?>">»</a> |
||
| 54 | </li> |
||
| 55 | <?php endif; ?> |
||
| 56 | </ul> |
||
| 57 | </nav> |
||
| 58 | <?php |
||
| 59 | $paging = \ob_get_clean(); |
||
| 60 | |||
| 61 | return compact($rows, 'paging'); |
||
| 62 | } |
||
| 64 |