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 |
||
| 27 | class GridBuilder |
||
| 28 | { |
||
| 29 | /** @var ActionFactory */ |
||
| 30 | protected $actionFactory; |
||
| 31 | |||
| 32 | /** @var LineFactory */ |
||
| 33 | protected $titleLineFactory; |
||
| 34 | |||
| 35 | /** @var LineFactory */ |
||
| 36 | protected $lineFactory; |
||
| 37 | |||
| 38 | /** @var PagerFactory */ |
||
| 39 | protected $pagerFactory; |
||
| 40 | |||
| 41 | /** @var DataCollectionInterface */ |
||
| 42 | protected $dataCollection; |
||
| 43 | |||
| 44 | /** @var ManialinkInterface */ |
||
| 45 | protected $manialink; |
||
| 46 | |||
| 47 | /** @var ManialinkFactory */ |
||
| 48 | protected $manialinkFactory; |
||
| 49 | |||
| 50 | /** @var AbstractColumn[] */ |
||
| 51 | protected $columns; |
||
| 52 | |||
| 53 | /** @var float */ |
||
| 54 | protected $totalWidthCoefficency = 0; |
||
| 55 | |||
| 56 | /** @var int */ |
||
| 57 | protected $currentPage = 1; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | protected $pageKey; |
||
| 61 | |||
| 62 | /** @var Action */ |
||
| 63 | protected $actionPreviousPage; |
||
| 64 | /** @var Action */ |
||
| 65 | protected $actionNextPage; |
||
| 66 | /** @var Action */ |
||
| 67 | protected $actionLastPage; |
||
| 68 | /** @var Action */ |
||
| 69 | protected $actionFirstPage; |
||
| 70 | |||
| 71 | /** @var Action[] */ |
||
| 72 | protected $temporaryActions = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * GridBuilder constructor. |
||
| 76 | * |
||
| 77 | * @param ActionFactory $actionFactory |
||
| 78 | * @param LineFactory $lineFactory |
||
| 79 | * @param LineFactory $titleLineFactory |
||
| 80 | * @param PagerFactory $pagerFactory |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function __construct( |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Set the data collection. |
||
| 98 | * |
||
| 99 | * @param DataCollectionInterface $dataCollection |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function setDataCollection(DataCollectionInterface $dataCollection) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set the manialink the content is generated for. |
||
| 112 | * |
||
| 113 | * @param ManialinkInterface $manialink |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function setManialink(ManialinkInterface $manialink) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the manialink factory responsible with the manialink. |
||
| 135 | * |
||
| 136 | * @param ManialinkFactory $manialinkFactory |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | public function setManialinkFactory($manialinkFactory) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $key |
||
| 149 | * @param $name |
||
| 150 | * @param $widthCoefficiency |
||
| 151 | * @param bool $sortable |
||
| 152 | * @param bool $translatable |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Add an action into a column. |
||
| 165 | * |
||
| 166 | * @param $key |
||
| 167 | * @param $name |
||
| 168 | * @param $widthCoefficiency |
||
| 169 | * @param $action |
||
| 170 | * @param $renderer |
||
| 171 | */ |
||
| 172 | public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Remove all columns. |
||
| 179 | */ |
||
| 180 | public function resetColumns() |
||
| 185 | |||
| 186 | public function build($width, $height) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Action callback to go to the first page. |
||
| 266 | */ |
||
| 267 | public function goToFirstPage() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Action callback to go to the previous page. |
||
| 274 | */ |
||
| 275 | public function goToPreviousPage() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Action callback to go to the next page. |
||
| 284 | */ |
||
| 285 | public function goToNextPage() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Action callback to go to the last page. |
||
| 294 | */ |
||
| 295 | public function goToLastPage() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Handle page change & refresh user window. |
||
| 302 | * |
||
| 303 | * @param $page |
||
| 304 | */ |
||
| 305 | protected function changePage($page) |
||
| 310 | } |
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.