| Conditions | 9 |
| Paths | 192 |
| Total Lines | 84 |
| 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 |
||
| 22 | function gmb_upgrades_screen() { |
||
| 23 | |||
| 24 | $action = isset( $_GET['gmb-upgrade'] ) ? sanitize_text_field( $_GET['gmb-upgrade'] ) : ''; |
||
| 25 | $step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1; |
||
| 26 | $total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false; |
||
| 27 | $custom = isset( $_GET['custom'] ) ? absint( $_GET['custom'] ) : 0; |
||
| 28 | $number = isset( $_GET['number'] ) ? absint( $_GET['number'] ) : 100; |
||
| 29 | $steps = round( ( $total / $number ), 0 ); |
||
| 30 | |||
| 31 | $doing_upgrade_args = array( |
||
| 32 | 'page' => 'gmb-upgrades', |
||
| 33 | 'gmb-upgrade' => $action, |
||
| 34 | 'step' => $step, |
||
| 35 | 'total' => $total, |
||
| 36 | 'custom' => $custom, |
||
| 37 | 'steps' => $steps |
||
| 38 | ); |
||
| 39 | update_option( 'gmb_doing_upgrade', $doing_upgrade_args ); |
||
| 40 | if ( $step > $steps ) { |
||
| 41 | // Prevent a weird case where the estimate was off. Usually only a couple. |
||
| 42 | $steps = $step; |
||
| 43 | } |
||
| 44 | ?> |
||
| 45 | <div class="wrap"> |
||
| 46 | <h2><?php _e( 'Maps Builder - Upgrade', 'gmb' ); ?></h2> |
||
| 47 | |||
| 48 | <?php if ( ! empty( $action ) ) : ?> |
||
| 49 | |||
| 50 | <div id="gmb-upgrade-status"> |
||
| 51 | <p><?php _e( 'The upgrade process has started, please be patient. This could take several minutes. You will be automatically redirected when the upgrade is finished.', 'gmb' ); ?></p> |
||
| 52 | |||
| 53 | <?php if ( ! empty( $total ) ) : ?> |
||
| 54 | <p> |
||
| 55 | <strong><?php printf( __( 'Step %d of approximately %d running', 'gmb' ), $step, $steps ); ?></strong> |
||
| 56 | </p> |
||
| 57 | <?php endif; ?> |
||
| 58 | </div> |
||
| 59 | <script type="text/javascript"> |
||
| 60 | setTimeout( function () { |
||
| 61 | document.location.href = "index.php?gmb_action=<?php echo $action; ?>&step=<?php echo $step; ?>&total=<?php echo $total; ?>&custom=<?php echo $custom; ?>"; |
||
| 62 | }, 250 ); |
||
| 63 | </script> |
||
| 64 | |||
| 65 | <?php else : ?> |
||
| 66 | |||
| 67 | <div id="gmb-upgrade-status" class="updated" style="margin-top:15px;"> |
||
| 68 | <p style="margin-bottom:8px;"> |
||
| 69 | <?php _e( 'The upgrade process has started, please do not close your browser or refresh. This could take several minutes. You will be automatically redirected when the upgrade has finished.', 'gmb' ); ?> |
||
| 70 | <img src="<?php echo GMB_PLUGIN_URL . '/assets/img/loading.gif'; ?>" id="gmb-upgrade-loader" style="position:relative; top:3px;" /> |
||
| 71 | </p> |
||
| 72 | </div> |
||
| 73 | <script type="text/javascript"> |
||
| 74 | jQuery( document ).ready( function () { |
||
| 75 | // Trigger upgrades on page load |
||
| 76 | var data = {action: 'gmb_trigger_upgrades'}; |
||
| 77 | var el_upgrade_status = jQuery( '#gmb-upgrade-status' ); |
||
| 78 | |||
| 79 | //Trigger via AJAX |
||
| 80 | jQuery.post( ajaxurl, data, function ( response ) { |
||
| 81 | |||
| 82 | //Uncomment for debugging |
||
| 83 | // jQuery( '#gmb-upgrade-status' ).after( response ); |
||
| 84 | |||
| 85 | //Success Message |
||
| 86 | if ( response == 'complete' ) { |
||
| 87 | |||
| 88 | el_upgrade_status.hide(); |
||
| 89 | el_upgrade_status.after( '<div class="updated"><p>The upgrade process has completed successfully. Hooray! You will now be redirected back to your previous page.</p></div>' ); |
||
| 90 | |||
| 91 | //Send user back to prev page |
||
| 92 | setTimeout( function () { |
||
| 93 | history.back(); |
||
| 94 | }, 4000 ); |
||
| 95 | |||
| 96 | } |
||
| 97 | } ); |
||
| 98 | } ); |
||
| 99 | </script> |
||
| 100 | |||
| 101 | <?php endif; ?> |
||
| 102 | |||
| 103 | </div> |
||
| 104 | <?php |
||
| 105 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.