Conditions | 9 |
Paths | 14 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
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 |
||
120 | public function changeUserPassword(AuthenticatableInterface $user, $password, $update = true) |
||
121 | { |
||
122 | if (!($user instanceof UserInterface)) { |
||
123 | return parent::changeUserPassword($user, $password); |
||
124 | } |
||
125 | |||
126 | if (!$this->validateAuthPassword($password)) { |
||
127 | throw new InvalidArgumentException( |
||
128 | 'Can not change password: password is invalid' |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | $userId = $user->getAuthId(); |
||
133 | |||
134 | if ($update && $userId) { |
||
135 | $userClass = get_class($user); |
||
136 | |||
137 | $this->logger->info(sprintf( |
||
138 | '[Authenticator] Changing password for user "%s" (%s)', |
||
139 | $userId, |
||
140 | $userClass |
||
141 | )); |
||
142 | } |
||
143 | |||
144 | $passwordKey = $user->getAuthPasswordKey(); |
||
145 | |||
146 | $user[$passwordKey] = password_hash($password, PASSWORD_DEFAULT); |
||
147 | $user['lastPasswordDate'] = 'now'; |
||
148 | $user['lastPasswordIp'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; |
||
149 | |||
150 | if ($update && $userId) { |
||
151 | $result = $user->update([ |
||
152 | $passwordKey, |
||
153 | 'last_password_date', |
||
154 | 'last_password_ip', |
||
155 | ]); |
||
156 | |||
157 | if ($result) { |
||
158 | $this->logger->notice(sprintf( |
||
159 | '[Authenticator] Password was changed for user "%s" (%s)', |
||
160 | $userId, |
||
161 | $userClass |
||
162 | )); |
||
163 | } else { |
||
164 | $this->logger->warning(sprintf( |
||
165 | '[Authenticator] Password failed to be changed for user "%s" (%s)', |
||
166 | $userId, |
||
167 | $userClass |
||
168 | )); |
||
169 | } |
||
170 | |||
171 | return $result; |
||
172 | } |
||
173 | |||
174 | return true; |
||
175 | } |
||
176 | } |
||
177 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: