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 |
||
| 39 | class AccountManager { |
||
| 40 | |||
| 41 | /** nobody can see my account details */ |
||
| 42 | const VISIBILITY_PRIVATE = 'private'; |
||
| 43 | /** only contacts, especially trusted servers can see my contact details */ |
||
| 44 | const VISIBILITY_CONTACTS_ONLY = 'contacts'; |
||
| 45 | /** every body ca see my contact detail, will be published to the lookup server */ |
||
| 46 | const VISIBILITY_PUBLIC = 'public'; |
||
| 47 | |||
| 48 | const PROPERTY_AVATAR = 'avatar'; |
||
| 49 | const PROPERTY_DISPLAYNAME = 'displayname'; |
||
| 50 | const PROPERTY_PHONE = 'phone'; |
||
| 51 | const PROPERTY_EMAIL = 'email'; |
||
| 52 | const PROPERTY_WEBSITE = 'website'; |
||
| 53 | const PROPERTY_ADDRESS = 'address'; |
||
| 54 | const PROPERTY_TWITTER = 'twitter'; |
||
| 55 | |||
| 56 | /** @var IDBConnection database connection */ |
||
| 57 | private $connection; |
||
| 58 | |||
| 59 | /** @var string table name */ |
||
| 60 | private $table = 'accounts'; |
||
| 61 | |||
| 62 | /** @var EventDispatcherInterface */ |
||
| 63 | private $eventDispatcher; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * AccountManager constructor. |
||
| 67 | * |
||
| 68 | * @param IDBConnection $connection |
||
| 69 | * @param EventDispatcherInterface $eventDispatcher |
||
| 70 | */ |
||
| 71 | public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * update user record |
||
| 78 | * |
||
| 79 | * @param IUser $user |
||
| 80 | * @param $data |
||
| 81 | */ |
||
| 82 | public function updateUser(IUser $user, $data) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * get stored data from a given user |
||
| 98 | * |
||
| 99 | * @param IUser $user |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function getUser(IUser $user) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * add new user to accounts table |
||
| 122 | * |
||
| 123 | * @param IUser $user |
||
| 124 | * @param array $data |
||
| 125 | */ |
||
| 126 | protected function insertNewUser(IUser $user, $data) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * update existing user in accounts table |
||
| 142 | * |
||
| 143 | * @param IUser $user |
||
| 144 | * @param array $data |
||
| 145 | */ |
||
| 146 | View Code Duplication | protected function updateExistingUser(IUser $user, $data) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * build default user record in case not data set exists yet |
||
| 158 | * |
||
| 159 | * @param IUser $user |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | protected function buildDefaultUserRecord(IUser $user) { |
||
| 200 | |||
| 201 | } |
||
| 202 |
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.