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