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 |
|
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 | $values = $form->getValues(); |
||
23 | $user = \mQueue\Model\UserMapper::insertUser($values); |
||
24 | |||
25 | \mQueue\Model\User::setCurrent($user); |
||
26 | |||
27 | $this->_helper->FlashMessenger('Subscription complete.'); |
||
28 | |||
29 | return $this->_helper->redirector('index', 'movie'); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | 2 | $this->view->form = $form; |
|
34 | 2 | } |
|
35 | |||
36 | public function loginAction() |
||
37 | { |
||
38 | $request = $this->getRequest(); |
||
39 | $form = new \mQueue\Form\Login(); |
||
40 | |||
41 | if ($this->getRequest()->isPost()) { |
||
42 | if ($form->isValid($request->getPost())) { |
||
43 | $values = $form->getValues(); |
||
44 | |||
45 | $user = \mQueue\Model\UserMapper::findEmailPassword($values['email'], $values['password']); |
||
46 | if ($user) { |
||
47 | \mQueue\Model\User::setCurrent($user); |
||
48 | |||
49 | $this->_helper->FlashMessenger('Logged in.'); |
||
50 | |||
51 | $referrer = $values['referrer']; |
||
52 | |||
53 | // If we have a valid referer to one page of ourselve (except login or logout), redirect to it |
||
54 | if (mb_strpos($referrer, $this->view->serverUrl() . $this->view->baseUrl()) === 0 |
||
55 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'login'])) !== 0 |
||
56 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'logout'])) !== 0) { |
||
57 | return $this->_redirect($values['referrer']); |
||
58 | } |
||
59 | |||
60 | 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 | public function logoutAction(): void |
||
74 | { |
||
75 | \mQueue\Model\User::setCurrent(null); |
||
76 | } |
||
77 | |||
78 | View Code Duplication | public function viewAction(): void |
|
92 | } |
||
93 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.