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