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  | 
            ||
| 24 | class GridBuilder  | 
            ||
| 25 | { | 
            ||
| 26 | /** @var ActionFactory */  | 
            ||
| 27 | protected $actionFactory;  | 
            ||
| 28 | |||
| 29 | /** @var LineFactory */  | 
            ||
| 30 | protected $titleLineFactory;  | 
            ||
| 31 | |||
| 32 | /** @var LineFactory */  | 
            ||
| 33 | protected $lineFactory;  | 
            ||
| 34 | |||
| 35 | /** @var PagerFactory */  | 
            ||
| 36 | protected $pagerFactory;  | 
            ||
| 37 | |||
| 38 | /** @var DataCollectionInterface */  | 
            ||
| 39 | protected $dataCollection;  | 
            ||
| 40 | |||
| 41 | /** @var ManialinkInterface */  | 
            ||
| 42 | protected $manialink;  | 
            ||
| 43 | |||
| 44 | /** @var ManialinkFactory */  | 
            ||
| 45 | protected $manialinkFactory;  | 
            ||
| 46 | |||
| 47 | /** @var AbstractColumn[] */  | 
            ||
| 48 | protected $columns;  | 
            ||
| 49 | |||
| 50 | /** @var float */  | 
            ||
| 51 | protected $totalWidthCoefficency = 0;  | 
            ||
| 52 | |||
| 53 | /** @var int */  | 
            ||
| 54 | protected $currentPage = 1;  | 
            ||
| 55 | |||
| 56 | /** @var string */  | 
            ||
| 57 | protected $pageKey;  | 
            ||
| 58 | |||
| 59 | /** @var Action */  | 
            ||
| 60 | protected $actionPreviousPage;  | 
            ||
| 61 | /** @var Action */  | 
            ||
| 62 | protected $actionNextPage;  | 
            ||
| 63 | /** @var Action */  | 
            ||
| 64 | protected $actionLastPage;  | 
            ||
| 65 | /** @var Action */  | 
            ||
| 66 | protected $actionFirstPage;  | 
            ||
| 67 | |||
| 68 | /** @var Action[] */  | 
            ||
| 69 | protected $temporaryActions = [];  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * GridBuilder constructor.  | 
            ||
| 73 | *  | 
            ||
| 74 | * @param ActionFactory $actionFactory  | 
            ||
| 75 | * @param LineFactory $lineFactory  | 
            ||
| 76 | * @param LineFactory $titleLineFactory  | 
            ||
| 77 | * @param PagerFactory $pagerFactory  | 
            ||
| 78 | */  | 
            ||
| 79 | 5 | View Code Duplication | public function __construct(  | 
            
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * Set the data collection.  | 
            ||
| 95 | *  | 
            ||
| 96 | * @param DataCollectionInterface $dataCollection  | 
            ||
| 97 | *  | 
            ||
| 98 | * @return $this  | 
            ||
| 99 | */  | 
            ||
| 100 | 5 | public function setDataCollection(DataCollectionInterface $dataCollection)  | 
            |
| 106 | |||
| 107 | /**  | 
            ||
| 108 | * Set the manialink the content is generated for.  | 
            ||
| 109 | *  | 
            ||
| 110 | * @param ManialinkInterface $manialink  | 
            ||
| 111 | *  | 
            ||
| 112 | * @return $this  | 
            ||
| 113 | */  | 
            ||
| 114 | 5 | public function setManialink(ManialinkInterface $manialink)  | 
            |
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Set the manialink factory responsible with the manialink.  | 
            ||
| 132 | *  | 
            ||
| 133 | * @param ManialinkFactory $manialinkFactory  | 
            ||
| 134 | *  | 
            ||
| 135 | * @return $this  | 
            ||
| 136 | */  | 
            ||
| 137 | 5 | public function setManialinkFactory($manialinkFactory)  | 
            |
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * @param string $key  | 
            ||
| 146 | * @param string $name  | 
            ||
| 147 | * @param integer $widthCoefficiency  | 
            ||
| 148 | * @param bool $sortable  | 
            ||
| 149 | * @param bool $translatable  | 
            ||
| 150 | *  | 
            ||
| 151 | * @return $this  | 
            ||
| 152 | */  | 
            ||
| 153 | 5 | public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false)  | 
            |
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * Add an action into a column.  | 
            ||
| 162 | *  | 
            ||
| 163 | * @param string $key  | 
            ||
| 164 | * @param string $name  | 
            ||
| 165 | * @param integer $widthCoefficiency  | 
            ||
| 166 | * @param $action  | 
            ||
| 167 | * @param Label $renderer  | 
            ||
| 168 | */  | 
            ||
| 169 | 5 | public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer)  | 
            |
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Remove all columns.  | 
            ||
| 176 | */  | 
            ||
| 177 | 1 | public function resetColumns()  | 
            |
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * Build a grid.  | 
            ||
| 185 | *  | 
            ||
| 186 | * @param double $width  | 
            ||
| 187 | * @param double $height  | 
            ||
| 188 | *  | 
            ||
| 189 | * @return Frame  | 
            ||
| 190 | */  | 
            ||
| 191 | 5 | public function build($width, $height)  | 
            |
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * Action callback to go to the first page.  | 
            ||
| 271 | */  | 
            ||
| 272 | 1 | public function goToFirstPage()  | 
            |
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * Action callback to go to the previous page.  | 
            ||
| 279 | */  | 
            ||
| 280 | 1 | public function goToPreviousPage()  | 
            |
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Action callback to go to the next page.  | 
            ||
| 289 | */  | 
            ||
| 290 | 3 | public function goToNextPage()  | 
            |
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Action callback to go to the last page.  | 
            ||
| 299 | */  | 
            ||
| 300 | 1 | public function goToLastPage()  | 
            |
| 304 | |||
| 305 | /**  | 
            ||
| 306 | * Handle page change & refresh user window.  | 
            ||
| 307 | *  | 
            ||
| 308 | * @param integer $page  | 
            ||
| 309 | */  | 
            ||
| 310 | 4 | protected function changePage($page)  | 
            |
| 315 | }  | 
            
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.