Conditions | 14 |
Paths | 64 |
Total Lines | 43 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
76 | public function __invoke(): ?AuditorUserInterface |
||
77 | { |
||
78 | $tokenUser = $this->getTokenUser(); |
||
79 | $impersonatorUser = $this->getImpersonatorUser(); |
||
80 | |||
81 | $identifier = null; |
||
82 | $username = null; |
||
83 | |||
84 | if (null !== $tokenUser && $tokenUser instanceof UserInterface) { |
||
85 | //Use full name of the user if possible |
||
86 | if ($tokenUser instanceof \App\Entity\User) { |
||
87 | $identifier = $tokenUser->getUsername(); |
||
88 | $username = (string) $tokenUser; |
||
89 | } else { |
||
90 | if (method_exists($tokenUser, 'getId')) { |
||
91 | $identifier = $tokenUser->getId(); |
||
92 | } |
||
93 | |||
94 | $username = $tokenUser->getUsername(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if (null !== $impersonatorUser && $impersonatorUser instanceof UserInterface) { |
||
99 | $username .= sprintf('[impersonator %s]', $impersonatorUser->getUsername()); |
||
100 | } |
||
101 | |||
102 | //Check if a username and identifier were manually provided |
||
103 | if (!empty($this->username) && !empty($this->identifier)) { |
||
104 | $username = $this->username; |
||
105 | $identifier = $this->identifier; |
||
106 | } elseif ($this->is_cli()) { //Check if we are on command line, then use the username of the user |
||
107 | $identifier = self::CLI_USER_IDENTIFER; |
||
108 | $username = 'CLI'; |
||
109 | if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) { |
||
110 | $username = sprintf('CLI [%s]', posix_getpwuid(posix_geteuid())['name']); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | if (null === $identifier && null === $username) { |
||
115 | return null; |
||
116 | } |
||
117 | |||
118 | return new User($identifier, $username); |
||
119 | } |
||
194 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.