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 |
||
| 22 | class Renderer |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The template renderer. |
||
| 26 | * |
||
| 27 | * Will be used to render HTML code for links. |
||
| 28 | * |
||
| 29 | * @var TemplateRenderer |
||
| 30 | */ |
||
| 31 | protected $xRenderer = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The Jaxon request to be paginated |
||
| 35 | * |
||
| 36 | * @var Request |
||
| 37 | */ |
||
| 38 | protected $xRequest = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $previousText = '«'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $nextText = '»'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $ellipsysText = '...'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | protected $totalPages = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var integer |
||
| 62 | */ |
||
| 63 | protected $currentPage = 0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The class contructor |
||
| 67 | * |
||
| 68 | * @param TemplateRenderer $xRenderer |
||
| 69 | */ |
||
| 70 | public function __construct(TemplateRenderer $xRenderer) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Set the text for the previous page link |
||
| 77 | * |
||
| 78 | * @param string $text The text for the previous page link |
||
| 79 | * |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function setPreviousText($text) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set the text for the next page link |
||
| 89 | * |
||
| 90 | * @param string $text The text for the previous page link |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function setNextText($text) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set the request to be paginated |
||
| 101 | * |
||
| 102 | * @param Request $xRequest The request to be paginated |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | public function setRequest(Request $xRequest) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the js call to a given page |
||
| 118 | * |
||
| 119 | * @param int $pageNum The page number |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | protected function getPageCall($pageNum) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Render the previous link. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | View Code Duplication | protected function getPrevLink() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Render the next link. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | View Code Duplication | protected function getNextLink() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Render the pagination links. |
||
| 162 | * |
||
| 163 | * @param integer $nNumber The page number |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | protected function getLink($nNumber) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Render an HTML pagination control. |
||
| 188 | * |
||
| 189 | * @param array $aPageNumbers The page numbers to be rendered |
||
| 190 | * @param integer $currentPage The current page number |
||
| 191 | * @param integer $totalPages The total number of pages |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function render(array $aPageNumbers, $currentPage, $totalPages) |
||
| 212 | } |
||
| 213 |