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 |
||
| 7 | View Code Duplication | class Dimension extends Model |
|
|
|
|||
| 8 | { |
||
| 9 | |||
| 10 | protected $id = null; |
||
| 11 | |||
| 12 | protected $name = null; |
||
| 13 | |||
| 14 | protected $mappingClasses = []; |
||
| 15 | |||
| 16 | protected $propNameMap = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Retrieve the id property |
||
| 20 | * |
||
| 21 | * @return string|null |
||
| 22 | */ |
||
| 23 | 6 | public function getId() |
|
| 27 | |||
| 28 | /** |
||
| 29 | * Set the id property |
||
| 30 | * |
||
| 31 | * @param string $id |
||
| 32 | * @return $this |
||
| 33 | */ |
||
| 34 | 4 | public function setId($id) |
|
| 35 | { |
||
| 36 | 4 | $this->id = $id; |
|
| 37 | 4 | return $this; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Retrieve the name property |
||
| 42 | * |
||
| 43 | * @return string|null |
||
| 44 | */ |
||
| 45 | 6 | public function getName() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set the name property |
||
| 52 | * |
||
| 53 | * @param string $name |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | 4 | public function setName($name) |
|
| 61 | } |
||
| 62 |
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.