Complex classes like Component 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Component, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Component |
||
| 8 | { |
||
| 9 | private $page; |
||
| 10 | private $get; |
||
| 11 | private $url; |
||
| 12 | private $start; |
||
| 13 | private $limit; // formerly $display |
||
| 14 | private $total; // formerly $num_pages |
||
| 15 | private $current; |
||
| 16 | private $links = array( |
||
| 17 | 'wrapper' => '<ul class="pagination">%s</ul>', |
||
| 18 | 'link' => '<li><a href="%s">%s</a></li>', |
||
| 19 | 'active' => '<li class="active"><span>%s</span></li>', |
||
| 20 | 'disabled' => '<li class="disabled"><span>%s</span></li>', |
||
| 21 | 'previous' => '«', |
||
| 22 | 'next' => '»', |
||
| 23 | 'dots' => '…', |
||
| 24 | ); |
||
| 25 | private $pager = array( |
||
| 26 | 'wrapper' => '<ul class="pager">%s</ul>', |
||
| 27 | 'previous' => '<li class="previous"><a href="%s">« %s</a></li>', |
||
| 28 | 'next' => '<li class="next"><a href="%s">%s »</a></li>', |
||
| 29 | ); |
||
| 30 | |||
| 31 | 20 | public function __construct($framework = 'bootstrap') // http://getbootstrap.com/components/#pagination |
|
| 32 | { |
||
| 33 | 20 | $this->page = Page::html(); |
|
| 34 | 20 | $this->get = false; |
|
| 35 | 20 | $this->set(); |
|
| 36 | switch ($framework) { |
||
| 37 | 20 | case 'zurb_foundation': // http://foundation.zurb.com/docs/components/pagination.html |
|
| 38 | 1 | $this->html('links', array( |
|
| 39 | 1 | 'active' => '<li class="current"><a href="">%s</a></li>', |
|
| 40 | 1 | 'disabled' => '<li class="unavailable"><a href="">%s</a></li>', |
|
| 41 | 1 | )); |
|
| 42 | 1 | break; |
|
| 43 | 19 | case 'semantic_ui': // http://semantic-ui.com/collections/menu.html#pagination |
|
| 44 | 1 | $this->html('links', array( |
|
| 45 | 1 | 'wrapper' => '<div class="ui pagination menu">%s</div>', |
|
| 46 | 1 | 'link' => '<a class="item" href="%s">%s</a>', |
|
| 47 | 1 | 'active' => '<div class="active item">%s</div>', |
|
| 48 | 1 | 'disabled' => '<div class="disabled item">%s</div>', |
|
| 49 | 1 | 'previous' => '<i class="left arrow icon"></i>', |
|
| 50 | 1 | 'next' => '<i class="right arrow icon"></i>', |
|
| 51 | 1 | )); |
|
| 52 | 1 | break; |
|
| 53 | 18 | case 'materialize': // http://materializecss.com/pagination.html |
|
| 54 | 1 | $this->html('links', array( |
|
| 55 | 1 | 'link' => '<li class="waves-effect"><a href="%s">%s</a></li>', |
|
| 56 | 1 | 'active' => '<li class="active"><a href="#!">%s</a></li>', |
|
| 57 | 1 | 'disabled' => '<li class="disabled"><a href="#!">%s</a></li>', |
|
| 58 | 1 | 'previous' => '<i class="material-icons">keyboard_arrow_left</i>', |
|
| 59 | 1 | 'next' => '<i class="material-icons">keyboard_arrow_right</i>', |
|
| 60 | 1 | )); |
|
| 61 | 1 | break; |
|
| 62 | 17 | case 'uikit': // http://getuikit.com/docs/pagination.html |
|
| 63 | 1 | $this->html('links', array( |
|
| 64 | 1 | 'wrapper' => '<ul class="uk-pagination">%s</ul>', |
|
| 65 | 1 | 'active' => '<li class="uk-active"><span>%s</span></li>', |
|
| 66 | 1 | 'disabled' => '<li class="uk-disabled"><span>%s</span></li>', |
|
| 67 | 1 | 'previous' => '<i class="uk-icon-angle-double-left"></i>', |
|
| 68 | 1 | 'next' => '<i class="uk-icon-angle-double-right"></i>', |
|
| 69 | 1 | )); |
|
| 70 | 1 | $this->html('pager', array( |
|
| 71 | 1 | 'wrapper' => '<ul class="uk-pagination">%s</ul>', |
|
| 72 | 1 | 'previous' => '<li class="uk-pagination-previous"><a href="%s"><i class="uk-icon-angle-double-left"></i> %s</a></li>', |
|
| 73 | 1 | 'next' => '<li class="uk-pagination-next"><a href="%s">%s <i class="uk-icon-angle-double-right"></i></a></li>', |
|
| 74 | 1 | )); |
|
| 75 | 1 | break; |
|
| 76 | } |
||
| 77 | 20 | } |
|
| 78 | |||
| 79 | 15 | public function __get($name) |
|
| 104 | |||
| 105 | 21 | public function set($page = 'page', $limit = 10, $url = null) |
|
| 132 | |||
| 133 | 13 | public function total($count) |
|
| 139 | |||
| 140 | 4 | public function html($type, array $options) |
|
| 148 | |||
| 149 | 7 | public function links($pad = 3, $array = false) |
|
| 204 | |||
| 205 | 17 | public function pager($previous = 'Previous', $next = 'Next') |
|
| 206 | { |
||
| 207 | 17 | $links = ''; |
|
| 208 | 17 | if (!empty($previous)) { |
|
| 209 | 15 | if (is_array($previous)) { |
|
| 210 | 3 | if (isset($previous['url']) && isset($previous['title'])) { |
|
| 211 | 3 | $links .= sprintf($this->pager['previous'], $previous['url'], $previous['title']); |
|
| 212 | 3 | } |
|
| 213 | 15 | } elseif (is_string($previous)) { |
|
| 214 | 12 | if ($this->get && $this->total > 1 && $this->current > 1) { |
|
| 215 | 1 | $links .= sprintf($this->pager['previous'], $this->page($this->current - 1), $previous); |
|
| 216 | 1 | } |
|
| 217 | 12 | } |
|
| 218 | 15 | } |
|
| 219 | 17 | if (!empty($next)) { |
|
| 220 | 15 | if (is_array($next)) { |
|
| 221 | 3 | if (isset($next['url']) && isset($next['title'])) { |
|
| 222 | 3 | $links .= sprintf($this->pager['next'], $next['url'], $next['title']); |
|
| 223 | 3 | } |
|
| 224 | 15 | } elseif (is_string($next)) { |
|
| 225 | 12 | if ($this->get && $this->current < $this->total) { |
|
| 226 | 2 | $links .= sprintf($this->pager['next'], $this->page($this->current + 1), $next); |
|
| 227 | 2 | } |
|
| 228 | 12 | } |
|
| 229 | 15 | } |
|
| 230 | |||
| 231 | 17 | return (!empty($links)) ? "\n".sprintf($this->pager['wrapper'], $links) : ''; |
|
| 232 | } |
||
| 233 | |||
| 234 | 7 | private function page($num) |
|
| 242 | } |
||
| 243 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.