Paging   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
c 1
b 0
f 0
dl 0
loc 55
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 44 10
1
<?php
2
3
namespace Core\Services\HTML;
4
5
use Core\Services\Globals\Globals;
6
7
class Paging
8
{
9
    /**
10
     * Paging of the past SQL statement
11
     * @param int $limit number of elements returned per page
12
     * @param string $queryString ex:'post.index'
13
     * @param object $table table involved
14
     * @param string $statement
15
     * @param null|array $attributes
16
     * @return array with the \compat function | !! use \extract to retrieve variables
17
     */
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);
0 ignored issues
show
Bug introduced by
It seems like $globals->getSERVER('QUERY_STRING') can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
        preg_match('/\d*$/', /** @scrutinizer ignore-type */ $globals->getSERVER('QUERY_STRING'), $page);
Loading history...
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); ?>">&laquo;</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); ?>">&raquo;</a>
54
                    </li>
55
                <?php endif; ?>
56
            </ul>
57
        </nav>
58
<?php
59
        $paging = \ob_get_clean();
60
61
        return compact($rows, 'paging');
62
    }
63
}
64