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:
Complex classes like GridBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GridBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class GridBuilder |
||
| 27 | { |
||
| 28 | |||
| 29 | /** @var ActionFactory */ |
||
| 30 | protected $actionFactory; |
||
| 31 | |||
| 32 | /** @var TitleLineFactory */ |
||
| 33 | protected $titleLineFactory; |
||
| 34 | |||
| 35 | /** @var LineFactory */ |
||
| 36 | protected $lineFactory; |
||
| 37 | |||
| 38 | /** @var PagerFactory */ |
||
| 39 | protected $pagerFactory; |
||
| 40 | |||
| 41 | /** @var Factory */ |
||
| 42 | protected $uiFactory; |
||
| 43 | |||
| 44 | /** @var DataCollectionInterface */ |
||
| 45 | protected $dataCollection; |
||
| 46 | |||
| 47 | /** @var ManialinkInterface */ |
||
| 48 | protected $manialink; |
||
| 49 | |||
| 50 | /** @var ManialinkFactory */ |
||
| 51 | protected $manialinkFactory; |
||
| 52 | |||
| 53 | /** @var AbstractColumn[] */ |
||
| 54 | protected $columns; |
||
| 55 | |||
| 56 | /** @var float */ |
||
| 57 | protected $totalWidthCoefficency = 0.; |
||
| 58 | |||
| 59 | /** @var int */ |
||
| 60 | protected $currentPage = 1; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | protected $pageKey; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | protected $actionPreviousPage; |
||
| 67 | /** @var string */ |
||
| 68 | protected $actionNextPage; |
||
| 69 | /** @var string */ |
||
| 70 | protected $actionLastPage; |
||
| 71 | /** @var string */ |
||
| 72 | protected $actionFirstPage; |
||
| 73 | /** @var string */ |
||
| 74 | protected $actionGotoPage; |
||
| 75 | /** @var string[] */ |
||
| 76 | protected $temporaryActions = []; |
||
| 77 | |||
| 78 | /** @var array */ |
||
| 79 | protected $temporaryEntries = []; |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * GridBuilder constructor. |
||
| 84 | * |
||
| 85 | * @param ActionFactory $actionFactory |
||
| 86 | * @param LineFactory $lineFactory |
||
| 87 | * @param TitleLineFactory $titleLineFactory |
||
| 88 | * @param PagerFactory $pagerFactory |
||
| 89 | */ |
||
| 90 | 5 | View Code Duplication | public function __construct( |
| 105 | |||
| 106 | /** |
||
| 107 | * Set the data collection. |
||
| 108 | * |
||
| 109 | * @param DataCollectionInterface $dataCollection |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 5 | public function setDataCollection(DataCollectionInterface $dataCollection) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Set the manialink the content is generated for. |
||
| 122 | * |
||
| 123 | * @param ManialinkInterface $manialink |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | 5 | public function setManialink(ManialinkInterface $manialink) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Set the manialink factory responsible with the manialink. |
||
| 147 | * |
||
| 148 | * @param ManialinkFactory $manialinkFactory |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | 5 | public function setManialinkFactory($manialinkFactory) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $key |
||
| 161 | * @param string $name |
||
| 162 | * @param integer $widthCoefficiency |
||
| 163 | * @param bool $sortable |
||
| 164 | * @param bool $translatable |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | 5 | public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $key |
||
| 177 | * @param string $name |
||
| 178 | * @param integer $widthCoefficiency |
||
| 179 | * @param bool $sortable |
||
| 180 | * @param bool $translatable |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function addInputColumn($key, $name, $widthCoefficiency) |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Add an action into a column. |
||
| 194 | * |
||
| 195 | * @param string $key |
||
| 196 | * @param string $name |
||
| 197 | * @param integer $widthCoefficiency |
||
| 198 | * @param $action |
||
| 199 | * @param Renderable $renderer |
||
| 200 | * |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | 5 | public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Remove all columns. |
||
| 212 | */ |
||
| 213 | public function resetColumns() |
||
| 214 | { |
||
| 215 | $this->columns = []; |
||
| 216 | $this->totalWidthCoefficency = 0.; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Build a grid. |
||
| 221 | * |
||
| 222 | * @param double $width |
||
| 223 | * @param double $height |
||
| 224 | * |
||
| 225 | * @return Frame |
||
| 226 | */ |
||
| 227 | 5 | public function build($width, $height) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Action callback to go to the first page. |
||
| 336 | */ |
||
| 337 | 1 | public function goToFirstPage($login = null, $entries = []) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Action callback to go to the previous page. |
||
| 345 | */ |
||
| 346 | 1 | public function goToPreviousPage($login = null, $entries = []) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Action callback to go to the next page. |
||
| 356 | */ |
||
| 357 | 3 | public function goToNextPage($login = null, $entries = []) |
|
| 364 | |||
| 365 | public function goToPage($login = null, $entries = []) |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Action callback to go to the last page. |
||
| 382 | */ |
||
| 383 | 1 | public function goToLastPage($login = null, $entries = []) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Updates dataCollection from entries. |
||
| 391 | */ |
||
| 392 | 4 | public function updateDataCollection($entries) |
|
| 420 | |||
| 421 | /** get dataCollection |
||
| 422 | * |
||
| 423 | * @return DataCollectionInterface |
||
| 424 | */ |
||
| 425 | public function getDataCollection() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Handle page change & refresh user window. |
||
| 432 | * |
||
| 433 | * @param integer $page |
||
| 434 | */ |
||
| 435 | 4 | protected function changePage($page) |
|
| 440 | |||
| 441 | public function sortColumn($login, $entries, $args) |
||
| 458 | } |
||
| 459 |
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.