| Total Complexity | 46 |
| Total Lines | 197 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PageNav often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageNav, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 32 | class PageNav |
||
| 33 | { |
||
| 34 | /**#@+ |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | public $total; |
||
| 38 | public $perpage; |
||
| 39 | public $current; |
||
| 40 | public $url; |
||
| 41 | /**#@-*/ |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param int $total_items Total number of items |
||
| 47 | * @param int $items_perpage Number of items per page |
||
| 48 | * @param int $current_start First item on the current page |
||
| 49 | * @param string $start_name Name for "start" or "offset" |
||
| 50 | * @param string $extra_arg Additional arguments to pass in the URL |
||
| 51 | **/ |
||
| 52 | public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '') |
||
| 53 | { |
||
| 54 | $this->total = (int)$total_items; |
||
| 55 | $this->perpage = (int)$items_perpage; |
||
| 56 | $this->current = (int)$current_start; |
||
| 57 | if ('' !== $extra_arg && ('&' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) { |
||
| 58 | $extra_arg .= '&'; |
||
| 59 | } |
||
| 60 | $this->url = $_SERVER['SCRIPT_NAME'] . '?' . $extra_arg . trim($start_name) . '='; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Create text navigation |
||
| 65 | * |
||
| 66 | * @param int $offset |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | **/ |
||
| 70 | public function renderNav($offset = 4) |
||
| 71 | { |
||
| 72 | $ret = ''; |
||
| 73 | if ($this->total <= $this->perpage) { |
||
| 74 | return $ret; |
||
| 75 | } |
||
| 76 | $total_pages = ceil($this->total / $this->perpage); |
||
| 77 | if ($total_pages > 1) { |
||
| 78 | $prev = $this->current - $this->perpage; |
||
| 79 | if ($prev >= 0) { |
||
| 80 | $ret .= '<a href="' . $this->url . $prev . '"><u>«</u></a> '; |
||
| 81 | } |
||
| 82 | $counter = 1; |
||
| 83 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
| 84 | while ($counter <= $total_pages) { |
||
| 85 | if ($counter == $current_page) { |
||
| 86 | $ret .= '<b>(' . $counter . ')</b> '; |
||
| 87 | } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter |
||
| 88 | || $counter == $total_pages) { |
||
| 89 | if ($counter == $total_pages && $current_page < $total_pages - $offset) { |
||
| 90 | $ret .= '... '; |
||
| 91 | } |
||
| 92 | $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> '; |
||
| 93 | if (1 == $counter && $current_page > 1 + $offset) { |
||
| 94 | $ret .= '... '; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | ++$counter; |
||
| 98 | } |
||
| 99 | $next = $this->current + $this->perpage; |
||
| 100 | if ($this->total > $next) { |
||
| 101 | $ret .= '<a href="' . $this->url . $next . '"><u>»</u></a> '; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $ret; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create a navigational dropdown list |
||
| 110 | * |
||
| 111 | * @param bool $showbutton Show the "Go" button? |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | **/ |
||
| 115 | public function renderSelect($showbutton = false) |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Create an enhanced navigational dropdown list |
||
| 147 | * |
||
| 148 | * @param bool $showbutton Show the "Go" button? |
||
| 149 | * @param null $titles |
||
|
|
|||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function renderEnhancedSelect($showbutton = false, $titles = null) |
||
| 154 | { |
||
| 155 | if ($this->total < $this->perpage) { |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | $total_pages = ceil($this->total / $this->perpage); |
||
| 159 | $ret = ''; |
||
| 160 | if ($total_pages > 1) { |
||
| 161 | $ret = '<form name="pagenavform">'; |
||
| 162 | $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">'; |
||
| 163 | $counter = 1; |
||
| 164 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
| 165 | while ($counter <= $total_pages) { |
||
| 166 | $title = $titles[$counter - 1] ?? $counter; |
||
| 167 | if ($counter == $current_page) { |
||
| 168 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $title . '</option>'; |
||
| 169 | } else { |
||
| 170 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $title . '</option>'; |
||
| 171 | } |
||
| 172 | ++$counter; |
||
| 173 | } |
||
| 174 | $ret .= '</select>'; |
||
| 175 | if ($showbutton) { |
||
| 176 | $ret .= ' <input type="submit" value="' . _GO . '">'; |
||
| 177 | } |
||
| 178 | $ret .= '</form>'; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $ret; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Create navigation with images |
||
| 186 | * |
||
| 187 | * @param int $offset |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | **/ |
||
| 191 | public function renderImageNav($offset = 4) |
||
| 229 | } |
||
| 230 | } |
||
| 231 |