Total Complexity | 47 |
Total Lines | 201 |
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 |
||
33 | class XoopsPageNav |
||
34 | { |
||
35 | /**#@+ |
||
36 | * @access private |
||
37 | */ |
||
38 | public $total; |
||
39 | public $perpage; |
||
40 | public $current; |
||
41 | public $url; |
||
42 | /**#@-*/ |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param int $total_items Total number of items |
||
48 | * @param int $items_perpage Number of items per page |
||
49 | * @param int $current_start First item on the current page |
||
50 | * @param string $start_name Name for "start" or "offset" |
||
51 | * @param string $extra_arg Additional arguments to pass in the URL |
||
52 | **/ |
||
53 | public function __construct($total_items, $items_perpage, $current_start, $start_name = 'start', $extra_arg = '') |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create text navigation |
||
66 | * |
||
67 | * @param int $offset |
||
68 | * |
||
69 | * @return string |
||
70 | **/ |
||
71 | public function renderNav($offset = 4) |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Create a navigational dropdown list |
||
111 | * |
||
112 | * @param bool $showbutton Show the "Go" button? |
||
113 | * |
||
114 | * @return string |
||
115 | **/ |
||
116 | public function renderSelect($showbutton = false) |
||
117 | { |
||
118 | if ($this->total < $this->perpage) { |
||
119 | return null; |
||
120 | } |
||
121 | $total_pages = ceil($this->total / $this->perpage); |
||
122 | $ret = ''; |
||
123 | if ($total_pages > 1) { |
||
124 | $ret = '<form name="pagenavform">'; |
||
125 | $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">'; |
||
126 | $counter = 1; |
||
127 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
128 | while ($counter <= $total_pages) { |
||
129 | if ($counter == $current_page) { |
||
130 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $counter . '</option>'; |
||
131 | } else { |
||
132 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $counter . '</option>'; |
||
133 | } |
||
134 | ++$counter; |
||
135 | } |
||
136 | $ret .= '</select>'; |
||
137 | if ($showbutton) { |
||
138 | $ret .= ' <input type="submit" value="' . _GO . '">'; |
||
139 | } |
||
140 | $ret .= '</form>'; |
||
141 | } |
||
142 | |||
143 | return $ret; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Create an enhanced navigational dropdown list |
||
148 | * |
||
149 | * @param bool $showbutton Show the "Go" button? |
||
150 | * @param null $titles |
||
|
|||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function renderEnhancedSelect($showbutton = false, $titles = null) |
||
155 | { |
||
156 | if ($this->total < $this->perpage) { |
||
157 | return null; |
||
158 | } |
||
159 | $total_pages = ceil($this->total / $this->perpage); |
||
160 | $ret = ''; |
||
161 | if ($total_pages > 1) { |
||
162 | $ret = '<form name="pagenavform">'; |
||
163 | $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">'; |
||
164 | $counter = 1; |
||
165 | $current_page = (int)floor(($this->current + $this->perpage) / $this->perpage); |
||
166 | while ($counter <= $total_pages) { |
||
167 | if (isset($titles[$counter - 1])) { |
||
168 | $title = $titles[$counter - 1]; |
||
169 | } else { |
||
170 | $title = $counter; |
||
171 | } |
||
172 | if ($counter == $current_page) { |
||
173 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '" selected>' . $title . '</option>'; |
||
174 | } else { |
||
175 | $ret .= '<option value="' . $this->url . (($counter - 1) * $this->perpage) . '">' . $title . '</option>'; |
||
176 | } |
||
177 | ++$counter; |
||
178 | } |
||
179 | $ret .= '</select>'; |
||
180 | if ($showbutton) { |
||
181 | $ret .= ' <input type="submit" value="' . _GO . '">'; |
||
182 | } |
||
183 | $ret .= '</form>'; |
||
184 | } |
||
185 | |||
186 | return $ret; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Create navigation with images |
||
191 | * |
||
192 | * @param int $offset |
||
193 | * |
||
194 | * @return string |
||
195 | **/ |
||
196 | public function renderImageNav($offset = 4) |
||
234 | } |
||
235 | } |
||
236 |