Total Complexity | 45 |
Total Lines | 222 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsPageNav 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 XoopsPageNav, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class XoopsPageNav |
||
27 | { |
||
28 | /** |
||
29 | * *#@+ |
||
30 | * |
||
31 | * @access private |
||
32 | */ |
||
33 | public $total; |
||
34 | public $perpage; |
||
35 | public $current; |
||
36 | public $url; |
||
37 | /** |
||
38 | * *#@- |
||
39 | */ |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param int $total_items Total number of items |
||
45 | * @param int $items_perpage Number of items per page |
||
46 | * @param int $current_start First item on the current page |
||
47 | * @param string $start_name Name for "start" or "offset" |
||
48 | * @param string $extra_arg Additional arguments to pass in the URL |
||
49 | */ |
||
50 | public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '') |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Create text navigation |
||
64 | * |
||
65 | * @param integer $offset |
||
66 | * @return string |
||
67 | */ |
||
68 | public function renderNav($offset = 4) |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create a navigational dropdown list |
||
127 | * |
||
128 | * @param boolean $showbutton Show the "Go" button? |
||
129 | * @return string |
||
130 | */ |
||
131 | public function renderSelect($showbutton = false) |
||
132 | { |
||
133 | $ret = ''; |
||
134 | if ($this->total < $this->perpage) { |
||
135 | return $ret; |
||
136 | } |
||
137 | $total_pages = ceil($this->total / $this->perpage); |
||
138 | if ($total_pages > 1) { |
||
139 | $counter = 1; |
||
140 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
141 | while ($counter <= $total_pages) { |
||
142 | if ($counter == $current_page) { |
||
143 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . $this->extra . '" selected>' . $counter . '</option>'; |
||
144 | } else { |
||
145 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . $this->extra . '">' . $counter . '</option>'; |
||
146 | } |
||
147 | ++$counter; |
||
148 | } |
||
149 | if ($showbutton) { |
||
150 | $navigation['button'] = true; |
||
151 | } else { |
||
152 | $navigation['button'] = false; |
||
153 | } |
||
154 | $navigation['select'] = $ret; |
||
155 | return $this->displayPageNav('Select', $navigation); |
||
156 | } |
||
157 | return $ret; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Create navigation with images |
||
162 | * |
||
163 | * @param integer $offset |
||
164 | * @return string |
||
165 | */ |
||
166 | public function renderImageNav($offset = 4) |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Display navigation in template |
||
232 | * |
||
233 | * @param string $type |
||
234 | * @param array $navigation |
||
235 | * @return string |
||
236 | */ |
||
237 | private function displayPageNav($type = 'nav', $navigation = array()){ |
||
248 | |||
249 | } |
||
251 |