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 |
||
46 | class Cf_PHPPaginator { |
||
47 | /* |
||
48 | * MAIN Settings (required) |
||
49 | */ |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | * @total_records string Total of records to paginate (specify manually) |
||
54 | */ |
||
55 | public $total_records = 0; |
||
56 | |||
57 | |||
58 | /* |
||
59 | * MAIN Settings (optional) |
||
60 | */ |
||
61 | |||
62 | /** |
||
63 | * @style string Specify Class to define |
||
64 | * @tutorial If is specified, it would create <div class="style"> otherwise it not create div |
||
65 | */ |
||
66 | public $style = ''; |
||
67 | |||
68 | /** |
||
69 | * @records number Max records to show on every page |
||
70 | */ |
||
71 | public $records = 20; |
||
72 | |||
73 | /** |
||
74 | * @max_links number Maxium quantity of links to show before and after actual page |
||
75 | * @example If current page is 4 and MAX is 2, it show "2 3 4 5 6" |
||
76 | */ |
||
77 | public $max_links = 3; |
||
78 | |||
79 | /** |
||
80 | * @get_name string Specify INDEX of GET that is used |
||
81 | * |
||
82 | * It will load automatically the current page. |
||
83 | * If you like, you can specify manually in "$cur_page" |
||
84 | * (if you like filter previously or the script will not load directly from URL) |
||
85 | */ |
||
86 | public $get_name = 'pag'; |
||
87 | |||
88 | /** |
||
89 | * @max_records number Specify maxium quantity of registers will be loaded. "0" disable this |
||
90 | */ |
||
91 | public $max_records = 0; |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @recicle_url boolean If is true, modify directly tue current URL to put the pagination (if not exist any, add &pag= to the end) |
||
96 | * If is true, $url_start and $url_end will be ignored |
||
97 | */ |
||
98 | public $recicle_url = true; |
||
99 | |||
100 | /** |
||
101 | * @specific_get array Specify what GETs (and order) you like recicle. |
||
102 | * Set array() to ALL (not recomended to avoid atacks DoS) |
||
103 | * Set '' for any |
||
104 | * @example array('section', 'pag', 'subsection'); |
||
105 | * If you don't need 'pag' in the midle, you can ignore it. |
||
106 | * WARNING: If the order is very important, use the pag at THE END of url |
||
107 | */ |
||
108 | public $specific_get = ''; |
||
109 | |||
110 | /** |
||
111 | * @url_start string specify how the URL of every link start. IF $recicle_url is TRUE, this will be ignored |
||
112 | * @example If you use MOD_REWRITE or JAVASCRIPT, you can put "http://mywebsite.com/subdir/pag-" or "javascript:paginate(" |
||
113 | * |
||
114 | * This value will auto completed if you leave empty |
||
115 | */ |
||
116 | public $url_start = ''; |
||
117 | |||
118 | /** |
||
119 | * @url_end string specify how the URL of every link ends. IF $recicle_url is TRUE, this will be ignored |
||
120 | * @example If you use MOD_REWRITE or JAVASCRIPT, you can put "/" or ");" |
||
121 | * |
||
122 | * This value will auto completed if you leave empty |
||
123 | */ |
||
124 | public $url_end = ''; |
||
125 | |||
126 | |||
127 | |||
128 | /* |
||
129 | * Design settings |
||
130 | */ |
||
131 | |||
132 | // Icons to show "First | Previous | Next | Last" |
||
133 | //& laquo; = � | & lt; = < |
||
134 | //& raquo; = � | & gt; = > |
||
135 | // Leave empty '' to not show or ' ' to show empty |
||
136 | public $first = '[<<]'; |
||
137 | public $previous = '[<]'; |
||
138 | public $next = '[>]'; |
||
139 | public $last = '[>>]'; |
||
140 | |||
141 | |||
142 | /* |
||
143 | * RETURNS FROM PAGINATOR and internal use |
||
144 | */ |
||
145 | // Return current page (auto loaded) |
||
146 | public $cur_page; |
||
147 | |||
148 | // original page loaded |
||
149 | // it contains the real value in GET (ex: -1, 5...) to compare later |
||
150 | public $original_page; |
||
151 | |||
152 | // Return total pages |
||
153 | public $total_pages; |
||
154 | |||
155 | // LIMIT_START / Specify the first record of the page (ex, in page 3 and 20 records is record num 40) |
||
156 | // Limit max is $records |
||
157 | public $first_record; |
||
158 | |||
159 | // Specify limit to put directly in mysql query ( LIMIT 0,20 ) |
||
160 | public $limit; |
||
161 | |||
162 | |||
163 | /* |
||
164 | * Functions |
||
165 | */ |
||
166 | /** |
||
167 | * @name paginate |
||
168 | * @tutorial This function is called directly to load all params |
||
169 | * @example $pag->paginate(); |
||
170 | */ |
||
171 | public function paginate() { |
||
176 | |||
177 | /** |
||
178 | * @name show |
||
179 | * @tutorial Show links of pagination |
||
180 | * @example $pag->show(); |
||
181 | */ |
||
182 | public function show() { |
||
224 | |||
225 | |||
226 | /** |
||
227 | * @name get |
||
228 | * @tutorial This function is autoloaded, it get the current page and filter number to avoid hackers |
||
229 | */ |
||
230 | private function get() { |
||
245 | |||
246 | /** |
||
247 | * @name calculate |
||
248 | * @tutorial Do all calcs about current and last page |
||
249 | */ |
||
250 | private function calculate() { |
||
280 | |||
281 | /** |
||
282 | * @name prepareUrl |
||
283 | * @tutorial It prepare the url to show in each link, specified by user |
||
284 | * If recicle URL is false, it will auto load $url_start and $url_end |
||
285 | */ |
||
286 | private function prepareUrl() { |
||
348 | } |
||
349 | ?> |
||
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: