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 namespace FreedomCore\TrinityCore\Console\Commands; |
||
11 | class Send extends BaseCommand |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Send Items To The Player |
||
16 | * @param string $playerName |
||
17 | * @param string $mailSubject |
||
18 | * @param string $mailText |
||
19 | * @param Items $items |
||
20 | * @return array|string |
||
21 | */ |
||
22 | View Code Duplication | public function items(string $playerName, string $mailSubject, string $mailText, Items $items) |
|
|
|||
23 | { |
||
24 | return $this->executeCommand(__FUNCTION__, [ |
||
25 | 'playerName' => $playerName, |
||
26 | 'mailSubject' => $this->inQuotes($mailSubject), |
||
27 | 'mailText' => $this->inQuotes($mailText), |
||
28 | 'items' => $items->toCommandAttribute() |
||
29 | ]); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Send mail to the player |
||
34 | * @param string $playerName |
||
35 | * @param string $mailSubject |
||
36 | * @param string $mailText |
||
37 | * @return array|string |
||
38 | */ |
||
39 | View Code Duplication | public function mail(string $playerName, string $mailSubject, string $mailText) |
|
47 | |||
48 | /** |
||
49 | * Send message to the player which will appear in the middle of the screen |
||
50 | * @param string $playerName |
||
51 | * @param string $message |
||
52 | * @return array|string |
||
53 | */ |
||
54 | public function message(string $playerName, string $message) |
||
58 | |||
59 | /** |
||
60 | * Send money to the player |
||
61 | * @param string $playerName |
||
62 | * @param string $mailSubject |
||
63 | * @param string $mailText |
||
64 | * @param int $amount |
||
65 | * @return array|string |
||
66 | */ |
||
67 | View Code Duplication | public function money(string $playerName, string $mailSubject, string $mailText, int $amount) |
|
76 | } |
||
77 |
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.