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