Complex classes like Install 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 Install, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Install extends ControllerAbstract |
||
26 | { |
||
27 | /** |
||
28 | * process the class |
||
29 | * |
||
30 | * @since 3.0.0 |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | |||
35 | 8 | public function process() : string |
|
36 | { |
||
37 | 8 | $postArray = $this->_normalizePost($this->_sanitizePost()); |
|
38 | |||
39 | /* validate database */ |
||
40 | |||
41 | 8 | $validateArray = $this->_validateDatabase($postArray); |
|
42 | 8 | if ($validateArray) |
|
|
|||
43 | { |
||
44 | 1 | return $this->_error( |
|
45 | [ |
||
46 | 1 | 'url' => 'install.php', |
|
47 | 1 | 'title' => $this->_language->get('database'), |
|
48 | 1 | 'message' => $validateArray |
|
49 | ]); |
||
50 | } |
||
51 | |||
52 | /* validate account */ |
||
53 | |||
54 | 7 | $validateArray = $this->_validateAccount($postArray); |
|
55 | 7 | if ($validateArray) |
|
56 | { |
||
57 | 1 | return $this->_error( |
|
58 | [ |
||
59 | 1 | 'url' => 'install.php', |
|
60 | 1 | 'title' => $this->_language->get('account'), |
|
61 | 1 | 'message' => $validateArray |
|
62 | ]); |
||
63 | } |
||
64 | |||
65 | /* touch config */ |
||
66 | |||
67 | $configArray = |
||
68 | [ |
||
69 | 6 | 'dbType' => $postArray['dbType'], |
|
70 | 6 | 'dbHost' => $postArray['dbHost'], |
|
71 | 6 | 'dbName' => $postArray['dbName'], |
|
72 | 6 | 'dbUser' => $postArray['dbUser'], |
|
73 | 6 | 'dbPassword' => $postArray['dbPassword'], |
|
74 | 6 | 'dbPrefix' => $postArray['dbPrefix'] |
|
75 | ]; |
||
76 | 6 | if (!$this->_touch($configArray)) |
|
77 | { |
||
78 | 1 | return $this->_error( |
|
79 | [ |
||
80 | 1 | 'url' => 'install.php', |
|
81 | 1 | 'message' => $this->_language->get('directory_permission_grant') . $this->_language->get('point') |
|
82 | ]); |
||
83 | } |
||
84 | |||
85 | /* write config */ |
||
86 | |||
87 | 5 | if (!$this->_write($configArray)) |
|
88 | { |
||
89 | 1 | return $this->_error( |
|
90 | [ |
||
91 | 1 | 'url' => 'install.php', |
|
92 | 1 | 'message' => $this->_language->get('file_permission_grant') . $this->_language->get('colon') . ' config.php' |
|
93 | ]); |
||
94 | } |
||
95 | |||
96 | /* refresh connection */ |
||
97 | |||
98 | 4 | if ($postArray['refreshConnection']) |
|
99 | { |
||
100 | $this->_refreshConnection(); |
||
101 | } |
||
102 | |||
103 | /* handle database */ |
||
104 | |||
105 | 4 | if ($this->_getStatus() === 0) |
|
106 | { |
||
107 | 1 | return $this->_error( |
|
108 | [ |
||
109 | 1 | 'url' => 'install.php', |
|
110 | 1 | 'message' => $this->_language->get('database_failed') |
|
111 | ]); |
||
112 | } |
||
113 | |||
114 | /* handle install */ |
||
115 | |||
116 | $adminArray = |
||
117 | [ |
||
118 | 3 | 'adminName' => $postArray['adminName'], |
|
119 | 3 | 'adminUser' => $postArray['adminUser'], |
|
120 | 3 | 'adminEmail' => $postArray['adminEmail'], |
|
121 | 3 | 'adminPassword' => $postArray['adminPassword'] |
|
122 | ]; |
||
123 | 3 | if (!$this->_install($adminArray)) |
|
124 | { |
||
125 | 1 | return $this->_error( |
|
126 | [ |
||
127 | 1 | 'url' => 'install.php', |
|
128 | 1 | 'message' => $this->_language->get('installation_failed') |
|
129 | ]); |
||
130 | } |
||
131 | |||
132 | /* handle mail */ |
||
133 | |||
134 | $mailArray = |
||
135 | [ |
||
136 | 2 | 'adminName' => $postArray['adminName'], |
|
137 | 2 | 'adminUser' => $postArray['adminUser'], |
|
138 | 2 | 'adminEmail' => $postArray['adminEmail'] |
|
139 | ]; |
||
140 | 2 | if (!$this->_mail($mailArray)) |
|
141 | { |
||
142 | 1 | return $this->_warning( |
|
143 | [ |
||
144 | 1 | 'url' => 'index.php', |
|
145 | 1 | 'message' => $this->_language->get('email_failed') |
|
146 | ]); |
||
147 | } |
||
148 | |||
149 | /* handle success */ |
||
150 | |||
151 | 1 | return $this->_success( |
|
152 | [ |
||
153 | 1 | 'url' => 'index.php', |
|
154 | 1 | 'message' => $this->_language->get('installation_completed') |
|
155 | ]); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * sanitize the post |
||
160 | * |
||
161 | * @since 4.0.0 |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | |||
166 | 8 | protected function _sanitizePost() : array |
|
191 | |||
192 | /** |
||
193 | * validate the database |
||
194 | * |
||
195 | * @since 3.0.0 |
||
196 | * |
||
197 | * @param array $postArray array of the post |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | |||
202 | 11 | protected function _validateDatabase(array $postArray = []) : array |
|
226 | |||
227 | /** |
||
228 | * validate the account |
||
229 | * |
||
230 | * @since 3.0.0 |
||
231 | * |
||
232 | * @param array $postArray array of the post |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | |||
237 | 10 | protected function _validateAccount(array $postArray = []) : array |
|
281 | |||
282 | /** |
||
283 | * touch sqlite file |
||
284 | * |
||
285 | * @since 3.0.0 |
||
286 | * |
||
287 | * @param array $configArray |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | |||
292 | 5 | protected function _touch(array $configArray = []) : bool |
|
301 | |||
302 | /** |
||
303 | * write config file |
||
304 | * |
||
305 | * @since 3.0.0 |
||
306 | * |
||
307 | * @param array $configArray |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | |||
312 | 4 | protected function _write(array $configArray = []) : bool |
|
322 | |||
323 | /** |
||
324 | * get the status |
||
325 | * |
||
326 | * @since 3.0.0 |
||
327 | * |
||
328 | * @return int |
||
329 | */ |
||
330 | |||
331 | 4 | protected function _getStatus() : int |
|
335 | |||
336 | /** |
||
337 | * refresh the connection |
||
338 | * |
||
339 | * @since 3.0.0 |
||
340 | */ |
||
341 | |||
342 | protected function _refreshConnection() : void |
||
347 | |||
348 | /** |
||
349 | * install the database |
||
350 | * |
||
351 | * @since 3.0.0 |
||
352 | * |
||
353 | * @param array $installArray |
||
354 | * |
||
355 | * @return bool |
||
356 | */ |
||
357 | |||
358 | 4 | protected function _install(array $installArray = []) : bool |
|
381 | |||
382 | /** |
||
383 | * send the mail |
||
384 | * |
||
385 | * @since 3.0.0 |
||
386 | * |
||
387 | * @param array $mailArray |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | |||
392 | 1 | protected function _mail(array $mailArray = []) : bool |
|
431 | |||
432 | /** |
||
433 | * show the success |
||
434 | * |
||
435 | * @since 3.0.0 |
||
436 | * |
||
437 | * @param array $successArray array of the success |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | |||
442 | 1 | protected function _success(array $successArray = []) : string |
|
450 | |||
451 | /** |
||
452 | * show the warning |
||
453 | * |
||
454 | * @since 3.0.0 |
||
455 | * |
||
456 | * @param array $warningArray array of the warning |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | |||
461 | 1 | protected function _warning(array $warningArray = []) : string |
|
469 | |||
470 | /** |
||
471 | * show the error |
||
472 | * |
||
473 | * @since 3.0.0 |
||
474 | * |
||
475 | * @param array $errorArray array of the error |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | |||
480 | 6 | protected function _error(array $errorArray = []) : string |
|
487 | } |
||
488 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.