Completed
Pull Request — master (#20)
by
unknown
03:12
created

Paginator::paginateItems()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 74
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 6.2894
c 0
b 0
f 0
cc 8
eloc 23
nc 10
nop 7

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace PhpBotFramework\Utilities;
4
5
define("DELIMITER", '::::::::::::::::::::::::::::::::::::::
6
    ');
7
8
class Paginator {
9
10
    static public function paginateItems($items, int $index, &$keyboard, $format_item, int $item_per_page = 3, $prefix = 'list', string $delimiter = DELIMITER) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
11
12
        // Calc the position of the first item to show
13
        $item_position = ($index - 1) * $item_per_page + 1;
14
15
        // Counter variable
16
        $cont = 1;
17
18
        // How many items did we display?
19
        $items_displayed = 0;
20
21
        // Get how many items did the database return
22
        $items_number = $items->rowCount();
23
24
        // Get how many complete pages there are
25
        $total_pages = intval($items_number / $item_per_page);
26
27
        // If there an incomplete page
28
        if (($items_number % $item_per_page) != 0) {
29
30
            $total_pages++;
31
32
        }
33
34
        // Initialize keyboard with the list
35
        $keyboard->addListKeyboard($index, $total_pages, $prefix);
36
37
        // Initialize empty string
38
        $message = '';
39
40
        // Iterate over all results
41
        while ($item = $items->fetch()) {
42
43
            // If we have to display the first item of the page and we found the item to show (using the position
44
            // calculated before)
45
            if ($items_displayed === 0 && $cont === $item_position) {
46
47
                // Format the item using closure
48
                $message .= $format_item($item, $keyboard);
49
50
                // We displayed an item
51
                $items_displayed++;
52
53
                // If we displayed at least an item but still not how much we want
54
            } elseif ($items_displayed > 0 && $items_displayed < $item_per_page) {
55
56
                // Add delimiter to the message
57
                $message .= $delimiter;
58
59
                // Format the item using closure
60
                $message .= $format_item($item, $keyboard);
61
62
                // We displayed an item
63
                $items_displayed++;
64
65
                // If we displayed all the item we wanted
66
            } elseif ($items_displayed === $item_per_page) {
67
68
                // Exit the cycle
69
                break;
70
71
                // We are just iterating over an unwanted result
72
            } else {
73
74
                $cont++;
75
76
            }
77
78
        }
79
80
        // Return the created string
81
        return $message;
82
83
    }
84
85
}
86