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 |
||
| 15 | View Code Duplication | class Tax extends Base |
|
|
|
|||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $id; |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $tax; |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $name; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return int |
||
| 32 | */ |
||
| 33 | public function getId() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param int $id |
||
| 40 | * |
||
| 41 | * @return Tax |
||
| 42 | */ |
||
| 43 | public function setId($id) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getTax() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $tax |
||
| 60 | * |
||
| 61 | * @return Tax |
||
| 62 | */ |
||
| 63 | public function setTax($tax) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getName() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $name |
||
| 80 | * |
||
| 81 | * @return Tax |
||
| 82 | */ |
||
| 83 | public function setName($name) |
||
| 89 | } |
||
| 90 |
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.