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 |
||
29 | class AccountService { |
||
30 | |||
31 | /** @var \OCA\Mail\Db\MailAccountMapper */ |
||
32 | private $mapper; |
||
33 | |||
34 | /** |
||
35 | * Cache accounts for multiple calls to 'findByUserId' |
||
36 | * |
||
37 | * @var IAccount[] |
||
38 | */ |
||
39 | private $accounts; |
||
40 | |||
41 | /** @var IL10N */ |
||
42 | private $l10n; |
||
43 | |||
44 | /** |
||
45 | * @param MailAccountMapper $mapper |
||
46 | */ |
||
47 | 7 | public function __construct(MailAccountMapper $mapper, IL10N $l10n) { |
|
51 | |||
52 | /** |
||
53 | * @param string $currentUserId |
||
54 | * @return IAccount[] |
||
55 | */ |
||
56 | 2 | public function findByUserId($currentUserId) { |
|
71 | |||
72 | /** |
||
73 | * @param $currentUserId |
||
74 | * @param $accountId |
||
75 | * @return IAccount |
||
76 | */ |
||
77 | 1 | public function find($currentUserId, $accountId) { |
|
92 | |||
93 | /** |
||
94 | * @param int $accountId |
||
95 | */ |
||
96 | 2 | public function delete($currentUserId, $accountId) { |
|
103 | |||
104 | /** |
||
105 | * @param $newAccount |
||
106 | * @return \OCA\Mail\Db\MailAccount |
||
107 | */ |
||
108 | 1 | public function save($newAccount) { |
|
111 | |||
112 | 1 | private function buildUnifiedAccount($userId) { |
|
115 | |||
116 | /** |
||
117 | * @param int $accountId |
||
118 | * @param String $currentUserId |
||
119 | * @param String $signature |
||
120 | * @return \OCA\Mail\Db\Alias |
||
121 | */ |
||
122 | View Code Duplication | public function updateSignature($accountId, $currentUserId, $signature) { |
|
131 | } |
||
132 |
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.