| Conditions | 9 |
| Paths | 15 |
| Total Lines | 78 |
| Code Lines | 24 |
| 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 |
||
| 83 | static public function paginateItems($items, int $index, $format_item, int $item_per_page = 3, $keyboard = null, $prefix = 'list', string $delimiter = DELIMITER) { |
||
| 84 | |||
| 85 | // Calc the position of the first item to show |
||
| 86 | $item_position = ($index - 1) * $item_per_page + 1; |
||
| 87 | |||
| 88 | // Counter variable |
||
| 89 | $cont = 1; |
||
| 90 | |||
| 91 | // How many items did we display? |
||
| 92 | $items_displayed = 0; |
||
| 93 | |||
| 94 | // If keyboard is valid |
||
| 95 | if (isset($keyboard)) { |
||
| 96 | |||
| 97 | // Get how many items did the database return |
||
| 98 | $items_number = $items->rowCount(); |
||
| 99 | |||
| 100 | // Get how many complete pages there are |
||
| 101 | $total_pages = intval($items_number / $item_per_page); |
||
| 102 | |||
| 103 | // If there an incomplete page |
||
| 104 | if (($items_number % $item_per_page) != 0) { |
||
| 105 | |||
| 106 | $total_pages++; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Initialize keyboard with the list |
||
| 110 | $keyboard->initializeCompositeListKeyboard($index, $total_pages, $prefix); |
||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | // Initialize empty string |
||
| 115 | $message = ''; |
||
| 116 | |||
| 117 | // Iterate over all results |
||
| 118 | while ($item = $items->fetch()) { |
||
| 119 | |||
| 120 | // If we have to display the first item of the page and we found the item to show (using the position |
||
| 121 | // calculated before) |
||
| 122 | if ($items_displayed === 0 && $cont === $item_position) { |
||
| 123 | |||
| 124 | // Format the item using closure |
||
| 125 | $message .= $format_item($item, $keyboard); |
||
| 126 | |||
| 127 | // We displayed an item |
||
| 128 | $items_displayed++; |
||
| 129 | |||
| 130 | // If we displayed at least an item but still not how much we want |
||
| 131 | } elseif ($items_displayed > 0 && $items_displayed < $item_per_page) { |
||
| 132 | |||
| 133 | // Add delimiter to the message |
||
| 134 | $message .= DELIMITER; |
||
| 135 | |||
| 136 | // Format the item using closure |
||
| 137 | $message .= $format_item($item, $keyboard); |
||
| 138 | |||
| 139 | // We displayed an item |
||
| 140 | $items_displayed++; |
||
| 141 | |||
| 142 | // If we displayed all the item we wanted |
||
| 143 | } elseif ($items_displayed === $item_per_page) { |
||
| 144 | |||
| 145 | // Exit the cycle |
||
| 146 | break; |
||
| 147 | |||
| 148 | // We are just iterating over an unwanted result |
||
| 149 | } else { |
||
| 150 | |||
| 151 | $cont++; |
||
| 152 | |||
| 153 | } |
||
| 154 | |||
| 155 | } |
||
| 156 | |||
| 157 | // Return the created string |
||
| 158 | return $message; |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 164 |