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 |
||
| 22 | class UserStorage extends AbstractDataStorage |
||
| 23 | { |
||
| 24 | /** @var string */ |
||
| 25 | protected $dataGroup = 'webhemi_user'; |
||
| 26 | /** @var string */ |
||
| 27 | protected $idKey = 'id_user'; |
||
| 28 | /** @var string */ |
||
| 29 | private $userName = 'username'; |
||
| 30 | /** @var string */ |
||
| 31 | private $email = 'email'; |
||
| 32 | /** @var string */ |
||
| 33 | private $password = 'password'; |
||
| 34 | /** @var string */ |
||
| 35 | private $hash = 'hash'; |
||
| 36 | /** @var int */ |
||
| 37 | private $isActive = 'is_active'; |
||
| 38 | /** @var int */ |
||
| 39 | private $isEnabled = 'is_enabled'; |
||
| 40 | /** @var string */ |
||
| 41 | private $dateCreated = 'date_created'; |
||
| 42 | /** @var string */ |
||
| 43 | private $dateModified = 'date_modified'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Populates an entity with storage data. |
||
| 47 | * |
||
| 48 | * @param DataEntityInterface $entity |
||
| 49 | * @param array $data |
||
| 50 | */ |
||
| 51 | 2 | protected function populateEntity(DataEntityInterface &$entity, array $data) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Returns a User entity identified by (unique) ID. |
||
| 67 | * |
||
| 68 | * @param int $identifier |
||
| 69 | * |
||
| 70 | * @return bool|UserEntity |
||
| 71 | */ |
||
| 72 | 1 | View Code Duplication | public function getUserById($identifier) |
| 84 | |||
| 85 | /** |
||
| 86 | * Returns a User entity identified by (unique) Email. |
||
| 87 | * |
||
| 88 | * @param $email |
||
| 89 | * |
||
| 90 | * @return bool|UserEntity |
||
| 91 | */ |
||
| 92 | 1 | View Code Duplication | public function getUserByEmail($email) |
| 104 | } |
||
| 105 |
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.