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