| Conditions | 13 |
| Paths | 32 |
| Total Lines | 102 |
| Code Lines | 52 |
| 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 |
||
| 93 | protected function task( $update ) { |
||
| 94 | // Pause upgrade immediately if admin pausing upgrades. |
||
| 95 | if( $this->is_paused_process() ) { |
||
| 96 | wp_die(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( empty( $update ) ) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | /* @var Give_Updates $give_updates */ |
||
| 104 | $give_updates = Give_Updates::get_instance(); |
||
| 105 | $resume_update = get_option( |
||
| 106 | 'give_doing_upgrade', |
||
| 107 | |||
| 108 | // Default update. |
||
| 109 | array( |
||
| 110 | 'update_info' => $update, |
||
| 111 | 'step' => 1, |
||
| 112 | 'update' => 1, |
||
| 113 | 'heading' => sprintf( 'Update %s of {update_count}', 1 ), |
||
| 114 | 'percentage' => $give_updates->percentage, |
||
| 115 | 'total_percentage' => 0, |
||
| 116 | ) |
||
| 117 | ); |
||
| 118 | |||
| 119 | // Continuously skip update if previous update does not complete yet. |
||
| 120 | if ( |
||
| 121 | $resume_update['update_info']['id'] !== $update['id'] && |
||
| 122 | ! give_has_upgrade_completed( $resume_update['update_info']['id'] ) |
||
| 123 | ) { |
||
| 124 | return $update; |
||
| 125 | } |
||
| 126 | |||
| 127 | // Set params. |
||
| 128 | $resume_update['update_info'] = $update; |
||
| 129 | $give_updates->step = absint( $resume_update['step'] ); |
||
| 130 | $give_updates->update = absint( $resume_update['update'] ); |
||
| 131 | $is_parent_update_completed = $give_updates->is_parent_updates_completed( $update ); |
||
| 132 | |||
| 133 | |||
| 134 | // Skip update if dependency update does not complete yet. |
||
| 135 | if ( empty( $is_parent_update_completed ) ) { |
||
| 136 | // @todo: set error when you have only one update with invalid dependency |
||
| 137 | if ( ! is_null( $is_parent_update_completed ) ) { |
||
| 138 | return $update; |
||
| 139 | } |
||
| 140 | |||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | // Pause upgrade immediately if found following: |
||
| 146 | // 1. Running update number greater then total update count |
||
| 147 | // 2. Processing percentage greater then 100% |
||
| 148 | if( ( |
||
| 149 | 101 < $resume_update['total_percentage'] ) || |
||
| 150 | ( $give_updates->get_total_db_update_count() < $resume_update['update'] ) || |
||
| 151 | ! in_array( $resume_update['update_info']['id'], $give_updates->get_update_ids() ) |
||
| 152 | ) { |
||
| 153 | if( ! $this->is_paused_process() ){ |
||
| 154 | $give_updates->__pause_db_update(true); |
||
| 155 | } |
||
| 156 | |||
| 157 | update_option( 'give_upgrade_error', 1 ); |
||
| 158 | |||
| 159 | wp_die(); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Disable cache. |
||
| 163 | Give_Cache::disable(); |
||
| 164 | |||
| 165 | // Run update. |
||
| 166 | if ( is_array( $update['callback'] ) ) { |
||
| 167 | $update['callback'][0]->$update['callback'][1](); |
||
| 168 | } else { |
||
| 169 | $update['callback'](); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Set update info. |
||
| 173 | $doing_upgrade_args = array( |
||
| 174 | 'update_info' => $update, |
||
| 175 | 'step' => ++ $give_updates->step, |
||
| 176 | 'update' => $give_updates->update, |
||
| 177 | 'heading' => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ), |
||
| 178 | 'percentage' => $give_updates->percentage, |
||
| 179 | 'total_percentage' => $give_updates->get_db_update_processing_percentage(), |
||
| 180 | ); |
||
| 181 | |||
| 182 | // Cache upgrade. |
||
| 183 | update_option( 'give_doing_upgrade', $doing_upgrade_args ); |
||
| 184 | |||
| 185 | // Enable cache. |
||
| 186 | Give_Cache::enable(); |
||
| 187 | |||
| 188 | // Check if current update completed or not. |
||
| 189 | if ( give_has_upgrade_completed( $update['id'] ) ) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $update; |
||
| 194 | } |
||
| 195 | |||
| 310 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.