Conditions | 9 |
Paths | 9 |
Total Lines | 90 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
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 |
||
164 | public function action( $action, Backup $backup ) { |
||
165 | |||
166 | if ( 'hmbkp_backup_complete' === $action && $this->get_email_address_array() ) { |
||
167 | |||
168 | $file = $backup->get_backup_filepath(); |
||
169 | |||
170 | $sent = false; |
||
171 | |||
172 | $download = add_query_arg( 'hmbkp_download', base64_encode( $file ), HMBKP_ADMIN_URL ); |
||
173 | $domain = parse_url( home_url(), PHP_URL_HOST ) . parse_url( home_url(), PHP_URL_PATH ); |
||
174 | |||
175 | $headers = 'From: BackUpWordPress <' . apply_filters( 'hmbkp_from_email', get_bloginfo( 'admin_email' ) ) . '>' . "\r\n"; |
||
176 | |||
177 | // The backup failed, send a message saying as much |
||
178 | if ( ! file_exists( $file ) && ( $errors = array_merge( $backup->get_errors(), $backup->get_warnings() ) ) ) { |
||
179 | |||
180 | $error_message = ''; |
||
181 | |||
182 | foreach ( $errors as $error_set ) { |
||
183 | $error_message .= implode( "\n - ", $error_set ); |
||
184 | } |
||
185 | |||
186 | if ( $error_message ) { |
||
187 | $error_message = ' - ' . $error_message; |
||
188 | } |
||
189 | |||
190 | $subject = sprintf( |
||
191 | /* translators: Domain URL */ |
||
192 | esc_html__( 'Backup of %s Failed', 'backupwordpress' ), |
||
193 | $domain |
||
194 | ); |
||
195 | |||
196 | $message = sprintf( |
||
197 | /* translators: 1: Site URL */ |
||
198 | esc_html__( 'BackUpWordPress was unable to backup your site %1$s.', 'backupwordpress' ) . "\n\n" . |
||
199 | esc_html__( 'Here are the errors that we\'ve encountered:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
||
200 | esc_html__( 'If the errors above look like Martian, forward this email to %3$s and we\'ll take a look', 'backupwordpress' ) . "\n\n" . |
||
201 | home_url(), |
||
202 | $error_message, |
||
203 | '[email protected]' |
||
204 | ); |
||
205 | |||
206 | wp_mail( $this->get_email_address_array(), $subject, $message, $headers ); |
||
207 | |||
208 | return; |
||
209 | |||
210 | } |
||
211 | |||
212 | $subject = sprintf( |
||
213 | /* translators: Domain URL */ |
||
214 | esc_html__( 'Backup of %s', 'backupwordpress' ), |
||
215 | $domain |
||
216 | ); |
||
217 | |||
218 | // If it's larger than the max attachment size limit assume it's not going to be able to send the backup |
||
219 | if ( @filesize( $file ) < get_max_attachment_size() ) { |
||
220 | |||
221 | $message = sprintf( |
||
222 | /* translators: 1: Site URL */ |
||
223 | esc_html__( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . |
||
224 | esc_html__( 'The backup file should be attached to this email.', 'backupwordpress' ) . "\n\n" . |
||
225 | /* translators: 2: WordPress admin URL to BackupWordPress page */ |
||
226 | esc_html__( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
||
227 | esc_html__( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), |
||
228 | home_url(), |
||
229 | $download |
||
230 | ); |
||
231 | |||
232 | $sent = wp_mail( $this->get_email_address_array(), $subject, $message, $headers, $file ); |
||
233 | |||
234 | } |
||
235 | |||
236 | // If we didn't send the email above then send just the notification |
||
237 | if ( ! $sent ) { |
||
238 | |||
239 | $message = sprintf( |
||
240 | /* translators: 1: Site URL */ |
||
241 | esc_html__( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . |
||
242 | esc_html__( 'Unfortunately, the backup file was too large to attach to this email.', 'backupwordpress' ) . "\n\n" . |
||
243 | /* translators: 2: WordPress admin URL to BackupWordPress page */ |
||
244 | esc_html__( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . |
||
245 | esc_html__( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), |
||
246 | home_url(), |
||
247 | $download |
||
248 | ); |
||
249 | wp_mail( $this->get_email_address_array(), $subject, $message, $headers ); |
||
250 | |||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
264 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.