Conditions | 11 |
Paths | 53 |
Total Lines | 52 |
Code Lines | 33 |
Lines | 13 |
Ratio | 25 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
51 | public function authenticate(TokenInterface $token) |
||
52 | { |
||
53 | if (!$this->supports($token)) { |
||
54 | $this->log(sprintf('Token not supported: %s', $token)); |
||
55 | return null; |
||
56 | } |
||
57 | |||
58 | if (!$user = $token->getUser()) { |
||
59 | $this->log(sprintf('User not found in token: %s', $token)); |
||
60 | throw new BadCredentialsException('User not found in request.'); |
||
61 | } |
||
62 | |||
63 | // Reattach the objects to Doctrine |
||
64 | $username = $token->getUsername(); |
||
65 | |||
66 | try { |
||
67 | View Code Duplication | if ($user instanceof UserInterface) { |
|
|
|||
68 | $token->setUser($this->userProvider->refreshUser($user)); |
||
69 | $user = $token->getUser(); |
||
70 | } else { |
||
71 | $user = $this->userProvider->loadUserByUsername($username); |
||
72 | } |
||
73 | if (empty($user) || !$user instanceof UserInterface) { |
||
74 | $this->log(sprintf('User not found for username "%s"', $username)); |
||
75 | throw new AuthenticationException('Shibboleth authentication failed.'); |
||
76 | } |
||
77 | $this->log(sprintf('User found for username "%s": %s', $username, $user)); |
||
78 | |||
79 | foreach ($token->getRoles() as $role) { |
||
80 | View Code Duplication | if ($role instanceof SwitchUserRole) { |
|
81 | $source = $role->getSource(); |
||
82 | $sourceUser = $source->getUser(); |
||
83 | if ($sourceUser instanceof UserInterface) { |
||
84 | $source->setUser($this->userProvider->refreshUser($sourceUser)); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } catch (UsernameNotFoundException $notFound) { |
||
89 | $this->log(sprintf('User not found for username "%s": %s', $username, $notFound->getMessage())); |
||
90 | throw new AuthenticationException('Shibboleth authentication failed.', 0, $notFound); |
||
91 | } |
||
92 | |||
93 | if ($user instanceof UserInterface) { |
||
94 | $this->userChecker->checkPostAuth($user); |
||
95 | } |
||
96 | |||
97 | $authenticatedToken = new KuleuvenUserToken($user, $user->getAttributes(), $this->providerKey, $token->getRoles()); |
||
98 | $authenticatedToken->setAuthenticated(true); |
||
99 | $this->log(sprintf('Token authenticated for username "%s": %s', $authenticatedToken->getUsername(), $authenticatedToken)); |
||
100 | |||
101 | return $authenticatedToken; |
||
102 | } |
||
103 | |||
112 |
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.