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() |
|
| 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 | 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 (mb_strpos($referrer, $this->view->serverUrl() . $this->view->baseUrl()) === 0 |
|
| 57 | 1 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'login'])) !== 0 |
|
| 58 | 1 | && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'logout'])) !== 0) { |
|
| 59 | return $this->_redirect($values['referrer']); |
||
| 60 | } |
||
| 61 | |||
| 62 | 1 | return $this->_helper->redirector('index', 'movie'); |
|
| 63 | } |
||
| 64 | $this->_helper->FlashMessenger(['error' => 'Login failed.']); |
||
| 65 | } |
||
| 66 | } else { |
||
| 67 | $form->setDefaults([ |
||
| 68 | 'referrer' => $this->getRequest()->getServer('HTTP_REFERER'), |
||
| 69 | ]); |
||
| 70 | } |
||
| 71 | |||
| 72 | $this->view->form = $form; |
||
| 73 | } |
||
| 74 | |||
| 75 | 1 | public function logoutAction(): void |
|
| 79 | |||
| 80 | View Code Duplication | public function viewAction(): void |
|
| 94 | } |
||
| 95 |
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.