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 |
||
| 18 | final class AuthController extends AbstractController |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var Router |
||
| 22 | */ |
||
| 23 | private $router; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Template |
||
| 27 | */ |
||
| 28 | private $template; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var SessionManager |
||
| 32 | */ |
||
| 33 | private $session; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var AdminUserService |
||
| 37 | */ |
||
| 38 | private $adminUserService; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * AuthController constructor. |
||
| 42 | * |
||
| 43 | * @param Router $router router |
||
| 44 | * @param Template $template template engine |
||
| 45 | * @param SessionManager $session session manager |
||
| 46 | * @param AdminUserService $adminUserService admin user service |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function __construct( |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Performs session check and redirects user to appropriate page or displays login page. |
||
| 62 | * |
||
| 63 | * @return \Psr\Http\Message\ResponseInterface |
||
| 64 | */ |
||
| 65 | public function login($error = false): \Psr\Http\Message\ResponseInterface |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Performs user credentials check and registers admin session if valid. |
||
| 76 | * |
||
| 77 | * @return \Psr\Http\Message\ResponseInterface |
||
| 78 | */ |
||
| 79 | public function loginHandle(): \Psr\Http\Message\ResponseInterface |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return \Psr\Http\Message\ResponseInterface |
||
| 103 | */ |
||
| 104 | public function logout(): \Psr\Http\Message\ResponseInterface |
||
| 110 | } |
||
| 111 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.