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() |
|
6 | { |
||
7 | /* Initialize action controller here */ |
||
8 | 3 | } |
|
9 | |||
10 | 1 | public function indexAction() |
|
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()) { |
|
1 ignored issue
–
show
|
|||
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; |
|
1 ignored issue
–
show
|
|||
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()) { |
|
1 ignored issue
–
show
|
|||
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 | Zend_Session::rememberMe(1 * 60 * 60 * 24 * 31 * 2); // Cookie for two months |
|
48 | |||
49 | 1 | \mQueue\Model\User::setCurrent($user); |
|
50 | |||
51 | 1 | $this->_helper->FlashMessenger('Logged in.'); |
|
52 | |||
53 | 1 | $referrer = $values['referrer']; |
|
54 | |||
55 | // If we have a valid referer to one page of ourselve (except login or logout), redirect to it |
||
56 | 1 | if (strpos($referrer, $this->view->serverUrl() . $this->view->baseUrl()) === 0 |
|
2 ignored issues
–
show
|
|||
57 | 1 | && strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'login'])) !== 0 |
|
2 ignored issues
–
show
|
|||
58 | 1 | && strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'logout'])) !== 0) { |
|
2 ignored issues
–
show
|
|||
59 | return $this->_redirect($values['referrer']); |
||
60 | } else { |
||
61 | 1 | return $this->_helper->redirector('index', 'movie'); |
|
62 | } |
||
63 | } else { |
||
64 | $this->_helper->FlashMessenger(['error' => 'Login failed.']); |
||
65 | } |
||
66 | } |
||
67 | } else { |
||
68 | $form->setDefaults([ |
||
69 | 'referrer' => $this->getRequest()->getServer('HTTP_REFERER'), |
||
1 ignored issue
–
show
|
|||
70 | ]); |
||
71 | } |
||
72 | |||
73 | $this->view->form = $form; |
||
1 ignored issue
–
show
|
|||
74 | } |
||
75 | |||
76 | 1 | public function logoutAction() |
|
80 | |||
81 | View Code Duplication | public function viewAction() |
|
82 | { |
||
95 | } |
||
96 |
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.