Complex classes like ModuleOptions 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 ModuleOptions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class ModuleOptions extends AbstractOptions implements |
||
8 | UserControllerOptionsInterface, |
||
9 | UserServiceOptionsInterface |
||
10 | { |
||
11 | /** |
||
12 | * Turn off strict options mode |
||
13 | */ |
||
14 | protected $__strictMode__ = false; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $useRedirectParameterIfPresent = true; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $loginRedirectRoute = 'lmcuser'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $logoutRedirectRoute = 'lmcuser/login'; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $loginFormTimeout = 300; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $userFormTimeout = 300; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $loginAfterRegistration = true; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $enableUserState = false; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $defaultUserState = 1; |
||
55 | |||
56 | /** |
||
57 | * @var Array |
||
58 | */ |
||
59 | protected $allowedLoginStates = array( null, 1 ); |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $authAdapters = array( 100 => 'LmcUser\Authentication\Adapter\Db' ); |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $authIdentityFields = array( 'email' ); |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $userEntityClass = 'LmcUser\Entity\User'; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $userLoginWidgetViewTemplate = 'lmc-user/user/login.phtml'; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $enableRegistration = true; |
||
85 | |||
86 | /** |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected $enableUsername = false; |
||
90 | |||
91 | /** |
||
92 | * @var bool |
||
93 | */ |
||
94 | protected $enableDisplayName = false; |
||
95 | |||
96 | /** |
||
97 | * @var bool |
||
98 | */ |
||
99 | protected $useRegistrationFormCaptcha = false; |
||
100 | |||
101 | /** |
||
102 | * @var bool |
||
103 | */ |
||
104 | protected $useLoginFormCaptcha = false; |
||
105 | |||
106 | /** |
||
107 | * @var bool |
||
108 | */ |
||
109 | protected $useLoginFormCsrf = true; |
||
110 | |||
111 | /** |
||
112 | * @var int |
||
113 | */ |
||
114 | protected $passwordCost = 14; |
||
115 | |||
116 | /** |
||
117 | * @var string |
||
118 | */ |
||
119 | |||
120 | protected $tableName = 'user'; |
||
121 | |||
122 | /** |
||
123 | * @var array |
||
124 | */ |
||
125 | protected $formCaptchaOptions = array( |
||
126 | 'class' => 'figlet', |
||
127 | 'options' => array( |
||
128 | 'wordLen' => 5, |
||
129 | 'expiration' => 300, |
||
130 | 'timeout' => 300, |
||
131 | ), |
||
132 | ); |
||
133 | |||
134 | /** |
||
135 | * set login redirect route |
||
136 | * |
||
137 | * @param string $loginRedirectRoute |
||
138 | * @return ModuleOptions |
||
139 | */ |
||
140 | public function setLoginRedirectRoute($loginRedirectRoute) |
||
145 | |||
146 | /** |
||
147 | * get login redirect route |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getLoginRedirectRoute() |
||
155 | |||
156 | /** |
||
157 | * set logout redirect route |
||
158 | * |
||
159 | * @param string $logoutRedirectRoute |
||
160 | * @return ModuleOptions |
||
161 | */ |
||
162 | public function setLogoutRedirectRoute($logoutRedirectRoute) |
||
167 | |||
168 | /** |
||
169 | * get logout redirect route |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getLogoutRedirectRoute() |
||
177 | |||
178 | /** |
||
179 | * set use redirect param if present |
||
180 | * |
||
181 | * @param bool $useRedirectParameterIfPresent |
||
182 | * @return ModuleOptions |
||
183 | */ |
||
184 | public function setUseRedirectParameterIfPresent($useRedirectParameterIfPresent) |
||
189 | |||
190 | /** |
||
191 | * get use redirect param if present |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function getUseRedirectParameterIfPresent() |
||
199 | |||
200 | /** |
||
201 | * set the view template for the user login widget |
||
202 | * |
||
203 | * @param string $userLoginWidgetViewTemplate |
||
204 | * @return ModuleOptions |
||
205 | */ |
||
206 | public function setUserLoginWidgetViewTemplate($userLoginWidgetViewTemplate) |
||
211 | |||
212 | /** |
||
213 | * get the view template for the user login widget |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getUserLoginWidgetViewTemplate() |
||
221 | |||
222 | /** |
||
223 | * set enable user registration |
||
224 | * |
||
225 | * @param bool $enableRegistration |
||
226 | * @return ModuleOptions |
||
227 | */ |
||
228 | public function setEnableRegistration($enableRegistration) |
||
233 | |||
234 | /** |
||
235 | * get enable user registration |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function getEnableRegistration() |
||
243 | |||
244 | /** |
||
245 | * set login form timeout |
||
246 | * |
||
247 | * @param int $loginFormTimeout |
||
248 | * @return ModuleOptions |
||
249 | */ |
||
250 | public function setLoginFormTimeout($loginFormTimeout) |
||
255 | |||
256 | /** |
||
257 | * get login form timeout in seconds |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | public function getLoginFormTimeout() |
||
265 | |||
266 | /** |
||
267 | * set user form timeout in seconds |
||
268 | * |
||
269 | * @param int $userFormTimeout |
||
270 | * @return ModuleOptions |
||
271 | */ |
||
272 | public function setUserFormTimeout($userFormTimeout) |
||
277 | |||
278 | /** |
||
279 | * get user form timeout in seconds |
||
280 | * |
||
281 | * @return int |
||
282 | */ |
||
283 | public function getUserFormTimeout() |
||
287 | |||
288 | /** |
||
289 | * set login after registration |
||
290 | * |
||
291 | * @param bool $loginAfterRegistration |
||
292 | * @return ModuleOptions |
||
293 | */ |
||
294 | public function setLoginAfterRegistration($loginAfterRegistration) |
||
299 | |||
300 | /** |
||
301 | * get login after registration |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function getLoginAfterRegistration() |
||
309 | |||
310 | /** |
||
311 | * get user state usage for registration/login process |
||
312 | * |
||
313 | * @return int |
||
314 | */ |
||
315 | public function getEnableUserState() |
||
319 | |||
320 | /** |
||
321 | * set user state usage for registration/login process |
||
322 | * |
||
323 | * @param boolean $flag |
||
324 | * @return ModuleOptions |
||
325 | */ |
||
326 | public function setEnableUserState($flag) |
||
331 | |||
332 | /** |
||
333 | * get default user state on registration |
||
334 | * |
||
335 | * @return int |
||
336 | */ |
||
337 | public function getDefaultUserState() |
||
341 | |||
342 | /** |
||
343 | * set default user state on registration |
||
344 | * |
||
345 | * @param int $state |
||
346 | * @return ModuleOptions |
||
347 | */ |
||
348 | public function setDefaultUserState($state) |
||
353 | |||
354 | /** |
||
355 | * get list of states to allow user login |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | public function getAllowedLoginStates() |
||
363 | |||
364 | /** |
||
365 | * set list of states to allow user login |
||
366 | * |
||
367 | * @param Array $states |
||
368 | * @return ModuleOptions |
||
369 | */ |
||
370 | public function setAllowedLoginStates(array $states) |
||
375 | |||
376 | /** |
||
377 | * set auth adapters |
||
378 | * |
||
379 | * @param array $authAdapterss |
||
380 | * @return ModuleOptions |
||
381 | */ |
||
382 | public function setAuthAdapters($authAdapters) |
||
387 | |||
388 | /** |
||
389 | * get auth adapters |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | public function getAuthAdapters() |
||
397 | |||
398 | /** |
||
399 | * set auth identity fields |
||
400 | * |
||
401 | * @param array $authIdentityFields |
||
402 | * @return ModuleOptions |
||
403 | */ |
||
404 | public function setAuthIdentityFields($authIdentityFields) |
||
409 | |||
410 | /** |
||
411 | * get auth identity fields |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | public function getAuthIdentityFields() |
||
419 | |||
420 | /** |
||
421 | * set enable username |
||
422 | * |
||
423 | * @param bool $flag |
||
424 | * @return ModuleOptions |
||
425 | */ |
||
426 | public function setEnableUsername($flag) |
||
431 | |||
432 | /** |
||
433 | * get enable username |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function getEnableUsername() |
||
441 | |||
442 | /** |
||
443 | * set enable display name |
||
444 | * |
||
445 | * @param bool $flag |
||
446 | * @return ModuleOptions |
||
447 | */ |
||
448 | public function setEnableDisplayName($flag) |
||
453 | |||
454 | /** |
||
455 | * get enable display name |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | public function getEnableDisplayName() |
||
463 | |||
464 | /** |
||
465 | * set use a captcha in registration form |
||
466 | * |
||
467 | * @param bool $useRegistrationFormCaptcha |
||
468 | * @return ModuleOptions |
||
469 | */ |
||
470 | public function setUseRegistrationFormCaptcha($useRegistrationFormCaptcha) |
||
475 | |||
476 | /** |
||
477 | * get use a captcha in registration form |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function getUseRegistrationFormCaptcha() |
||
485 | |||
486 | /** |
||
487 | * set use a captcha in login form |
||
488 | * |
||
489 | * @param bool $useLoginFormCaptcha |
||
490 | * @return ModuleOptions |
||
491 | */ |
||
492 | public function setUseLoginFormCaptcha($useLoginFormCaptcha) |
||
497 | |||
498 | /** |
||
499 | * get use a captcha in login form |
||
500 | * |
||
501 | * @return bool |
||
502 | */ |
||
503 | public function getUseLoginFormCaptcha() |
||
507 | |||
508 | /** |
||
509 | * set use a csrf in login form |
||
510 | * |
||
511 | * @param bool $useLoginFormCsrf |
||
512 | * @return ModuleOptions |
||
513 | */ |
||
514 | public function setUseLoginFormCsrf($useLoginFormCsrf) |
||
519 | |||
520 | /** |
||
521 | * get use a csrf in login form |
||
522 | * |
||
523 | * @return bool |
||
524 | */ |
||
525 | public function getUseLoginFormCsrf() |
||
529 | |||
530 | /** |
||
531 | * set user entity class name |
||
532 | * |
||
533 | * @param string $userEntityClass |
||
534 | * @return ModuleOptions |
||
535 | */ |
||
536 | public function setUserEntityClass($userEntityClass) |
||
541 | |||
542 | /** |
||
543 | * get user entity class name |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | public function getUserEntityClass() |
||
551 | |||
552 | /** |
||
553 | * set password cost |
||
554 | * |
||
555 | * @param int $passwordCost |
||
556 | * @return ModuleOptions |
||
557 | */ |
||
558 | public function setPasswordCost($passwordCost) |
||
563 | |||
564 | /** |
||
565 | * get password cost |
||
566 | * |
||
567 | * @return int |
||
568 | */ |
||
569 | public function getPasswordCost() |
||
573 | |||
574 | /** |
||
575 | * set user table name |
||
576 | * |
||
577 | * @param string $tableName |
||
578 | */ |
||
579 | public function setTableName($tableName) |
||
583 | |||
584 | /** |
||
585 | * get user table name |
||
586 | * |
||
587 | * @return string |
||
588 | */ |
||
589 | public function getTableName() |
||
593 | |||
594 | /** |
||
595 | * set form CAPTCHA options |
||
596 | * |
||
597 | * @param array $formCaptchaOptions |
||
598 | * @return ModuleOptions |
||
599 | */ |
||
600 | public function setFormCaptchaOptions($formCaptchaOptions) |
||
605 | |||
606 | /** |
||
607 | * get form CAPTCHA options |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | public function getFormCaptchaOptions() |
||
615 | } |
||
616 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.