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 |
||
| 25 | View Code Duplication | class Label |
|
|
|
|||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @ORM\Id |
||
| 29 | * @ORM\GeneratedValue |
||
| 30 | * @ORM\Column(type="integer") |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $id = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @ORM\Column(type="string", length=16) |
||
| 38 | * @Assert\NotBlank() |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $name = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @ORM\ManyToMany(targetEntity="Item", mappedBy="labels") |
||
| 46 | * |
||
| 47 | * @var ArrayCollection |
||
| 48 | */ |
||
| 49 | protected $items; |
||
| 50 | |||
| 51 | 86 | public function __construct() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return int |
||
| 58 | */ |
||
| 59 | 1 | public function getId() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $name |
||
| 66 | * |
||
| 67 | * @return Label |
||
| 68 | */ |
||
| 69 | 2 | public function setName($name) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 2 | public function getName() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param Item $item |
||
| 86 | * |
||
| 87 | * @return Label |
||
| 88 | */ |
||
| 89 | 1 | public function addItem(Item $item) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param Item $item |
||
| 101 | * |
||
| 102 | * @return Label |
||
| 103 | */ |
||
| 104 | 1 | public function removeItem(Item $item) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return ArrayCollection |
||
| 116 | */ |
||
| 117 | 1 | public function getItems() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 1 | public function __toString() |
|
| 129 | } |
||
| 130 |
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.