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 |
||
| 19 | View Code Duplication | class ApplicationEntity implements DataEntityInterface |
|
|
|
|||
| 20 | { |
||
| 21 | /** @var int */ |
||
| 22 | private $applicationId; |
||
| 23 | /** @var string */ |
||
| 24 | private $name; |
||
| 25 | /** @var string */ |
||
| 26 | private $title; |
||
| 27 | /** @var string */ |
||
| 28 | private $description; |
||
| 29 | /** @var bool */ |
||
| 30 | private $isReadOnly; |
||
| 31 | /** @var DateTime */ |
||
| 32 | private $dateCreated; |
||
| 33 | /** @var DateTime */ |
||
| 34 | private $dateModified; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Sets the value of the entity identifier. |
||
| 38 | * |
||
| 39 | * @param int $entityId |
||
| 40 | * @return ApplicationEntity |
||
| 41 | */ |
||
| 42 | 1 | public function setKeyData($entityId) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Gets the value of the entity identifier. |
||
| 51 | * |
||
| 52 | * @return int |
||
| 53 | */ |
||
| 54 | 2 | public function getKeyData() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @param int $applicationId |
||
| 61 | * |
||
| 62 | * @return ApplicationEntity |
||
| 63 | */ |
||
| 64 | 6 | public function setApplicationId($applicationId) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | 3 | public function getApplicationId() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $name |
||
| 81 | * |
||
| 82 | * @return ApplicationEntity |
||
| 83 | */ |
||
| 84 | 3 | public function setName($name) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 2 | public function getName() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $title |
||
| 101 | * |
||
| 102 | * @return ApplicationEntity |
||
| 103 | */ |
||
| 104 | 3 | public function setTitle($title) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 2 | public function getTitle() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $description |
||
| 121 | * |
||
| 122 | * @return ApplicationEntity |
||
| 123 | */ |
||
| 124 | 3 | public function setDescription($description) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 2 | public function getDescription() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param bool $state |
||
| 141 | * |
||
| 142 | * @return ApplicationEntity |
||
| 143 | */ |
||
| 144 | 3 | public function setReadOnly($state) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 2 | public function getReadOnly() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param DateTime $dateCreated |
||
| 161 | * |
||
| 162 | * @return ApplicationEntity |
||
| 163 | */ |
||
| 164 | 3 | public function setDateCreated(DateTime $dateCreated) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @return DateTime |
||
| 173 | */ |
||
| 174 | 2 | public function getDateCreated() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param DateTime $dateModified |
||
| 181 | * |
||
| 182 | * @return ApplicationEntity |
||
| 183 | */ |
||
| 184 | 3 | public function setDateModified(DateTime $dateModified) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return DateTime |
||
| 193 | */ |
||
| 194 | 2 | public function getDateModified() |
|
| 198 | } |
||
| 199 |
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.