| Total Complexity | 40 |
| Total Lines | 149 |
| 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 |
||
| 8 | class PageNav |
||
| 9 | { |
||
| 10 | public $total; |
||
| 11 | public $perpage; |
||
| 12 | public $current; |
||
| 13 | public $url; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * MylinksPageNav constructor. |
||
| 17 | * @param $total_items |
||
| 18 | * @param $items_perpage |
||
| 19 | * @param $current_start |
||
| 20 | * @param string $start_name |
||
| 21 | * @param string $extra_arg |
||
| 22 | */ |
||
| 23 | public function __construct( |
||
| 24 | $total_items, |
||
| 25 | $items_perpage, |
||
| 26 | $current_start, |
||
| 27 | $start_name = 'start', |
||
| 28 | $extra_arg = '' |
||
| 29 | ) { |
||
| 30 | $this->total = (int)$total_items; |
||
| 31 | $this->perpage = (int)$items_perpage; |
||
| 32 | $this->current = (int)$current_start; |
||
| 33 | if ('' != $extra_arg && ('&' !== mb_substr($extra_arg, -5) || '&' !== mb_substr($extra_arg, -1))) { |
||
| 34 | $extra_arg .= '&'; |
||
| 35 | } |
||
| 36 | $this->url = xoops_getenv('SCRIPT_NAME') . '?' . $extra_arg . trim($start_name) . '='; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param int $offset |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function renderNav($offset = 5) |
||
| 44 | { |
||
| 45 | $ret = ''; |
||
| 46 | if ($this->total <= $this->perpage) { |
||
| 47 | return $ret; |
||
| 48 | } |
||
| 49 | $total_pages = ceil($this->total / $this->perpage); |
||
| 50 | if ($total_pages > 1) { |
||
| 51 | $prev = $this->current - $this->perpage; |
||
| 52 | if ($prev >= 0) { |
||
| 53 | $ret .= '<a href="' . $this->url . $prev . '"><u>«</u></a> '; |
||
| 54 | } |
||
| 55 | $counter = 1; |
||
| 56 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
| 57 | while ($counter <= $total_pages) { |
||
| 58 | if ($counter == $current_page) { |
||
| 59 | $ret .= '<strong>(' . $counter . ')</strong> '; |
||
| 60 | } elseif (($counter > $current_page - $offset && $counter < $current_page + $offset) || 1 == $counter |
||
| 61 | || $counter == $total_pages) { |
||
| 62 | if ($counter == $total_pages && $current_page < $total_pages - $offset) { |
||
| 63 | $ret .= '... '; |
||
| 64 | } |
||
| 65 | $ret .= '<a href="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</a> '; |
||
| 66 | if (1 == $counter && $current_page > 1 + $offset) { |
||
| 67 | $ret .= '... '; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | ++$counter; |
||
| 71 | } |
||
| 72 | $next = $this->current + $this->perpage; |
||
| 73 | if ($this->total > $next) { |
||
| 74 | $ret .= '<a href="' . $this->url . $next . '"><u>»</u></a> '; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return $ret; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param bool $showbutton |
||
| 83 | * @return string|void |
||
| 84 | */ |
||
| 85 | public function renderSelect($showbutton = false) |
||
| 86 | { |
||
| 87 | if ($this->total < $this->perpage) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | $total_pages = ceil($this->total / $this->perpage); |
||
| 91 | $ret = ''; |
||
| 92 | if ($total_pages > 1) { |
||
| 93 | $ret = '<form name="pagenavform" action="' . xoops_getenv('SCRIPT_NAME') . '">'; |
||
| 94 | $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">'; |
||
| 95 | $counter = 1; |
||
| 96 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
| 97 | while ($counter <= $total_pages) { |
||
| 98 | if ($counter == $current_page) { |
||
| 99 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $counter . '</option>'; |
||
| 100 | } else { |
||
| 101 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>'; |
||
| 102 | } |
||
| 103 | ++$counter; |
||
| 104 | } |
||
| 105 | $ret .= '</select>'; |
||
| 106 | if ($showbutton) { |
||
| 107 | $ret .= ' <input type="submit" value="' . _GO . '">'; |
||
| 108 | } |
||
| 109 | $ret .= '</form>'; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $ret; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param int $offset |
||
| 117 | * @return string|void |
||
| 118 | */ |
||
| 119 | public function renderImageNav($offset = 5) |
||
| 157 | } |
||
| 158 | } |
||
| 159 |