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 |
||
17 | final class FormAuthentication implements AuthenticationInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var PasswordManagerInterface |
||
21 | */ |
||
22 | private $passwordManager; |
||
23 | |||
24 | /** |
||
25 | * @var SessionInterface |
||
26 | */ |
||
27 | private $session; |
||
28 | |||
29 | const USER_KEY = 'u'; |
||
30 | |||
31 | /** |
||
32 | * @var RepositoryInterface |
||
33 | */ |
||
34 | private $userRepository; |
||
35 | |||
36 | /** |
||
37 | * @var LoggerInterface |
||
38 | */ |
||
39 | private $logger; |
||
40 | |||
41 | /** |
||
42 | * @param PasswordManagerInterface $passwordManager |
||
43 | * @param SessionInterface $session |
||
44 | * @param RepositoryInterface $userRepository |
||
45 | */ |
||
46 | public function __construct( |
||
57 | |||
58 | /** |
||
59 | * @param Request $request |
||
60 | * |
||
61 | * @throws AuthenticationExceptionInterface |
||
62 | */ |
||
63 | public function login(Request $request) |
||
93 | |||
94 | /** |
||
95 | * @param null|array|object $data |
||
96 | */ |
||
97 | private function checkingRequirements($data) |
||
118 | |||
119 | /** |
||
120 | * @param Request $request |
||
121 | */ |
||
122 | public function logout(Request $request) |
||
136 | |||
137 | /** |
||
138 | * @param Request $request |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isAuthenticated(Request $request): bool |
||
146 | |||
147 | /** |
||
148 | * @param Request $request |
||
149 | * |
||
150 | * @return UserPasswordInterface|null |
||
151 | */ |
||
152 | public function getAuthenticatedUser(Request $request) |
||
175 | |||
176 | /** |
||
177 | * @param Request $request |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | private function checkForUserIdWithinSession(Request $request): bool |
||
185 | |||
186 | /** |
||
187 | * @param Request $request |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | private function getUserIdFromSession(Request $request): string |
||
195 | |||
196 | /** |
||
197 | * @param array $criteria |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | private function getCriteriaAsSting(array $criteria): string |
||
210 | } |
||
211 |
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.