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 |
||
30 | class MailAccountMapper extends Mapper { |
||
31 | |||
32 | /** |
||
33 | * @param IDb $db |
||
34 | */ |
||
35 | 2 | public function __construct(IDb $db) { |
|
38 | |||
39 | /** Finds an Mail Account by id |
||
40 | * |
||
41 | * @param int $userId |
||
42 | * @param int $accountId |
||
43 | * @return MailAccount |
||
44 | */ |
||
45 | 1 | public function find($userId, $accountId) { |
|
51 | |||
52 | /** |
||
53 | * Finds all Mail Accounts by user id existing for this user |
||
54 | * @param string $userId the id of the user that we want to find |
||
55 | * @param $userId |
||
56 | * @return MailAccount[] |
||
57 | */ |
||
58 | 1 | public function findByUserId($userId) { |
|
64 | |||
65 | View Code Duplication | public function findByEmail($email, $userId) { |
|
74 | |||
75 | /** |
||
76 | * Saves an User Account into the database |
||
77 | * @param MailAccount $account |
||
78 | * @return MailAccount |
||
79 | */ |
||
80 | 1 | public function save(MailAccount $account) { |
|
88 | |||
89 | } |
||
90 |
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.