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 bool |
||
51 | */ |
||
52 | protected $hideUserNotFoundExceptions; |
||
53 | |||
54 | /** |
||
55 | * @var LdapUserChecker |
||
56 | */ |
||
57 | protected $userChecker; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $domain; |
||
63 | |||
64 | /** |
||
65 | * @var RouterInterface |
||
66 | */ |
||
67 | protected $router; |
||
68 | |||
69 | /** |
||
70 | * @var EventDispatcherInterface |
||
71 | */ |
||
72 | protected $dispatcher; |
||
73 | |||
74 | /** |
||
75 | * @var string The entry point/start path route name. |
||
76 | */ |
||
77 | protected $startPath = 'login'; |
||
78 | |||
79 | /** |
||
80 | * @var AuthenticationSuccessHandlerInterface |
||
81 | */ |
||
82 | protected $successHandler; |
||
83 | |||
84 | /** |
||
85 | * @var AuthenticationFailureHandlerInterface |
||
86 | */ |
||
87 | protected $failureHandler; |
||
88 | |||
89 | /** |
||
90 | * @param bool $hideUserNotFoundExceptions |
||
91 | * @param LdapUserChecker $userChecker |
||
92 | * @param LdapManager $ldap |
||
93 | * @param RouterInterface $router |
||
94 | * @param EventDispatcherInterface $dispatcher |
||
95 | * @param AuthenticationSuccessHandlerInterface $successHandler |
||
96 | * @param AuthenticationFailureHandlerInterface $failureHandler |
||
97 | */ |
||
98 | public function __construct($hideUserNotFoundExceptions = true, LdapUserChecker $userChecker, LdapManager $ldap, RouterInterface $router, EventDispatcherInterface $dispatcher, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler) |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getCredentials(Request $request) |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getUser($credentials, UserProviderInterface $userProvider) |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function checkCredentials($credentials, UserInterface $user) |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | View Code Duplication | public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | View Code Duplication | public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function start(Request $request, AuthenticationException $authException = null) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function supportsRememberMe() |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function createAuthenticatedToken(UserInterface $user, $providerKey) |
||
257 | |||
258 | /** |
||
259 | * Set the entry point/start path. |
||
260 | * |
||
261 | * @param string $startPath |
||
262 | */ |
||
263 | public function setStartPath($startPath) |
||
268 | |||
269 | /** |
||
270 | * If the domain needs to a different context for the request, then switch it. |
||
271 | * |
||
272 | * @param array $credentials |
||
273 | */ |
||
274 | protected function switchDomainIfNeeded($credentials) |
||
280 | |||
281 | /** |
||
282 | * If the passed domain is not the current context, then switch back to it. |
||
283 | * |
||
284 | * @param string $domain |
||
285 | */ |
||
286 | protected function switchDomainBackIfNeeded($domain) |
||
292 | |||
293 | /** |
||
294 | * Determine whether or not the exception should be masked with a BadCredentials or not. |
||
295 | * |
||
296 | * @param \Exception $e |
||
297 | * @throws \Exception |
||
298 | */ |
||
299 | protected function hideOrThrow(\Exception $e) |
||
311 | |||
312 | /** |
||
313 | * Sets the start path based on the route name that was set and adds it to the default auth handlers. |
||
314 | */ |
||
315 | protected function configureAuthHandlers() |
||
327 | } |
||
328 |
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.