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 |
||
| 20 | View Code Duplication | class CRoleGroup |
|
|
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var integer |
||
| 24 | * |
||
| 25 | * @ORM\Column(name="iid", type="integer") |
||
| 26 | * @ORM\Id |
||
| 27 | * @ORM\GeneratedValue |
||
| 28 | */ |
||
| 29 | private $iid; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer |
||
| 33 | * |
||
| 34 | * @ORM\Column(name="c_id", type="integer") |
||
| 35 | */ |
||
| 36 | private $cId; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var integer |
||
| 40 | * |
||
| 41 | * @ORM\Column(name="id", type="integer", nullable=true) |
||
| 42 | */ |
||
| 43 | private $id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var integer |
||
| 47 | * |
||
| 48 | * @ORM\Column(name="role_id", type="integer", nullable=false) |
||
| 49 | */ |
||
| 50 | private $roleId; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | * |
||
| 55 | * @ORM\Column(name="scope", type="string", length=20, nullable=false) |
||
| 56 | */ |
||
| 57 | private $scope; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var integer |
||
| 61 | * |
||
| 62 | * @ORM\Column(name="group_id", type="integer") |
||
| 63 | */ |
||
| 64 | private $groupId; |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Set roleId |
||
| 69 | * |
||
| 70 | * @param integer $roleId |
||
| 71 | * @return CRoleGroup |
||
| 72 | */ |
||
| 73 | public function setRoleId($roleId) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get roleId |
||
| 82 | * |
||
| 83 | * @return integer |
||
| 84 | */ |
||
| 85 | public function getRoleId() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set scope |
||
| 92 | * |
||
| 93 | * @param string $scope |
||
| 94 | * @return CRoleGroup |
||
| 95 | */ |
||
| 96 | public function setScope($scope) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get scope |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getScope() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set id |
||
| 115 | * |
||
| 116 | * @param integer $id |
||
| 117 | * @return CRoleGroup |
||
| 118 | */ |
||
| 119 | public function setId($id) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get id |
||
| 128 | * |
||
| 129 | * @return integer |
||
| 130 | */ |
||
| 131 | public function getId() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set cId |
||
| 138 | * |
||
| 139 | * @param integer $cId |
||
| 140 | * @return CRoleGroup |
||
| 141 | */ |
||
| 142 | public function setCId($cId) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get cId |
||
| 151 | * |
||
| 152 | * @return integer |
||
| 153 | */ |
||
| 154 | public function getCId() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set groupId |
||
| 161 | * |
||
| 162 | * @param integer $groupId |
||
| 163 | * @return CRoleGroup |
||
| 164 | */ |
||
| 165 | public function setGroupId($groupId) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get groupId |
||
| 174 | * |
||
| 175 | * @return integer |
||
| 176 | */ |
||
| 177 | public function getGroupId() |
||
| 181 | } |
||
| 182 |
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.