| Conditions | 15 |
| Paths | 104 |
| Total Lines | 128 |
| 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 |
||
| 148 | protected function task( $update ) { |
||
| 149 | // Pause upgrade immediately if admin pausing upgrades. |
||
| 150 | if( $this->is_paused_process() ) { |
||
| 151 | wp_die(); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( empty( $update ) ) { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | // Delete cache. |
||
| 159 | self::flush_cache(); |
||
| 160 | |||
| 161 | /* @var Give_Updates $give_updates */ |
||
| 162 | $give_updates = Give_Updates::get_instance(); |
||
| 163 | $resume_update = get_option( |
||
| 164 | 'give_doing_upgrade', |
||
| 165 | |||
| 166 | // Default update. |
||
| 167 | array( |
||
| 168 | 'update_info' => $update, |
||
| 169 | 'step' => 1, |
||
| 170 | 'update' => 1, |
||
| 171 | 'heading' => sprintf( 'Update %s of {update_count}', 1 ), |
||
| 172 | 'percentage' => $give_updates->percentage, |
||
| 173 | 'total_percentage' => 0, |
||
| 174 | ) |
||
| 175 | ); |
||
| 176 | |||
| 177 | // Continuously skip update if previous update does not complete yet. |
||
| 178 | if ( |
||
| 179 | $resume_update['update_info']['id'] !== $update['id'] && |
||
| 180 | ! give_has_upgrade_completed( $resume_update['update_info']['id'] ) |
||
| 181 | ) { |
||
| 182 | return $update; |
||
| 183 | } |
||
| 184 | |||
| 185 | // Set params. |
||
| 186 | $resume_update['update_info'] = $update; |
||
| 187 | $give_updates->step = absint( $resume_update['step'] ); |
||
| 188 | $give_updates->update = absint( $resume_update['update'] ); |
||
| 189 | $is_parent_update_completed = $give_updates->is_parent_updates_completed( $update ); |
||
| 190 | |||
| 191 | |||
| 192 | // Skip update if dependency update does not complete yet. |
||
| 193 | if ( empty( $is_parent_update_completed ) ) { |
||
| 194 | // @todo: set error when you have only one update with invalid dependency |
||
| 195 | if ( ! is_null( $is_parent_update_completed ) ) { |
||
| 196 | return $update; |
||
| 197 | } |
||
| 198 | |||
| 199 | return false; |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | // Pause upgrade immediately if found following: |
||
| 204 | // 1. Running update number greater then total update count |
||
| 205 | // 2. Processing percentage greater then 100% |
||
| 206 | if( ( |
||
| 207 | 101 < $resume_update['total_percentage'] ) || |
||
| 208 | ( $give_updates->get_total_db_update_count() < $resume_update['update'] ) || |
||
| 209 | ! in_array( $resume_update['update_info']['id'], $give_updates->get_update_ids() ) |
||
| 210 | ) { |
||
| 211 | if( ! $this->is_paused_process() ){ |
||
| 212 | $give_updates->__pause_db_update(true); |
||
| 213 | } |
||
| 214 | |||
| 215 | update_option( 'give_upgrade_error', 1, false ); |
||
| 216 | |||
| 217 | $log_data = 'Update Task' . "\n"; |
||
| 218 | $log_data .= "Total update count: {$give_updates->get_total_db_update_count()}\n"; |
||
| 219 | $log_data .= 'Update IDs: ' . print_r( $give_updates->get_update_ids() , true ); |
||
| 220 | $log_data .= 'Update: ' . print_r( $resume_update , true ); |
||
| 221 | |||
| 222 | Give()->logs->add( 'Update Error', $log_data, 0, 'update' ); |
||
| 223 | |||
| 224 | wp_die(); |
||
| 225 | } |
||
| 226 | |||
| 227 | // Disable cache. |
||
| 228 | Give_Cache::disable(); |
||
| 229 | |||
| 230 | try{ |
||
| 231 | // Run update. |
||
| 232 | if ( is_array( $update['callback'] ) ) { |
||
| 233 | $update['callback'][0]->$update['callback'][1](); |
||
| 234 | } else { |
||
| 235 | $update['callback'](); |
||
| 236 | } |
||
| 237 | } catch ( Exception $e ){ |
||
| 238 | |||
| 239 | if( ! $this->is_paused_process() ){ |
||
| 240 | $give_updates->__pause_db_update(true); |
||
| 241 | } |
||
| 242 | |||
| 243 | $log_data = 'Update Task' . "\n"; |
||
| 244 | $log_data .= print_r( $resume_update, true ) . "\n\n"; |
||
| 245 | $log_data .= "Error\n {$e->getMessage()}"; |
||
| 246 | |||
| 247 | Give()->logs->add( 'Update Error', $log_data, 0, 'update' ); |
||
| 248 | update_option( 'give_upgrade_error', 1, false ); |
||
| 249 | |||
| 250 | wp_die(); |
||
| 251 | } |
||
| 252 | |||
| 253 | // Set update info. |
||
| 254 | $doing_upgrade_args = array( |
||
| 255 | 'update_info' => $update, |
||
| 256 | 'step' => ++ $give_updates->step, |
||
| 257 | 'update' => $give_updates->update, |
||
| 258 | 'heading' => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ), |
||
| 259 | 'percentage' => $give_updates->percentage, |
||
| 260 | 'total_percentage' => $give_updates->get_db_update_processing_percentage(), |
||
| 261 | ); |
||
| 262 | |||
| 263 | // Cache upgrade. |
||
| 264 | update_option( 'give_doing_upgrade', $doing_upgrade_args, false ); |
||
| 265 | |||
| 266 | // Enable cache. |
||
| 267 | Give_Cache::enable(); |
||
| 268 | |||
| 269 | // Check if current update completed or not. |
||
| 270 | if ( give_has_upgrade_completed( $update['id'] ) ) { |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | return $update; |
||
| 275 | } |
||
| 276 | |||
| 468 |
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.