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 |
||
8 | class AuthController extends Controller |
||
9 | { |
||
10 | public function getSignOut($request, $response) |
||
16 | |||
17 | public function getSignUp($request, $response) |
||
21 | |||
22 | public function getSignIn($request, $response) |
||
26 | |||
27 | public function postSignUp($request, $response) |
||
28 | { |
||
29 | if ($this->validator->validateRegisterForm($request)->getErrorsCount() > 0) { |
||
30 | $_SESSION['errors'] = $this->validator->getErrors(); |
||
31 | return $response->withRedirect($this->router->pathFor('auth.signup')); |
||
32 | } |
||
33 | |||
34 | $user = User::create([ |
||
35 | 'email' => $request->getParam('email'), |
||
36 | 'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT), |
||
37 | 'name' => $request->getParam('name'), |
||
38 | ]); |
||
39 | |||
40 | $this->flash->addMessage('success', 'Zostałeś zarejestrowany oraz zalogowany'); |
||
41 | |||
42 | $this->auth->attempt($user->email, $request->getParam('password')); |
||
43 | |||
44 | return $response->withRedirect($this->router->pathFor('home')); |
||
45 | } |
||
46 | |||
47 | public function postSignIn($request, $response) |
||
66 | } |
||
67 |