Conditions | 10 |
Paths | 48 |
Total Lines | 35 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 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 |
||
23 | public function getUser(): ?UserInterface |
||
24 | { |
||
25 | try { |
||
26 | $token = $this->security->getToken(); |
||
27 | } catch (Exception $e) { |
||
28 | $token = null; |
||
29 | } |
||
30 | |||
31 | if (null === $token) { |
||
32 | return null; |
||
33 | } |
||
34 | |||
35 | $tokenUser = $token->getUser(); |
||
36 | if (!($tokenUser instanceof BaseUserInterface)) { |
||
37 | return null; |
||
38 | } |
||
39 | |||
40 | $impersonation = ''; |
||
41 | if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) { |
||
42 | // Symfony > 4.3 |
||
43 | if ($token instanceof SwitchUserToken) { |
||
44 | $impersonatorUser = $token->getOriginalToken()->getUser(); |
||
45 | } else { |
||
46 | $impersonatorUser = $this->getImpersonatorUser(); |
||
47 | } |
||
48 | |||
49 | if (\is_object($impersonatorUser)) { |
||
50 | $id = method_exists($impersonatorUser, 'getId') ? $impersonatorUser->getId() : null; |
||
51 | $username = method_exists($impersonatorUser, 'getUsername') ? $impersonatorUser->getUsername() : (string) $impersonatorUser; |
||
52 | $impersonation = ' [impersonator '.$username.':'.$id.']'; |
||
53 | } |
||
54 | } |
||
55 | $id = method_exists($tokenUser, 'getId') ? $tokenUser->getId() : null; |
||
56 | |||
57 | return new User($id, $tokenUser->getUsername().$impersonation); |
||
58 | } |
||
97 |