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 PublishDeploymentScript extends Command |
||
17 | { |
||
18 | use ChecksEnv, DiesIfEnvVariableIsnotInstalled; |
||
19 | |||
20 | /** |
||
21 | * The name and signature of the console command. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $signature = 'publish:deployment_script {file?} {--server=} {--site=}'; |
||
26 | |||
27 | /** |
||
28 | * The console command description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Deployment script on Laravel Forge site'; |
||
33 | |||
34 | /** |
||
35 | * Server forge id. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $server; |
||
40 | |||
41 | /** |
||
42 | * Laravel forge site id. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $site; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * File with new script content. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $file; |
||
55 | |||
56 | /** |
||
57 | * Guzzle http client. |
||
58 | * |
||
59 | * @var Client |
||
60 | */ |
||
61 | protected $http; |
||
62 | |||
63 | /** |
||
64 | * Create a new command instance. |
||
65 | * |
||
66 | */ |
||
67 | public function __construct(Client $http) |
||
72 | |||
73 | /** |
||
74 | * Execute the console command. |
||
75 | * |
||
76 | */ |
||
77 | public function handle() |
||
87 | |||
88 | protected function showDeploymentScript() |
||
105 | |||
106 | /** |
||
107 | * Update deployment script. |
||
108 | */ |
||
109 | View Code Duplication | protected function updateDeploymentScript() |
|
131 | |||
132 | /** |
||
133 | * Obtain API URL endpoint. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | protected function obtainAPIURLEndpoint($type) |
||
143 | |||
144 | /** |
||
145 | * Abort command execution. |
||
146 | */ |
||
147 | protected function abortCommandExecution() |
||
153 | } |
||
154 |
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.