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 |
||
16 | class PublishAssignmentUsers extends Command |
||
17 | { |
||
18 | use InteractsWithLocalGithub, InteractsWithEnvironment; |
||
19 | |||
20 | use AborstIfEnvVariableIsnotInstalled; |
||
21 | |||
22 | /** |
||
23 | * The name and signature of the console command. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $signature = 'publish:assignment_users {--user=* : The user/s assigned to this assignment}'; |
||
28 | |||
29 | /** |
||
30 | * The console command description. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description = 'Assing users to current assignment'; |
||
35 | |||
36 | /** |
||
37 | * Server names. |
||
38 | * |
||
39 | * @var Client |
||
40 | */ |
||
41 | protected $http; |
||
42 | |||
43 | /** |
||
44 | * Users. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $users; |
||
49 | |||
50 | /** |
||
51 | * SaveEnvVariable constructor. |
||
52 | * |
||
53 | */ |
||
54 | public function __construct(Client $http) |
||
59 | |||
60 | /** |
||
61 | * Execute the console command. |
||
62 | * |
||
63 | */ |
||
64 | public function handle() |
||
77 | |||
78 | /** |
||
79 | * Assign users to assignment |
||
80 | * |
||
81 | * @return array|mixed |
||
82 | */ |
||
83 | View Code Duplication | protected function assignUsersToAssignment() |
|
108 | |||
109 | /** |
||
110 | * Get users |
||
111 | */ |
||
112 | protected function users() |
||
129 | |||
130 | /** |
||
131 | * Ask for users. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function askForUsers() |
||
153 | |||
154 | /** |
||
155 | * Abort command execution. |
||
156 | */ |
||
157 | protected function abortCommandExecution() |
||
161 | } |
||
162 |
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.