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 |
||
3 | class UserController extends Zend_Controller_Action |
||
4 | { |
||
5 | 3 | public function init(): void |
|
6 | { |
||
7 | // Initialize action controller here |
||
8 | 3 | } |
|
9 | |||
10 | 1 | public function indexAction(): void |
|
11 | { |
||
12 | 1 | $this->view->users = \mQueue\Model\UserMapper::fetchAll(); |
|
13 | 1 | } |
|
14 | |||
15 | 2 | public function newAction() |
|
16 | { |
||
17 | 2 | $request = $this->getRequest(); |
|
18 | 2 | $form = new \mQueue\Form\User(); |
|
19 | |||
20 | 2 | if ($this->getRequest()->isPost()) { |
|
|
|||
21 | 2 | if ($form->isValid($request->getPost())) { |
|
22 | 2 | $values = $form->getValues(); |
|
23 | 2 | $user = \mQueue\Model\UserMapper::insertUser($values); |
|
24 | |||
25 | 2 | \mQueue\Model\User::setCurrent($user); |
|
26 | |||
27 | 2 | $this->_helper->FlashMessenger('Subscription complete.'); |
|
28 | |||
29 | 2 | return $this->_helper->redirector('index', 'movie'); |
|
30 | } |
||
31 | } |
||
32 | |||
33 | 2 | $this->view->form = $form; |
|
34 | 2 | } |
|
35 | |||
36 | 1 | public function loginAction() |
|
37 | { |
||
38 | 1 | $request = $this->getRequest(); |
|
39 | 1 | $form = new \mQueue\Form\Login(); |
|
40 | |||
41 | 1 | if ($this->getRequest()->isPost()) { |
|
42 | 1 | if ($form->isValid($request->getPost())) { |
|
43 | 1 | $values = $form->getValues(); |
|
44 | |||
45 | 1 | $user = \mQueue\Model\UserMapper::findEmailPassword($values['email'], $values['password']); |
|
46 | 1 | if ($user) { |
|
47 | 1 | \mQueue\Model\User::setCurrent($user); |
|
48 | |||
49 | 1 | $this->_helper->FlashMessenger('Logged in.'); |
|
50 | |||
51 | 1 | $referrer = $values['referrer']; |
|
52 | |||
53 | // If we have a valid referer to one page of ourselve (except login or logout), redirect to it |
||
54 | 1 | if (mb_strpos($referrer, $this->view->serverUrl() . $this->view->baseUrl()) === 0 |
|
55 | 1 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'login'])) !== 0 |
|
56 | 1 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'logout'])) !== 0) { |
|
57 | return $this->_redirect($values['referrer']); |
||
58 | } |
||
59 | |||
60 | 1 | return $this->_helper->redirector('index', 'movie'); |
|
61 | } |
||
62 | $this->_helper->FlashMessenger(['error' => 'Login failed.']); |
||
63 | } |
||
64 | } else { |
||
65 | $form->setDefaults([ |
||
66 | 'referrer' => $this->getRequest()->getServer('HTTP_REFERER'), |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | $this->view->form = $form; |
||
71 | } |
||
72 | |||
73 | 1 | public function logoutAction(): void |
|
74 | { |
||
75 | 1 | \mQueue\Model\User::setCurrent(null); |
|
76 | 1 | } |
|
77 | |||
78 | public function viewAction(): void |
||
91 | } |
||
92 | } |
||
93 |