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 |
||
| 11 | class Layout |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Collection |
||
| 15 | */ |
||
| 16 | protected $columns; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Column |
||
| 20 | */ |
||
| 21 | protected $current; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Form |
||
| 25 | */ |
||
| 26 | protected $parent; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Layout constructor. |
||
| 30 | * |
||
| 31 | * @param Form $form |
||
| 32 | */ |
||
| 33 | public function __construct(Form $form) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Add a filter to layout column. |
||
| 44 | * |
||
| 45 | * @param Form\Field $field |
||
| 46 | */ |
||
| 47 | public function addField(Form\Field $field) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Add a new column in layout. |
||
| 54 | * |
||
| 55 | * @param int $width |
||
| 56 | * @param \Closure $closure |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function column($width, \Closure $closure) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get all columns in filter layout. |
||
| 77 | * |
||
| 78 | * @return Collection |
||
| 79 | */ |
||
| 80 | public function columns() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Remove reserved fields from form layout. |
||
| 91 | * |
||
| 92 | * @param array $fields |
||
| 93 | */ |
||
| 94 | public function removeReservedFields(array $fields) |
||
| 104 | } |
||
| 105 |
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.