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 |
||
37 | class PMF_Pagination |
||
38 | { |
||
39 | /** |
||
40 | * Template vars |
||
41 | */ |
||
42 | const TPL_VAR_LINK_URL = '{LINK_URL}'; |
||
43 | const TPL_VAR_LINK_TEXT = '{LINK_TEXT}'; |
||
44 | const TPL_VAR_LAYOUT_CONTENT = '{LAYOUT_CONTENT}'; |
||
45 | |||
46 | /** |
||
47 | * Base url used for links |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $baseUrl = ''; |
||
52 | |||
53 | /** |
||
54 | * Total items count |
||
55 | * |
||
56 | * @var integer |
||
57 | */ |
||
58 | protected $total = 0; |
||
59 | |||
60 | /** |
||
61 | * Items per page count |
||
62 | * |
||
63 | * @var integer |
||
64 | */ |
||
65 | protected $perPage = 0; |
||
66 | |||
67 | /** |
||
68 | * Number of adjacent links |
||
69 | * |
||
70 | * @var integer |
||
71 | */ |
||
72 | protected $adjacents = 4; |
||
73 | |||
74 | /** |
||
75 | * Default link template. |
||
76 | * Possible variables are {LINK}, {TITLE}, {TEXT} |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $linkTpl = '<li><a href="{LINK_URL}">{LINK_TEXT}</a></li>'; |
||
81 | |||
82 | /** |
||
83 | * Current page link template |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $currentPageLinkTpl = '<li class="active"><a href="{LINK_URL}">{LINK_TEXT}</a></li>'; |
||
88 | |||
89 | /** |
||
90 | * Next page link template |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $nextPageLinkTpl = '<li><a href="{LINK_URL}">→</a></li>'; |
||
95 | |||
96 | /** |
||
97 | * Previous page link template |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $prevPageLinkTpl = '<li><a href="{LINK_URL}">←</a></li>'; |
||
102 | |||
103 | /** |
||
104 | * First page link template |
||
105 | * |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $firstPageLinkTpl = '<li><a href="{LINK_URL}">⇤</a></li>'; |
||
109 | |||
110 | /** |
||
111 | * Last page link template |
||
112 | * |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $lastPageLinkTpl = '<li><a href="{LINK_URL}">⇥</a></li>'; |
||
116 | |||
117 | /** |
||
118 | * Layout template |
||
119 | * |
||
120 | * @var string |
||
121 | */ |
||
122 | protected $layoutTpl = '<div class="text-center"><ul class="pagination">{LAYOUT_CONTENT}</ul></div>'; |
||
123 | |||
124 | /** |
||
125 | * Current page index |
||
126 | * |
||
127 | * @var integer |
||
128 | */ |
||
129 | protected $currentPage = 0; |
||
130 | |||
131 | /** |
||
132 | * Param name to associate the page numbers to |
||
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $pageParamName = 'page'; |
||
137 | |||
138 | /** |
||
139 | * SEO name |
||
140 | * |
||
141 | * @var string |
||
142 | */ |
||
143 | protected $seoName = ''; |
||
144 | |||
145 | /** |
||
146 | * Use rewritten URLs without GET variables |
||
147 | * |
||
148 | * @var boolean |
||
149 | */ |
||
150 | protected $useRewrite = false; |
||
151 | |||
152 | /** |
||
153 | * Rewritten URL format for page param |
||
154 | * |
||
155 | * @var string |
||
156 | */ |
||
157 | protected $rewriteUrl = ''; |
||
158 | |||
159 | /** |
||
160 | * @var PMF_Configuration |
||
161 | */ |
||
162 | private $_config; |
||
163 | |||
164 | /** |
||
165 | * Constructor |
||
166 | * |
||
167 | * We read in the current page from the baseUrl, so if it contains |
||
168 | * no pageParamName, first page is asumed |
||
169 | * |
||
170 | * @param PMF_Configuration $config |
||
171 | * @param array $options initialization options, |
||
172 | * possible options: |
||
173 | * - baseUrl (default "") |
||
174 | * - total |
||
175 | * - perPage |
||
176 | * - linkTpl |
||
177 | * - currentPageLinkTpl |
||
178 | * - nextPageLinkTpl |
||
179 | * - prevPageLinkTpl |
||
180 | * - firstPageLinkTpl |
||
181 | * - lastPageLinkTpl |
||
182 | * - layoutTpl |
||
183 | * - pageParamName (default "page") |
||
184 | * - useRewrite |
||
185 | * |
||
186 | * @return PMF_Pagination |
||
187 | */ |
||
188 | public function __construct(PMF_Configuration $config, Array $options = null) |
||
248 | |||
249 | /** |
||
250 | * Returns the current page URL |
||
251 | * |
||
252 | * @param string $url URL |
||
253 | * |
||
254 | * @return integer |
||
255 | */ |
||
256 | protected function getCurrentPageFromUrl($url) |
||
269 | |||
270 | /** |
||
271 | * Render full pagination string |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function render() |
||
346 | |||
347 | /** |
||
348 | * Render url for a given page |
||
349 | * |
||
350 | * @param string $url url |
||
351 | * @param integer $page page number |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function renderUrl($url, $page) |
||
370 | |||
371 | /** |
||
372 | * Render a link |
||
373 | * |
||
374 | * @param string $tpl link template |
||
375 | * @param string $url url value for template container |
||
376 | * @param string $linkText text value for template container |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function renderLink($tpl, $url, $linkText) |
||
387 | |||
388 | /** |
||
389 | * Render the whole pagination layout |
||
390 | * |
||
391 | * @param string $content layout contents |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function renderLayout($content) |
||
399 | } |