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 |
||
13 | final class Mail extends Singleton |
||
14 | { |
||
15 | /** |
||
16 | * Stored Mail Model. |
||
17 | * |
||
18 | * @var MailModel |
||
19 | */ |
||
20 | protected $mailModel; |
||
21 | |||
22 | /** |
||
23 | * Send an Email. |
||
24 | * |
||
25 | * @param array $configuration |
||
26 | * @param string $view |
||
27 | */ |
||
28 | View Code Duplication | public function send(array $configuration, string $view = 'habbo-web-mail.confirm-mail') |
|
|
|||
29 | { |
||
30 | if (Config::get('mail.enable')) { |
||
31 | MailFacade::send($view, $configuration, function ($message) use ($configuration) { |
||
32 | $message->from(Config::get('mail.from.address'), Config::get('mail.from.name')); |
||
33 | $message->to($configuration['email'])->subject($configuration['subject']); |
||
34 | }); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Store an E-mail. |
||
40 | * |
||
41 | * @param string $email |
||
42 | * @param string $url |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function store(string $email, string $url): string |
||
52 | |||
53 | /** |
||
54 | * Return if Exists E-Mail with that Token. |
||
55 | * |
||
56 | * @param string $token |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function has(string $token) |
||
64 | |||
65 | /** |
||
66 | * Get an E-mail by Token. |
||
67 | * |
||
68 | * @param string $token |
||
69 | * |
||
70 | * @return MailModel |
||
71 | */ |
||
72 | public function get(string $token = '') |
||
88 | |||
89 | /** |
||
90 | * Set Mail Model in Cache. |
||
91 | * |
||
92 | * @param MailModel $model |
||
93 | * |
||
94 | * @return MailModel |
||
95 | */ |
||
96 | public function set(MailModel $model) |
||
100 | |||
101 | /** |
||
102 | * Update Mail Model Data. |
||
103 | * |
||
104 | * @param array $parameters |
||
105 | * |
||
106 | * @return MailModel |
||
107 | */ |
||
108 | public function update(array $parameters) |
||
114 | } |
||
115 |
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.