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 |
||
10 | class PaginatorExtension extends \Twig_Extension |
||
11 | { |
||
12 | private $app; |
||
13 | |||
14 | /** |
||
15 | * @param Application $app |
||
16 | */ |
||
17 | public function __construct(Application $app) |
||
18 | { |
||
19 | $this->app = $app; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @return string |
||
24 | */ |
||
25 | public function getName() |
||
29 | |||
30 | /** |
||
31 | * @return \Twig_SimpleFunction[] |
||
32 | */ |
||
33 | public function getFunctions() |
||
34 | { |
||
35 | return array( |
||
36 | new \Twig_SimpleFunction( |
||
37 | 'paginator_limit_per_page_render', |
||
38 | array( |
||
39 | $this, |
||
40 | 'paginatorLimitPerPageRender', |
||
41 | ), |
||
42 | array( |
||
43 | 'is_safe' => array('html'), |
||
44 | ) |
||
45 | ), |
||
46 | new \Twig_SimpleFunction( |
||
47 | 'paginator_search_render', |
||
48 | array( |
||
49 | $this, |
||
50 | 'paginatorSearchRender', |
||
51 | ), |
||
52 | array( |
||
53 | 'is_safe' => array('html'), |
||
54 | ) |
||
55 | ), |
||
56 | new \Twig_SimpleFunction( |
||
57 | 'paginator_top_render', |
||
58 | array( |
||
59 | $this, |
||
60 | 'paginatorTopRender', |
||
61 | ), |
||
62 | array( |
||
63 | 'is_safe' => array('html'), |
||
64 | ) |
||
65 | ), |
||
66 | new \Twig_SimpleFunction( |
||
67 | 'paginator_pagination_render', |
||
68 | array( |
||
69 | $this, |
||
70 | 'paginatorPaginationRender', |
||
71 | ), |
||
72 | array( |
||
73 | 'is_safe' => array('html'), |
||
74 | ) |
||
75 | ), |
||
76 | new \Twig_SimpleFunction( |
||
77 | 'paginator_results_text_render', |
||
78 | array( |
||
79 | $this, |
||
80 | 'paginatorResultsTextRender', |
||
81 | ), |
||
82 | array( |
||
83 | 'is_safe' => array('html'), |
||
84 | ) |
||
85 | ), |
||
86 | new \Twig_SimpleFunction( |
||
87 | 'paginator_bottom_render', |
||
88 | array( |
||
89 | $this, |
||
90 | 'paginatorBottomRender', |
||
91 | ), |
||
92 | array( |
||
93 | 'is_safe' => array('html'), |
||
94 | ) |
||
95 | ), |
||
96 | new \Twig_SimpleFunction( |
||
97 | 'paginator_sortable', |
||
98 | array( |
||
99 | $this, |
||
100 | 'paginatorSortable', |
||
101 | ), |
||
102 | array( |
||
103 | 'is_safe' => array('html'), |
||
104 | ) |
||
105 | ), |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $pagination |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public function paginatorPaginationRender($pagination) |
||
115 | { |
||
116 | if (!$pagination) { |
||
117 | return ''; |
||
118 | } |
||
119 | |||
120 | $output = ''; |
||
121 | |||
122 | $paginationData = $pagination->getPaginationData(); |
||
123 | $maxPageRange = isset($paginationData['pageRangeLimit']) |
||
124 | ? intval($paginationData['pageRangeLimit']) |
||
125 | : 10 |
||
126 | ; |
||
127 | $route = $paginationData['route']; |
||
128 | $routeParameters = $this->app['request']->query->all(); |
||
129 | if (isset($paginationData['routeParameters'])) { |
||
130 | $routeParameters = array_merge( |
||
131 | $routeParameters, |
||
132 | $paginationData['routeParameters'] |
||
133 | ); |
||
134 | } |
||
135 | $pageCount = ceil( |
||
136 | intval($paginationData['totalCount']) / |
||
137 | intval($paginationData['numItemsPerPage']) |
||
138 | ); |
||
139 | $currentPage = intval($paginationData['current']); |
||
140 | |||
141 | if ($pageCount > 1) { |
||
142 | $pageRange = range(1, $pageCount); |
||
143 | |||
144 | // Page range by max page numbers |
||
145 | $pageRangeTmp = array(); |
||
146 | $rangeFrom = $currentPage - ceil($maxPageRange / 2); |
||
147 | $rangeTo = $currentPage + ceil($maxPageRange / 2); |
||
148 | |||
149 | foreach (range($rangeFrom, $rangeTo) as $singleRangePage) { |
||
150 | if (in_array($singleRangePage, $pageRange)) { |
||
151 | $pageRangeTmp[] = $singleRangePage; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $pageRange = $pageRangeTmp; |
||
156 | // Page range by max page numbers /END |
||
157 | |||
158 | // Prev |
||
159 | View Code Duplication | if ($currentPage > 1) { |
|
|
|||
160 | $routeParameters = array_merge( |
||
161 | $routeParameters, |
||
162 | array( |
||
163 | $pagination->getPaginatorOption('pageParameterName') => $currentPage - 1, |
||
164 | ) |
||
165 | ); |
||
166 | |||
167 | $prevUrl = $this->app['url_generator']->generate( |
||
168 | $route, |
||
169 | $routeParameters |
||
170 | ); |
||
171 | } else { |
||
172 | $prevUrl = '#'; |
||
173 | } |
||
174 | // Prev /END |
||
175 | |||
176 | // Next |
||
177 | View Code Duplication | if ($currentPage < $pageCount) { |
|
178 | $routeParameters = array_merge( |
||
179 | $routeParameters, |
||
180 | array( |
||
181 | $pagination->getPaginatorOption('pageParameterName') => $currentPage + 1, |
||
182 | ) |
||
183 | ); |
||
184 | |||
185 | $nextUrl = $this->app['url_generator']->generate( |
||
186 | $route, |
||
187 | $routeParameters |
||
188 | ); |
||
189 | } else { |
||
190 | $nextUrl = '#'; |
||
191 | } |
||
192 | // Next /END |
||
193 | |||
194 | $output = $this->app['twig']->render( |
||
195 | 'twig/paginator/pagination.html.twig', |
||
196 | array( |
||
197 | 'app' => $this->app, |
||
198 | 'prevUrl' => $prevUrl, |
||
199 | 'nextUrl' => $nextUrl, |
||
200 | 'pageRange' => $pageRange, |
||
201 | 'routeParameters' => $routeParameters, |
||
202 | 'pagination' => $pagination, |
||
203 | 'route' => $route, |
||
204 | 'currentPage' => $currentPage, |
||
205 | 'pageCount' => $pageCount, |
||
206 | ) |
||
207 | ); |
||
208 | } |
||
209 | |||
210 | return $output; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param $pagination |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function paginatorResultsTextRender($pagination) |
||
219 | { |
||
220 | if (!$pagination) { |
||
221 | return ''; |
||
222 | } |
||
223 | |||
224 | $output = ''; |
||
225 | |||
226 | $paginationData = $pagination->getPaginationData(); |
||
227 | $pageCount = ceil( |
||
228 | intval($paginationData['totalCount']) / |
||
229 | intval($paginationData['numItemsPerPage']) |
||
230 | ); |
||
231 | $currentPage = intval($paginationData['current']); |
||
232 | $total = $paginationData['totalCount']; |
||
233 | |||
234 | if ($total > 0 && $currentPage <= $pageCount) { |
||
235 | $from = (($currentPage - 1) * $paginationData['numItemsPerPage']) + 1; |
||
236 | $to = $currentPage * $paginationData['numItemsPerPage'] > $total |
||
237 | ? $total |
||
238 | : $currentPage * $paginationData['numItemsPerPage'] |
||
239 | ; |
||
240 | |||
241 | $output = $this->app['twig']->render( |
||
242 | 'twig/paginator/results-text.html.twig', |
||
243 | array( |
||
244 | 'app' => $this->app, |
||
245 | 'from' => $from, |
||
246 | 'to' => $to, |
||
247 | 'total' => $total, |
||
248 | ) |
||
249 | ); |
||
250 | } |
||
251 | |||
252 | return $output; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param $pagination |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function paginatorBottomRender($pagination) |
||
269 | |||
270 | /** |
||
271 | * @param $pagination |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function paginatorLimitPerPageRender($pagination) |
||
297 | |||
298 | /** |
||
299 | * @param $pagination |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function paginatorSearchRender($pagination) |
||
313 | |||
314 | /** |
||
315 | * @param $pagination |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | public function paginatorTopRender($pagination) |
||
328 | |||
329 | /** |
||
330 | * @param $pagination |
||
331 | * @param $text |
||
332 | * @param $key |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | public function paginatorSortable($pagination, $text = '', $key = '') |
||
395 | } |
||
396 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.