| Conditions | 15 |
| Paths | 11 |
| Total Lines | 108 |
| Code Lines | 57 |
| 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 | if ( empty( $update ) ) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | /* @var Give_Updates $give_updates */ |
||
| 99 | $give_updates = Give_Updates::get_instance(); |
||
| 100 | $resume_update = get_option( |
||
| 101 | 'give_doing_upgrade', |
||
| 102 | |||
| 103 | // Default update. |
||
| 104 | array( |
||
| 105 | 'update_info' => $update, |
||
| 106 | 'step' => 1, |
||
| 107 | 'update' => 1, |
||
| 108 | 'heading' => sprintf( 'Update %s of {update_count}', 1 ), |
||
| 109 | 'percentage' => $give_updates->percentage, |
||
| 110 | ) |
||
| 111 | ); |
||
| 112 | |||
| 113 | // Continuously skip update if previous update does not complete yet. |
||
| 114 | if ( |
||
| 115 | $resume_update['update_info']['id'] !== $update['id'] && |
||
| 116 | ! give_has_upgrade_completed( $resume_update['update_info']['id'] ) |
||
| 117 | ) { |
||
| 118 | $batch = Give_Updates::$background_updater->get_all_batch(); |
||
| 119 | $batch_data_count = count( $batch->data ); |
||
| 120 | |||
| 121 | if ( ! empty( $batch ) && 1 === $batch_data_count ) { |
||
| 122 | if ( ! empty( $update['depend'] ) ) { |
||
| 123 | |||
| 124 | $give_updates = Give_Updates::get_instance(); |
||
| 125 | $all_updates = $give_updates->get_updates( 'database', 'all' ); |
||
| 126 | $all_update_ids = wp_list_pluck( $all_updates, 'id' ); |
||
| 127 | |||
| 128 | foreach ( $update['depend'] as $depend ) { |
||
| 129 | if ( give_has_upgrade_completed( $depend ) ) { |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ( in_array( $depend, $all_update_ids ) ) { |
||
| 134 | array_unshift( $batch->data, $all_updates[ array_search( $depend, $all_update_ids ) ] ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | if( $batch_data_count !== count( $batch->data ) ) { |
||
| 139 | update_option( $batch->key, $batch->data ); |
||
| 140 | $this->dispatch(); |
||
| 141 | |||
| 142 | wp_die(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | return $update; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Set params. |
||
| 152 | $resume_update['update_info'] = $update; |
||
| 153 | $give_updates->step = absint( $resume_update['step'] ); |
||
| 154 | $give_updates->update = absint( $resume_update['update'] ); |
||
| 155 | $is_parent_update_completed = $give_updates->is_parent_updates_completed( $update ); |
||
| 156 | |||
| 157 | |||
| 158 | // Skip update if dependency update does not complete yet. |
||
| 159 | if ( empty( $is_parent_update_completed ) ) { |
||
| 160 | // @todo: set error when you have only one update with invalid dependency |
||
| 161 | if ( ! is_null( $is_parent_update_completed ) ) { |
||
| 162 | return $update; |
||
| 163 | } |
||
| 164 | |||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Disable cache. |
||
| 169 | Give_Cache::disable(); |
||
| 170 | |||
| 171 | // Run update. |
||
| 172 | if ( is_array( $update['callback'] ) ) { |
||
| 173 | $update['callback'][0]->$update['callback'][1](); |
||
| 174 | } else { |
||
| 175 | $update['callback'](); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Set update info. |
||
| 179 | $doing_upgrade_args = array( |
||
| 180 | 'update_info' => $update, |
||
| 181 | 'step' => ++ $give_updates->step, |
||
| 182 | 'update' => $give_updates->update, |
||
| 183 | 'heading' => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ), |
||
| 184 | 'percentage' => $give_updates->percentage, |
||
| 185 | 'total_percentage' => $give_updates->get_db_update_processing_percentage(), |
||
| 186 | ); |
||
| 187 | |||
| 188 | // Cache upgrade. |
||
| 189 | update_option( 'give_doing_upgrade', $doing_upgrade_args ); |
||
| 190 | |||
| 191 | // Enable cache. |
||
| 192 | Give_Cache::enable(); |
||
| 193 | |||
| 194 | // Check if current update completed or not. |
||
| 195 | if ( give_has_upgrade_completed( $update['id'] ) ) { |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | |||
| 199 | return $update; |
||
| 200 | } |
||
| 201 | |||
| 237 |
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.