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 |
||
13 | class UserController extends Base |
||
14 | { |
||
15 | |||
16 | View Code Duplication | public function loginForm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
|
17 | { |
||
18 | $template = new Template('login.twig'); |
||
19 | $template->title = 'Log in'; |
||
20 | $template->name = $this->getQueryParam($request, 'name'); |
||
21 | $response->getBody()->write($template->render()); |
||
22 | return $response; |
||
23 | } |
||
24 | |||
25 | public function login(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
26 | { |
||
27 | $name = $this->getBodyParam($request, 'name'); |
||
28 | $nameQueryString = $name ? "?name=$name" : ''; |
||
29 | if ($this->getBodyParam($request, 'register')) { |
||
30 | return new RedirectResponse($this->config->baseUrl() . '/register' . $nameQueryString); |
||
31 | } |
||
32 | if ($this->getBodyParam($request, 'remind')) { |
||
33 | return new RedirectResponse($this->config->baseUrl() . '/remind' . $nameQueryString); |
||
34 | } |
||
35 | $template = new Template('login.twig'); |
||
36 | $user = $this->db->query('SELECT id, password FROM users WHERE name=:n', ['n' => $name])->fetch(); |
||
37 | if (isset($user->password)) { |
||
38 | if (password_verify($this->getBodyParam($request, 'password'), $user->password)) { |
||
39 | session_regenerate_id(true); |
||
40 | $_SESSION['userid'] = $user->id; |
||
41 | return new RedirectResponse($this->config->baseUrl()); |
||
42 | } |
||
43 | } |
||
44 | $template->alert('warning', 'Acess denied.', true); |
||
45 | return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name); |
||
46 | } |
||
47 | |||
48 | public function logout(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
49 | { |
||
50 | session_unset(); |
||
51 | session_destroy(); |
||
52 | session_write_close(); |
||
53 | setcookie(session_name(), '', 0, '/'); |
||
54 | session_regenerate_id(true); |
||
55 | return new RedirectResponse($this->config->baseUrl() . '/login'); |
||
56 | } |
||
57 | |||
58 | public function registerForm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
59 | { |
||
60 | $template = new Template('register.twig'); |
||
61 | $template->title = 'Register'; |
||
62 | $template->name = $this->getAnyParam($request, 'name'); |
||
63 | $template->email = $this->getBodyParam($request, 'email'); |
||
64 | $response->getBody()->write($template->render()); |
||
65 | return $response; |
||
66 | } |
||
67 | |||
68 | public function register(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
69 | { |
||
70 | $name = $this->getBodyParam($request, 'name'); |
||
71 | $email = $this->getBodyParam($request, 'email'); |
||
72 | $template = new Template('register.twig'); |
||
73 | if ($this->getBodyParam($request, 'login')) { |
||
74 | return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name); |
||
75 | } |
||
76 | $password = $this->getBodyParam($request, 'password'); |
||
77 | View Code Duplication | if ($password !== $this->getBodyParam($request, 'password-confirmation')) { |
|
78 | $template->alert('warning', 'Your passwords did not match.', true); |
||
79 | return new RedirectResponse($this->config->baseUrl() . "/register?name=$name&email=$email"); |
||
80 | } |
||
81 | $user = new User($this->db); |
||
82 | $user->register($name, $email, $password); |
||
83 | $template->alert('success', 'Thank you for registering.', true); |
||
84 | return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $user->getName()); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | public function remindForm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
|
88 | { |
||
89 | $template = new Template('remind.twig'); |
||
90 | $template->title = 'Remind'; |
||
91 | $template->name = $this->getBodyParam($request, 'name'); |
||
92 | $response->getBody()->write($template->render()); |
||
93 | return $response; |
||
94 | } |
||
95 | |||
96 | public function remind(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
97 | { |
||
98 | $name = $this->getBodyParam($request, 'name'); |
||
99 | if ($this->getBodyParam($request, 'login')) { |
||
100 | return new RedirectResponse($this->config->baseUrl() . '/login?name=' . $name); |
||
101 | } |
||
102 | $config = new Config(); |
||
103 | $user = new User($this->db); |
||
104 | $user->loadByName($name); |
||
105 | $template = new Template('remind_email.twig'); |
||
106 | if (!empty($user->getEmail())) { |
||
107 | $template->user = $user; |
||
108 | $template->token = $user->getReminder(); |
||
109 | $message = Swift_Message::newInstance() |
||
110 | ->setSubject('Password reminder') |
||
111 | ->setFrom(array($config->siteEmail() => $config->siteTitle())) |
||
112 | ->setTo(array($user->getEmail() => $user->getName())) |
||
113 | ->setBody($template->render(), 'text/html'); |
||
114 | $this->email($message); |
||
115 | } else { |
||
116 | // Pause for a moment, so it's not so obvious which users' names are resulting in mail being sent. |
||
117 | sleep(5); |
||
118 | } |
||
119 | $template->alert('success', 'Please check your email', true); |
||
120 | return new RedirectResponse($this->config->baseUrl() . '/remind?name=' . $name); |
||
121 | } |
||
122 | |||
123 | public function remindResetForm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
124 | { |
||
125 | if (!isset($args['token'])) { |
||
126 | return new RedirectResponse($this->config->baseUrl() . '/remind'); |
||
127 | } |
||
128 | $template = new Template('remind_reset.twig'); |
||
129 | $template->title = 'Password Reset'; |
||
130 | $template->userid = $args['userid']; |
||
131 | $template->token = $args['token']; |
||
132 | $response->getBody()->write($template->render()); |
||
133 | return $response; |
||
134 | } |
||
135 | |||
136 | public function remindReset(ServerRequestInterface $request, ResponseInterface $response, array $args) |
||
137 | { |
||
138 | $template = new Template('remind_reset.twig'); |
||
139 | // First check that the passwords match. |
||
140 | $password = $request->getAttribute('password'); |
||
141 | if ($password !== $request->getAttribute('password-confirmation')) { |
||
142 | $template->alert('warning', 'Your passwords did not match.', true); |
||
143 | return new RedirectResponse($this->config->baseUrl() . "/remind/" . $args['userid'] . "/" . $args['token']); |
||
144 | } |
||
145 | // Then see if the token is valid. |
||
146 | $user = new User($this->db); |
||
147 | $user->load($args['userid']); |
||
148 | View Code Duplication | if (!$user->checkReminderToken($args['token'])) { |
|
149 | $template->alert('warning', 'That reminder token has expired. Please try again.', true); |
||
150 | return new RedirectResponse($this->config->baseUrl() . "/remind"); |
||
151 | } |
||
152 | // Finally change the password. This will delete the token as well. |
||
153 | $user->changePassword($password); |
||
154 | $template->alert('success', 'Your password has been changed. Please log in.', true); |
||
155 | return new RedirectResponse($this->config->baseUrl() . "/login?name=" . $user->getName()); |
||
156 | } |
||
157 | |||
158 | View Code Duplication | public function profile(ServerRequestInterface $request, ResponseInterface $response, array $args) |
|
159 | { |
||
160 | $template = new Template('profile.twig'); |
||
161 | $template->title = 'Profile'; |
||
162 | $response->getBody()->write($template->render()); |
||
163 | return $response; |
||
164 | } |
||
165 | } |
||
166 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.