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 |
||
21 | View Code Duplication | class CommandModel extends AbstractModel |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private static $endpoint = '/commands'; |
||
27 | |||
28 | /** |
||
29 | * @param array $requestOptions |
||
30 | * @return ResponseInterface |
||
31 | */ |
||
32 | public function createCommand(array $requestOptions) |
||
36 | |||
37 | /** |
||
38 | * @param array $requestOptions |
||
39 | * @return ResponseInterface |
||
40 | */ |
||
41 | public function listCommandsForTeam(array $requestOptions) |
||
45 | |||
46 | /** |
||
47 | * @param $teamId |
||
48 | * @return ResponseInterface |
||
49 | */ |
||
50 | public function listAutocompleteCommands($teamId) |
||
54 | |||
55 | /** |
||
56 | * @param $commandId |
||
57 | * @param array $requestOptions |
||
58 | * @return ResponseInterface |
||
59 | */ |
||
60 | public function updateCommand($commandId, array $requestOptions) |
||
64 | |||
65 | /** |
||
66 | * @param $commandId |
||
67 | * @return ResponseInterface |
||
68 | */ |
||
69 | public function deleteCommand($commandId) |
||
73 | |||
74 | /** |
||
75 | * @param $commandId |
||
76 | * @return ResponseInterface |
||
77 | */ |
||
78 | public function generateNewToken($commandId) |
||
82 | |||
83 | /** |
||
84 | * @param array $requestOptions |
||
85 | * @return ResponseInterface |
||
86 | */ |
||
87 | public function executeCommand(array $requestOptions) |
||
91 | |||
92 | /** |
||
93 | * @param $commandId |
||
94 | * @param array $requestOptions |
||
95 | * @return ResponseInterface |
||
96 | */ |
||
97 | public function moveCommand($commandId, array $requestOptions) |
||
101 | |||
102 | /** |
||
103 | * @param $commandId |
||
104 | * @return ResponseInterface |
||
105 | */ |
||
106 | public function getCommand($commandId) |
||
110 | } |
||
111 |
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.