| Conditions | 14 |
| Paths | 53 |
| Total Lines | 120 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 90 | public function do_sync_for_queue( $queue ) { |
||
| 91 | |||
| 92 | do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
||
| 93 | |||
| 94 | if ( $queue->size() === 0 ) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | // now that we're sure we are about to sync, try to |
||
| 99 | // ignore user abort so we can avoid getting into a |
||
| 100 | // bad state |
||
| 101 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 102 | ignore_user_abort( true ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
| 106 | |||
| 107 | if ( ! $buffer ) { |
||
| 108 | // buffer has no items |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( is_wp_error( $buffer ) ) { |
||
| 113 | // another buffer is currently sending |
||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | $upload_size = 0; |
||
| 118 | $items_to_send = array(); |
||
| 119 | $items = $buffer->get_items(); |
||
| 120 | |||
| 121 | // set up current screen to avoid errors rendering content |
||
| 122 | require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php'); |
||
| 123 | require_once(ABSPATH . 'wp-admin/includes/screen.php'); |
||
| 124 | set_current_screen( 'sync' ); |
||
| 125 | |||
| 126 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 127 | // this is expensive, but the only way to really know :/ |
||
| 128 | foreach ( $items as $key => $item ) { |
||
| 129 | /** |
||
| 130 | * Modify the data within an action before it is serialized and sent to the server |
||
| 131 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 132 | * so that we don't have to serialize the whole object into the queue. |
||
| 133 | * |
||
| 134 | * @since 4.2.0 |
||
| 135 | * |
||
| 136 | * @param array The action parameters |
||
| 137 | */ |
||
| 138 | $item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
||
| 139 | |||
| 140 | $encoded_item = $this->codec->encode( $item ); |
||
| 141 | |||
| 142 | $upload_size += strlen( $encoded_item ); |
||
| 143 | |||
| 144 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | |||
| 148 | $items_to_send[ $key ] = $encoded_item; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Fires when data is ready to send to the server. |
||
| 153 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 154 | * The items will be automatically re-sent later |
||
| 155 | * |
||
| 156 | * @since 4.2.0 |
||
| 157 | * |
||
| 158 | * @param array $data The action buffer |
||
| 159 | */ |
||
| 160 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id ); |
||
| 161 | |||
| 162 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
| 163 | $checked_in_item_ids = $queue->checkin( $buffer ); |
||
| 164 | |||
| 165 | if ( is_wp_error( $checked_in_item_ids ) ) { |
||
| 166 | error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
||
| 167 | $queue->force_checkin(); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ( is_wp_error( $processed_item_ids ) ) { |
||
| 171 | return $processed_item_ids; |
||
| 172 | } |
||
| 173 | |||
| 174 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 175 | // before syncing again |
||
| 176 | return new WP_Error( 'server_error' ); |
||
| 177 | |||
| 178 | } else { |
||
| 179 | |||
| 180 | // detect if the last item ID was an error |
||
| 181 | $had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
||
| 182 | |||
| 183 | if ( $had_wp_error ) { |
||
| 184 | $wp_error = array_pop( $processed_item_ids ); |
||
| 185 | } |
||
| 186 | |||
| 187 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Allows us to keep track of all the actions that have been sent. |
||
| 191 | * Allows us to calculate the progress of specific actions. |
||
| 192 | * |
||
| 193 | * @since 4.2.0 |
||
| 194 | * |
||
| 195 | * @param array $processed_actions The actions that we send successfully. |
||
| 196 | */ |
||
| 197 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
| 198 | |||
| 199 | $queue->close( $buffer, $processed_item_ids ); |
||
| 200 | |||
| 201 | // returning a WP_Error is a sign to the caller that we should wait a while |
||
| 202 | // before syncing again |
||
| 203 | if ( $had_wp_error ) { |
||
| 204 | return $wp_error; |
||
|
|
|||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 306 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: