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 | View Code Duplication | class Column |
|
|
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Collection |
||
| 15 | */ |
||
| 16 | protected $fields; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $width; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Column constructor. |
||
| 25 | * |
||
| 26 | * @param int $width |
||
| 27 | */ |
||
| 28 | public function __construct($width = 12) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Add a filter to this column. |
||
| 36 | * |
||
| 37 | * @param Field $field |
||
| 38 | */ |
||
| 39 | public function add(Field $field) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Remove fields from column. |
||
| 46 | * |
||
| 47 | * @param $fields |
||
| 48 | */ |
||
| 49 | public function removeFields($fields) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get all filters in this column. |
||
| 58 | * |
||
| 59 | * @return Collection |
||
| 60 | */ |
||
| 61 | public function fields() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set column width. |
||
| 68 | * |
||
| 69 | * @param int $width |
||
| 70 | */ |
||
| 71 | public function setWidth($width) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get column width. |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | public function width() |
||
| 85 | } |
||
| 86 |
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.