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 |
||
42 | class LdapGuardAuthenticator extends AbstractGuardAuthenticator |
||
43 | { |
||
44 | /** |
||
45 | * @var LdapManager |
||
46 | */ |
||
47 | protected $ldap; |
||
48 | |||
49 | /** |
||
50 | * @var LdapUserChecker |
||
51 | */ |
||
52 | protected $userChecker; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $domain; |
||
58 | |||
59 | /** |
||
60 | * @var EventDispatcherInterface |
||
61 | */ |
||
62 | protected $dispatcher; |
||
63 | |||
64 | /** |
||
65 | * @var AuthenticationSuccessHandlerInterface |
||
66 | */ |
||
67 | protected $successHandler; |
||
68 | |||
69 | /** |
||
70 | * @var AuthenticationFailureHandlerInterface |
||
71 | */ |
||
72 | protected $failureHandler; |
||
73 | |||
74 | /** |
||
75 | * @var AuthenticationEntryPointInterface |
||
76 | */ |
||
77 | protected $entryPoint; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $options = [ |
||
83 | 'hide_user_not_found_exceptions' => true, |
||
84 | 'username_parameter' => '_username', |
||
85 | 'password_parameter' => '_password', |
||
86 | 'domain_parameter' => '_ldap_domain', |
||
87 | 'post_only' => false, |
||
88 | 'remember_me' => false, |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * @param bool $hideUserNotFoundExceptions |
||
93 | * @param LdapUserChecker $userChecker |
||
94 | * @param LdapManager $ldap |
||
95 | * @param AuthenticationEntryPointInterface $entryPoint |
||
96 | * @param EventDispatcherInterface $dispatcher |
||
97 | * @param AuthenticationSuccessHandlerInterface $successHandler |
||
98 | * @param AuthenticationFailureHandlerInterface $failureHandler |
||
99 | * @param array $options |
||
100 | */ |
||
101 | public function __construct($hideUserNotFoundExceptions = true, LdapUserChecker $userChecker, LdapManager $ldap, AuthenticationEntryPointInterface $entryPoint, EventDispatcherInterface $dispatcher, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options) |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function getCredentials(Request $request) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function getUser($credentials, UserProviderInterface $userProvider) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function checkCredentials($credentials, UserInterface $user) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | View Code Duplication | public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | View Code Duplication | public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | View Code Duplication | public function start(Request $request, AuthenticationException $authException = null) |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function supportsRememberMe() |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function createAuthenticatedToken(UserInterface $user, $providerKey) |
||
263 | |||
264 | /** |
||
265 | * @param string $param |
||
266 | * @param Request $request |
||
267 | * @return string|null |
||
268 | */ |
||
269 | protected function getRequestParameter($param, Request $request) |
||
279 | |||
280 | /** |
||
281 | * If no LDAP credentials are in the config then attempt to use the user supplied credentials from the login. But |
||
282 | * only if we are using the LdapUserProvider. |
||
283 | * |
||
284 | * @param array $credentials |
||
285 | * @param UserProviderInterface $userProvider |
||
286 | */ |
||
287 | protected function setLdapCredentialsIfNeeded(array $credentials, UserProviderInterface $userProvider) |
||
303 | |||
304 | /** |
||
305 | * If the domain needs to a different context for the request, then switch it. |
||
306 | * |
||
307 | * @param array $credentials |
||
308 | */ |
||
309 | protected function switchDomainIfNeeded(array $credentials) |
||
315 | |||
316 | /** |
||
317 | * If the passed domain is not the current context, then switch back to it. |
||
318 | * |
||
319 | * @param string $domain |
||
320 | */ |
||
321 | protected function switchDomainBackIfNeeded($domain) |
||
327 | |||
328 | /** |
||
329 | * Determine whether or not the exception should be masked with a BadCredentials or not. |
||
330 | * |
||
331 | * @param \Exception $e |
||
332 | * @throws \Exception |
||
333 | */ |
||
334 | protected function hideOrThrow(\Exception $e) |
||
346 | } |
||
347 |
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.