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:
Complex classes like AuthManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class AuthManager extends \Risoluto\RisolutoCliBase implements \Risoluto\RisolutoCliInterface |
||
22 | { |
||
23 | /** |
||
24 | * run() |
||
25 | * |
||
26 | * 主処理を行う |
||
27 | * |
||
28 | * @access public |
||
29 | * |
||
30 | * @param array $options オプション情報 |
||
31 | * |
||
32 | * @return void なし |
||
33 | */ |
||
34 | public function run( array $options ) |
||
35 | { |
||
36 | // 引数がなければ使用方法を表示する |
||
37 | if (empty( $options ) or ( count( $options ) > 1 )) { |
||
38 | $this->usage(); |
||
39 | exit; |
||
40 | } |
||
41 | |||
42 | // 引数を分解し適切な処理を行う |
||
43 | $operations = $this->separateOptions( $options[ 0 ] ); |
||
44 | switch (strtolower( $operations[ 'command' ] )) { |
||
45 | case 'init': |
||
46 | $this->init(); |
||
47 | break; |
||
48 | |||
49 | case 'adduser': |
||
50 | $this->addUser(); |
||
51 | break; |
||
52 | |||
53 | case 'addgroup': |
||
54 | $this->addGroup(); |
||
55 | break; |
||
56 | |||
57 | case 'moduser': |
||
58 | $this->modUser(); |
||
59 | break; |
||
60 | |||
61 | case 'modgroup': |
||
62 | $this->modGroup(); |
||
63 | break; |
||
64 | |||
65 | case 'deluser': |
||
66 | $this->delUser(); |
||
67 | break; |
||
68 | |||
69 | case 'delgroup': |
||
70 | $this->delGroup(); |
||
71 | break; |
||
72 | |||
73 | case 'showuser': |
||
74 | $this->showUser(); |
||
75 | break; |
||
76 | |||
77 | case 'showgroup': |
||
78 | $this->showGroup(); |
||
79 | break; |
||
80 | |||
81 | case 'showuserall': |
||
82 | $this->showUserAll(); |
||
83 | break; |
||
84 | |||
85 | case 'showgroupall': |
||
86 | $this->showGroupAll(); |
||
87 | break; |
||
88 | |||
89 | // 未定義なら使用方法を表示 |
||
90 | default : |
||
91 | $this->usage(); |
||
92 | break; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * init() |
||
98 | * |
||
99 | * 認証情報初期化処理を行う |
||
100 | * |
||
101 | * @access private |
||
102 | * |
||
103 | * @param void |
||
104 | * |
||
105 | * @return void なし |
||
106 | */ |
||
107 | private function init() |
||
108 | { |
||
109 | // 警告メッセージを表示し、承諾した場合のみ処理を実行する |
||
110 | $enter = $this->readFromStdin( "Delete all user/group data. Continue?[y/N]" ); |
||
111 | if (strtolower( $enter ) == 'y') { |
||
112 | if (\Risoluto\Auth::callProviderMethod( 'init', [ ] )) { |
||
113 | echo "All OK!" . PHP_EOL; |
||
114 | } else { |
||
115 | echo "Oops! Error happened." . PHP_EOL; |
||
116 | } |
||
117 | } else { |
||
118 | echo "Canceled." . PHP_EOL; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * addUser() |
||
124 | * |
||
125 | * ユーザー追加処理を行う |
||
126 | * |
||
127 | * @access private |
||
128 | * |
||
129 | * @param void |
||
130 | * |
||
131 | * @return void なし |
||
132 | */ |
||
133 | View Code Duplication | private function addUser() |
|
|
|||
134 | { |
||
135 | // 登録に必要な情報を順番に取得していく |
||
136 | $option[ 'by_who' ] = 'Risoluto CLI - ' . RISOLUTOCLI_SELF; |
||
137 | $option[ 'status' ] = 1; |
||
138 | |||
139 | do { |
||
140 | $option[ 'userid' ] = $this->readFromStdin( "Enter user id: " ); |
||
141 | } while (empty( $option[ 'userid' ] )); |
||
142 | do { |
||
143 | $option[ 'username' ] = $this->readFromStdin( "Enter user name: " ); |
||
144 | } while (empty( $option[ 'username' ] )); |
||
145 | do { |
||
146 | $option[ 'password' ] = $this->readFromStdin( "Enter user password: ", false ); |
||
147 | $option[ 'password_again' ] = $this->readFromStdin( "Enter user password again: ", false ); |
||
148 | } while (empty( $option[ 'password' ] ) or $option[ 'password' ] != $option[ 'password_again' ]); |
||
149 | do { |
||
150 | $option[ 'groupno' ] = $this->readFromStdin( "Enter group no: " ); |
||
151 | } while (empty( $option[ 'groupno' ] ) or !is_numeric( $option[ 'groupno' ] )); |
||
152 | |||
153 | // 確認メッセージを表示し、承諾した場合のみ処理を実行する |
||
154 | $enter = $this->readFromStdin( "Add this user data. Continue?[y/N]" ); |
||
155 | if (strtolower( $enter ) == 'y') { |
||
156 | if (\Risoluto\Auth::callProviderMethod( 'addUser', $option )) { |
||
157 | echo "All OK!" . PHP_EOL; |
||
158 | } else { |
||
159 | echo "Oops! Error happened." . PHP_EOL; |
||
160 | } |
||
161 | } else { |
||
162 | echo "Canceled." . PHP_EOL; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * addGroup() |
||
168 | * |
||
169 | * グループ追加処理を行う |
||
170 | * |
||
171 | * @access private |
||
172 | * |
||
173 | * @param void |
||
174 | * |
||
175 | * @return void なし |
||
176 | */ |
||
177 | View Code Duplication | private function addGroup() |
|
178 | { |
||
179 | // 登録に必要な情報を順番に取得していく |
||
180 | $option[ 'by_who' ] = 'Risoluto CLI - ' . RISOLUTOCLI_SELF; |
||
181 | $option[ 'status' ] = 1; |
||
182 | |||
183 | do { |
||
184 | $option[ 'groupid' ] = $this->readFromStdin( "Enter group id: " ); |
||
185 | } while (empty( $option[ 'groupid' ] )); |
||
186 | do { |
||
187 | $option[ 'groupname' ] = $this->readFromStdin( "Enter group name: " ); |
||
188 | } while (empty( $option[ 'groupname' ] )); |
||
189 | |||
190 | // 確認メッセージを表示し、承諾した場合のみ処理を実行する |
||
191 | $enter = $this->readFromStdin( "Add this group data. Continue?[y/N]" ); |
||
192 | if (strtolower( $enter ) == 'y') { |
||
193 | if (\Risoluto\Auth::callProviderMethod( 'addGroup', $option )) { |
||
194 | echo "All OK!" . PHP_EOL; |
||
195 | } else { |
||
196 | echo "Oops! Error happened." . PHP_EOL; |
||
197 | } |
||
198 | } else { |
||
199 | echo "Canceled." . PHP_EOL; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * modUser() |
||
205 | * |
||
206 | * ユーザー情報変更処理を行う |
||
207 | * |
||
208 | * @access private |
||
209 | * |
||
210 | * @param void |
||
211 | * |
||
212 | * @return void なし |
||
213 | */ |
||
214 | View Code Duplication | private function modUser() |
|
215 | { |
||
216 | // 変更処理に必要な情報を順番に取得していく |
||
217 | $option[ 'by_who' ] = 'Risoluto CLI - ' . RISOLUTOCLI_SELF; |
||
218 | |||
219 | do { |
||
220 | $option[ 'userid' ] = $this->readFromStdin( "Enter user id: " ); |
||
221 | } while (empty( $option[ 'userid' ] )); |
||
222 | do { |
||
223 | $option[ 'username' ] = $this->readFromStdin( "Enter user name: " ); |
||
224 | } while (empty( $option[ 'username' ] )); |
||
225 | do { |
||
226 | $option[ 'password' ] = $this->readFromStdin( "Enter user password: ", false ); |
||
227 | $option[ 'password_again' ] = $this->readFromStdin( "Enter user password again: ", false ); |
||
228 | } while (empty( $option[ 'password' ] ) or $option[ 'password' ] != $option[ 'password_again' ]); |
||
229 | do { |
||
230 | $option[ 'groupno' ] = $this->readFromStdin( "Enter group no: " ); |
||
231 | } while (empty( $option[ 'groupno' ] ) or !is_numeric( $option[ 'groupno' ] )); |
||
232 | do { |
||
233 | $option[ 'status' ] = $this->readFromStdin( "Enter status: " ); |
||
234 | } while (!is_numeric( $option[ 'status' ] )); |
||
235 | |||
236 | // 確認メッセージを表示し、承諾した場合のみ処理を実行する |
||
237 | $enter = $this->readFromStdin( "Modify this user data. Continue?[y/N]" ); |
||
238 | if (strtolower( $enter ) == 'y') { |
||
239 | if (\Risoluto\Auth::callProviderMethod( 'modUser', $option )) { |
||
240 | echo "All OK!" . PHP_EOL; |
||
241 | } else { |
||
242 | echo "Oops! Error happened." . PHP_EOL; |
||
243 | } |
||
244 | } else { |
||
245 | echo "Canceled." . PHP_EOL; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * modGroup() |
||
251 | * |
||
252 | * グループ情報変更処理を行う |
||
253 | * |
||
254 | * @access private |
||
255 | * |
||
256 | * @param void |
||
257 | * |
||
258 | * @return void なし |
||
259 | */ |
||
260 | View Code Duplication | private function modGroup() |
|
261 | { |
||
262 | // 変更処理に必要な情報を順番に取得していく |
||
263 | $option[ 'by_who' ] = 'Risoluto CLI - ' . RISOLUTOCLI_SELF; |
||
264 | |||
265 | do { |
||
266 | $option[ 'groupid' ] = $this->readFromStdin( "Enter group id: " ); |
||
267 | } while (empty( $option[ 'groupid' ] )); |
||
268 | do { |
||
269 | $option[ 'groupname' ] = $this->readFromStdin( "Enter group name: " ); |
||
270 | } while (empty( $option[ 'groupname' ] )); |
||
271 | do { |
||
272 | $option[ 'status' ] = $this->readFromStdin( "Enter status: " ); |
||
273 | } while (!is_numeric( $option[ 'status' ] )); |
||
274 | |||
275 | // 確認メッセージを表示し、承諾した場合のみ処理を実行する |
||
276 | $enter = $this->readFromStdin( "Modify this group data. Continue?[y/N]" ); |
||
277 | if (strtolower( $enter ) == 'y') { |
||
278 | if (\Risoluto\Auth::callProviderMethod( 'modGroup', $option )) { |
||
279 | echo "All OK!" . PHP_EOL; |
||
280 | } else { |
||
281 | echo "Oops! Error happened." . PHP_EOL; |
||
282 | } |
||
283 | } else { |
||
284 | echo "Canceled." . PHP_EOL; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * delUser() |
||
290 | * |
||
291 | * ユーザー情報削除処理を行う |
||
292 | * |
||
293 | * @access private |
||
294 | * |
||
295 | * @param void |
||
296 | * |
||
297 | * @return void なし |
||
298 | */ |
||
299 | View Code Duplication | private function delUser() |
|
318 | |||
319 | /** |
||
320 | * delGroup() |
||
321 | * |
||
322 | * グループ情報削除処理を行う |
||
323 | * |
||
324 | * @access private |
||
325 | * |
||
326 | * @param void |
||
327 | * |
||
328 | * @return void なし |
||
329 | */ |
||
330 | View Code Duplication | private function delGroup() |
|
349 | |||
350 | /** |
||
351 | * showUser() |
||
352 | * |
||
353 | * ユーザー情報を表示する |
||
354 | * |
||
355 | * @access private |
||
356 | * |
||
357 | * @param void |
||
358 | * |
||
359 | * @return void なし |
||
360 | */ |
||
361 | View Code Duplication | private function showUser() |
|
371 | |||
372 | /** |
||
373 | * showGroup() |
||
374 | * |
||
375 | * グループ情報を表示する |
||
376 | * |
||
377 | * @access private |
||
378 | * |
||
379 | * @param void |
||
380 | * |
||
381 | * @return void なし |
||
382 | */ |
||
383 | View Code Duplication | private function showGroup() |
|
393 | |||
394 | |||
395 | /** |
||
396 | * showUserAll() |
||
397 | * |
||
398 | * ユーザー情報をすべて表示する |
||
399 | * |
||
400 | * @access private |
||
401 | * |
||
402 | * @param void |
||
403 | * |
||
404 | * @return void なし |
||
405 | */ |
||
406 | private function showUserAll() |
||
411 | |||
412 | /** |
||
413 | * showGroupAll() |
||
414 | * |
||
415 | * グループ情報をすべて表示する |
||
416 | * |
||
417 | * @access private |
||
418 | * |
||
419 | * @param void |
||
420 | * |
||
421 | * @return void なし |
||
422 | */ |
||
423 | private function showGroupAll() |
||
428 | |||
429 | /** |
||
430 | * usage() |
||
431 | * |
||
432 | * 使用方法を表示する |
||
433 | * |
||
434 | * @access private |
||
435 | * |
||
436 | * @param void |
||
437 | * |
||
438 | * @return void なし |
||
439 | */ |
||
440 | private function usage() |
||
461 | } |
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.