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 |
||
30 | class StartupController extends Controller |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * {@inheritDoc} |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $components = ['Flash']; |
||
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | * |
||
43 | * @param \Cake\Event\Event $event The event that was triggered |
||
44 | * @return void |
||
45 | */ |
||
46 | public function beforeFilter(Event $event) |
||
47 | { |
||
48 | if (is_readable(ROOT . '/config/settings.php')) { |
||
49 | $this->redirect('/'); |
||
50 | } |
||
51 | |||
52 | $this->_prepareLayout(); |
||
53 | $this->viewBuilder() |
||
54 | ->className('CMS\View\View') |
||
55 | ->theme(false) |
||
56 | ->layout('Installer.startup') |
||
57 | ->helpers(['Menu.Menu']); |
||
58 | |||
59 | if (!empty($this->request->query['locale']) && !in_array($this->request->params['action'], ['language', 'index'])) { |
||
60 | I18n::locale($this->request->query['locale']); |
||
61 | $this->request->session()->write('installation.language', I18n::locale()); |
||
62 | } elseif ($this->request->session()->read('installation.language')) { |
||
63 | I18n::locale($this->request->session()->read('installation.language')); |
||
64 | } |
||
65 | |||
66 | Router::addUrlFilter(function ($params, $request) { |
||
67 | if (!in_array($request->params['action'], ['language', 'index'])) { |
||
68 | $params['locale'] = I18n::locale(); |
||
69 | } |
||
70 | |||
71 | return $params; |
||
72 | }); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Main action. |
||
77 | * |
||
78 | * We redirect to first step of the installation process: `language`. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function index() |
||
90 | |||
91 | /** |
||
92 | * First step of the installation process. |
||
93 | * |
||
94 | * User must select the language they want to use for the installation process. |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function language() |
||
128 | |||
129 | /** |
||
130 | * Second step of the installation process. |
||
131 | * |
||
132 | * We check server requirements here. |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function requirements() |
||
152 | |||
153 | /** |
||
154 | * Third step of the installation process. |
||
155 | * |
||
156 | * License agreement. |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function license() |
||
169 | |||
170 | /** |
||
171 | * Fourth step of the installation process. |
||
172 | * |
||
173 | * User must introduce database connection information. |
||
174 | * |
||
175 | * @return void |
||
176 | */ |
||
177 | public function database() |
||
199 | |||
200 | /** |
||
201 | * Fifth step of the installation process. |
||
202 | * |
||
203 | * Create a new administrator user account. |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public function account() |
||
233 | |||
234 | /** |
||
235 | * Last step of the installation process. |
||
236 | * |
||
237 | * Here we say "thanks" and redirect to site's frontend or backend. |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | public function finish() |
||
260 | |||
261 | // @codingStandardsIgnoreStart |
||
262 | /** |
||
263 | * Shortcut for Controller::set('title_for_layout', ...) |
||
264 | * |
||
265 | * @param string $titleForLayout Page's title |
||
266 | * @return void |
||
267 | */ |
||
268 | protected function title($titleForLayout) |
||
272 | // @codingStandardsIgnoreEnd |
||
273 | |||
274 | // @codingStandardsIgnoreStart |
||
275 | /** |
||
276 | * Shortcut for Controller::set('description_for_layout', ...) |
||
277 | * |
||
278 | * @param string $descriptionForLayout Page's description |
||
279 | * @return void |
||
280 | */ |
||
281 | protected function description($descriptionForLayout) |
||
285 | // @codingStandardsIgnoreEnd |
||
286 | |||
287 | /** |
||
288 | * Gets an instance of ServerTest class. |
||
289 | * |
||
290 | * @return \Installer\Utility\ServerTest |
||
291 | */ |
||
292 | protected function _getTester() |
||
327 | |||
328 | /** |
||
329 | * Check if the given step name was completed. Or marks current step as |
||
330 | * completed. |
||
331 | * |
||
332 | * If $check is set to NULL, it marks current step (controller's action name) |
||
333 | * as completed. If $check is set to a string, it checks if that step was |
||
334 | * completed before. |
||
335 | * |
||
336 | * This allows steps to control user navigation, so users can not pass to the |
||
337 | * next step without completing all previous steps. |
||
338 | * |
||
339 | * @param null|string $check Name of the step to check, or false to mark as |
||
340 | * completed current step |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function _step($check = null) |
||
356 | |||
357 | /** |
||
358 | * Sets some view-variables used across all steps. |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | protected function _prepareLayout() |
||
393 | } |
||
394 |