Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 12 | class Pagination_Core |
||
| 13 | { |
||
| 14 | |||
| 15 | // Config values |
||
| 16 | protected $base_url = ''; |
||
| 17 | protected $directory = 'pagination'; |
||
| 18 | protected $style = 'classic'; |
||
| 19 | protected $uri_segment = 3; |
||
| 20 | protected $query_string = ''; |
||
| 21 | protected $items_per_page = 20; |
||
| 22 | protected $total_items = 0; |
||
| 23 | protected $auto_hide = false; |
||
| 24 | |||
| 25 | // Autogenerated values |
||
| 26 | protected $url; |
||
| 27 | protected $current_page; |
||
| 28 | protected $total_pages; |
||
| 29 | protected $current_first_item; |
||
| 30 | protected $current_last_item; |
||
| 31 | protected $first_page; |
||
| 32 | protected $last_page; |
||
| 33 | protected $previous_page; |
||
| 34 | protected $next_page; |
||
| 35 | protected $sql_offset; |
||
| 36 | protected $sql_limit; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Constructs and returns a new Pagination object. |
||
| 40 | * |
||
| 41 | * @param array configuration settings |
||
| 42 | * @return object |
||
|
|
|||
| 43 | */ |
||
| 44 | public function factory($config = array()) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructs a new Pagination object. |
||
| 51 | * |
||
| 52 | * @param array configuration settings |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function __construct($config = array()) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets config values. |
||
| 70 | * |
||
| 71 | * @throws Kohana_Exception |
||
| 72 | * @param array configuration settings |
||
| 73 | * @return void |
||
| 74 | */ |
||
| 75 | public function initialize($config = array()) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Generates the HTML for the chosen pagination style. |
||
| 173 | * |
||
| 174 | * @param string pagination style |
||
| 175 | * @return string pagination html |
||
| 176 | */ |
||
| 177 | public function render($style = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Magically converts Pagination object to string. |
||
| 195 | * |
||
| 196 | * @return string pagination html |
||
| 197 | */ |
||
| 198 | public function __toString() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Magically gets a pagination variable. |
||
| 205 | * |
||
| 206 | * @param string variable key |
||
| 207 | * @return mixed variable value if the key is found |
||
| 208 | * @return void if the key is not found |
||
| 209 | */ |
||
| 210 | public function __get($key) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Adds a secondary interface for accessing properties, e.g. $pagination->total_pages(). |
||
| 219 | * Note that $pagination->total_pages is the recommended way to access properties. |
||
| 220 | * |
||
| 221 | * @param string function name |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function __call($func, $args = null) |
||
| 228 | } // End Pagination Class |
||
| 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.