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 PublishMySQL extends Command |
||
17 | { |
||
18 | use ChecksEnv, DiesIfEnvVariableIsnotInstalled, WaitsForMySQLDatabase; |
||
19 | |||
20 | /** |
||
21 | * The name and signature of the console command. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $signature = 'publish:mysql {name?} {user?} {password?} {--server=} {--dump} {--wait}'; |
||
26 | |||
27 | /** |
||
28 | * The console command description. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Manage MySQL databases'; |
||
33 | |||
34 | /** |
||
35 | * Server forge id. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $server; |
||
40 | |||
41 | /** |
||
42 | * API endpoint URL. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $url; |
||
47 | |||
48 | /** |
||
49 | * Guzzle http client. |
||
50 | * |
||
51 | * @var Client |
||
52 | */ |
||
53 | protected $http; |
||
54 | |||
55 | /** |
||
56 | * Create a new command instance. |
||
57 | * |
||
58 | */ |
||
59 | public function __construct(Client $http) |
||
64 | |||
65 | /** |
||
66 | * Execute the console command. |
||
67 | * |
||
68 | */ |
||
69 | public function handle() |
||
79 | |||
80 | /** |
||
81 | * Create MySQL database. |
||
82 | */ |
||
83 | protected function createMySQLDatabase() |
||
109 | |||
110 | /** |
||
111 | * List MySQL databases. |
||
112 | */ |
||
113 | protected function listMySQLDatabases() |
||
141 | |||
142 | /** |
||
143 | * Check parameters. |
||
144 | */ |
||
145 | protected function checkParameters() |
||
154 | |||
155 | /** |
||
156 | * Get data. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | View Code Duplication | protected function getData() |
|
174 | |||
175 | /** |
||
176 | * Obtain API URL endpoint. |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | protected function obtainAPIURLEndpoint() |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Abort command execution. |
||
189 | */ |
||
190 | protected function abortCommandExecution() |
||
195 | } |
||
196 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.