| Conditions | 17 |
| Paths | 1442 |
| Total Lines | 95 |
| Code Lines | 42 |
| Lines | 12 |
| Ratio | 12.63 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 75 | public function initialize($config = array()) |
||
| 76 | { |
||
| 77 | // Load config group |
||
| 78 | if (isset($config['group'])) { |
||
| 79 | // Load and validate config group |
||
| 80 | View Code Duplication | if (! is_array($group_config = Kohana::config('pagination.'.$config['group']))) { |
|
| 81 | throw new Kohana_Exception('pagination.undefined_group', $config['group']); |
||
| 82 | } |
||
| 83 | |||
| 84 | // All pagination config groups inherit default config group |
||
| 85 | View Code Duplication | if ($config['group'] !== 'default') { |
|
| 86 | // Load and validate default config group |
||
| 87 | if (! is_array($default_config = Kohana::config('pagination.default'))) { |
||
| 88 | throw new Kohana_Exception('pagination.undefined_group', 'default'); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Merge config group with default config group |
||
| 92 | $group_config += $default_config; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Merge custom config items with config group |
||
| 96 | $config += $group_config; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Assign config values to the object |
||
| 100 | foreach ($config as $key => $value) { |
||
| 101 | if (property_exists($this, $key)) { |
||
| 102 | $this->$key = $value; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // Clean view directory |
||
| 107 | $this->directory = trim($this->directory, '/').'/'; |
||
| 108 | |||
| 109 | // Build generic URL with page in query string |
||
| 110 | if ($this->query_string !== '') { |
||
| 111 | // Extract current page |
||
| 112 | $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; |
||
| 113 | |||
| 114 | // Insert {page} placeholder |
||
| 115 | $_GET[$this->query_string] = '{page}'; |
||
| 116 | |||
| 117 | // Create full URL |
||
| 118 | $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url; |
||
| 119 | $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); |
||
| 120 | |||
| 121 | // Reset page number |
||
| 122 | $_GET[$this->query_string] = $this->current_page; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Build generic URL with page as URI segment |
||
| 126 | else { |
||
| 127 | // Use current URI if no base_url set |
||
| 128 | $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); |
||
| 129 | |||
| 130 | // Convert uri 'label' to corresponding integer if needed |
||
| 131 | if (is_string($this->uri_segment)) { |
||
| 132 | if (($key = array_search($this->uri_segment, $this->url)) === false) { |
||
| 133 | // If uri 'label' is not found, auto add it to base_url |
||
| 134 | $this->url[] = $this->uri_segment; |
||
| 135 | $this->uri_segment = count($this->url) + 1; |
||
| 136 | } else { |
||
| 137 | $this->uri_segment = $key + 2; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // Insert {page} placeholder |
||
| 142 | $this->url[$this->uri_segment - 1] = '{page}'; |
||
| 143 | |||
| 144 | // Create full URL |
||
| 145 | $this->url = url::site(implode('/', $this->url)).Router::$query_string; |
||
| 146 | |||
| 147 | // Extract current page |
||
| 148 | $this->current_page = URI::instance()->segment($this->uri_segment); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Core pagination values |
||
| 152 | $this->total_items = (int) max(0, $this->total_items); |
||
| 153 | $this->items_per_page = (int) max(1, $this->items_per_page); |
||
| 154 | $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); |
||
| 155 | $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); |
||
| 156 | $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); |
||
| 157 | $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); |
||
| 158 | |||
| 159 | // If there is no first/last/previous/next page, relative to the |
||
| 160 | // current page, value is set to FALSE. Valid page number otherwise. |
||
| 161 | $this->first_page = ($this->current_page === 1) ? false : 1; |
||
| 162 | $this->last_page = ($this->current_page >= $this->total_pages) ? false : $this->total_pages; |
||
| 163 | $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : false; |
||
| 164 | $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : false; |
||
| 165 | |||
| 166 | // SQL values |
||
| 167 | $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; |
||
| 168 | $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); |
||
| 169 | } |
||
| 170 | |||
| 229 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.