| Conditions | 46 |
| Paths | > 20000 |
| Total Lines | 175 |
| Lines | 41 |
| Ratio | 23.43 % |
| 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 |
||
| 23 | public function create_links() { |
||
| 24 | // If our item count or per-page total is zero there is no need to continue. |
||
| 25 | if ($this->total_rows == 0 OR $this->per_page == 0) { |
||
| 26 | return ''; |
||
| 27 | } |
||
| 28 | |||
| 29 | // Calculate the total number of pages |
||
| 30 | $num_pages = ceil($this->total_rows / $this->per_page); |
||
| 31 | |||
| 32 | // Is there only one page? Hm... nothing more to do here then. |
||
| 33 | if ($num_pages == 1) { |
||
| 34 | return ''; |
||
| 35 | } |
||
| 36 | |||
| 37 | // Set the base page index for starting page number |
||
| 38 | if ($this->use_page_numbers) { |
||
| 39 | $base_page = 1; |
||
| 40 | } else { |
||
| 41 | $base_page = 0; |
||
| 42 | } |
||
| 43 | |||
| 44 | // Determine the current page number. |
||
| 45 | $CI =& get_instance(); |
||
| 46 | |||
| 47 | View Code Duplication | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) { |
|
| 48 | if ($CI->input->get($this->query_string_segment) != $base_page) { |
||
| 49 | $this->cur_page = $CI->input->get($this->query_string_segment); |
||
| 50 | |||
| 51 | // Prep the current page - no funny business! |
||
| 52 | $this->cur_page = (int) $this->cur_page; |
||
| 53 | } |
||
| 54 | } else { |
||
| 55 | if ($CI->uri->segment($this->uri_segment) != $base_page) { |
||
| 56 | $this->cur_page = $CI->uri->segment($this->uri_segment); |
||
| 57 | |||
| 58 | // Prep the current page - no funny business! |
||
| 59 | $this->cur_page = (int) $this->cur_page; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | // Set current page to 1 if using page numbers instead of offset |
||
| 64 | if ($this->use_page_numbers AND $this->cur_page == 0) { |
||
| 65 | $this->cur_page = $base_page; |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->num_links = (int) $this->num_links; |
||
| 69 | |||
| 70 | if ($this->num_links < 1) { |
||
| 71 | show_error('Your number of links must be a positive number.'); |
||
| 72 | } |
||
| 73 | |||
| 74 | if (!is_numeric($this->cur_page)) { |
||
| 75 | $this->cur_page = $base_page; |
||
| 76 | } |
||
| 77 | |||
| 78 | // Is the page number beyond the result range? |
||
| 79 | // If so we show the last page |
||
| 80 | if ($this->use_page_numbers) { |
||
| 81 | if ($this->cur_page > $num_pages) { |
||
| 82 | $this->cur_page = $num_pages; |
||
| 83 | } |
||
| 84 | } else { |
||
| 85 | if ($this->cur_page > $this->total_rows) { |
||
| 86 | $this->cur_page = ($num_pages - 1) * $this->per_page; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $uri_page_number = $this->cur_page; |
||
| 91 | |||
| 92 | if (!$this->use_page_numbers) { |
||
| 93 | $this->cur_page = floor(($this->cur_page / $this->per_page) + 1); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Calculate the start and end numbers. These determine |
||
| 97 | // which number to start and end the digit links with |
||
| 98 | $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; |
||
| 99 | $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; |
||
| 100 | |||
| 101 | // Is pagination being used over GET or POST? If get, add a per_page query |
||
| 102 | // string. If post, add a trailing slash to the base URL if needed |
||
| 103 | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) { |
||
| 104 | if (strpos($this->base_url, '?') === false) { |
||
| 105 | $this->base_url = rtrim($this->base_url) . '?' . $this->query_string_segment . '='; |
||
| 106 | } else { |
||
| 107 | $this->base_url = rtrim($this->base_url) . '&' . $this->query_string_segment . '='; |
||
| 108 | |||
| 109 | } |
||
| 110 | } else { |
||
| 111 | $this->base_url = rtrim($this->base_url, '/') . '/'; |
||
| 112 | } |
||
| 113 | |||
| 114 | // And here we go... |
||
| 115 | $output = ''; |
||
| 116 | |||
| 117 | // Render the "First" link |
||
| 118 | if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1)) { |
||
| 119 | $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url; |
||
| 120 | $output .= $this->first_tag_open . '<a ' . $this->anchor_class . 'href="' . $first_url . '">' . $this->first_link . '</a>' . $this->first_tag_close; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Render the "previous" link |
||
| 124 | if ($this->prev_link !== FALSE AND $this->cur_page != 1) { |
||
| 125 | if ($this->use_page_numbers) { |
||
| 126 | $i = $uri_page_number - 1; |
||
| 127 | } else { |
||
| 128 | $i = $uri_page_number - $this->per_page; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($i == 0 && $this->first_url != '') { |
||
| 132 | $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->first_url . '">' . $this->prev_link . '</a>' . $this->prev_tag_close; |
||
| 133 | View Code Duplication | } else { |
|
| 134 | $i = ($i == 0) ? '' : $this->prefix . $i . $this->suffix; |
||
| 135 | $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $i . '">' . $this->prev_link . '</a>' . $this->prev_tag_close; |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | // Render the pages |
||
| 141 | if ($this->display_pages !== FALSE) { |
||
| 142 | // Write the digit links |
||
| 143 | for ($loop = $start - 1; $loop <= $end; $loop++) { |
||
| 144 | if ($this->use_page_numbers) { |
||
| 145 | $i = $loop; |
||
| 146 | } else { |
||
| 147 | $i = ($loop * $this->per_page) - $this->per_page; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($i >= $base_page) { |
||
| 151 | if ($this->cur_page == $loop) { |
||
| 152 | $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; // Current page |
||
| 153 | } else { |
||
| 154 | $n = ($i == $base_page) ? '' : $i; |
||
| 155 | |||
| 156 | if ($n == '' && $this->first_url != '') { |
||
| 157 | $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->first_url . '">' . $loop . '</a>' . $this->num_tag_close; |
||
| 158 | View Code Duplication | } else { |
|
| 159 | $n = ($n == '') ? '' : $this->prefix . $n . $this->suffix; |
||
| 160 | |||
| 161 | $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $n . '">' . $loop . '</a>' . $this->num_tag_close; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // Render the "next" link |
||
| 169 | View Code Duplication | if ($this->next_link !== FALSE AND $this->cur_page < $num_pages) { |
|
| 170 | if ($this->use_page_numbers) { |
||
| 171 | $i = $this->cur_page + 1; |
||
| 172 | } else { |
||
| 173 | $i = ($this->cur_page * $this->per_page); |
||
| 174 | } |
||
| 175 | |||
| 176 | $output .= $this->next_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $this->prefix . $i . $this->suffix . '">' . $this->next_link . '</a>' . $this->next_tag_close; |
||
| 177 | } |
||
| 178 | |||
| 179 | // Render the "Last" link |
||
| 180 | View Code Duplication | if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages) { |
|
| 181 | if ($this->use_page_numbers) { |
||
| 182 | $i = $num_pages; |
||
| 183 | } else { |
||
| 184 | $i = (($num_pages * $this->per_page) - $this->per_page); |
||
| 185 | } |
||
| 186 | $output .= $this->last_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $this->prefix . $i . $this->suffix . '">' . $this->last_link . '</a>' . $this->last_tag_close; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Kill double slashes. Note: Sometimes we can end up with a double slash |
||
| 190 | // in the penultimate link so we'll kill all double slashes. |
||
| 191 | $output = preg_replace('#([^:])//+#', "\\1/", $output); |
||
| 192 | |||
| 193 | // Add the wrapper HTML if exists |
||
| 194 | $output = $this->full_tag_open . $output . $this->full_tag_close; |
||
| 195 | |||
| 196 | return $output; |
||
| 197 | } |
||
| 198 | |||
| 348 | } |