| Conditions | 21 | 
| Paths | 245 | 
| Total Lines | 172 | 
| Code Lines | 72 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 8 | ||
| Bugs | 2 | Features | 2 | 
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 | $skipped_items_ids = array();  | 
            ||
| 127 | |||
| 128 | // we estimate the total encoded size as we go by encoding each item individually  | 
            ||
| 129 | // this is expensive, but the only way to really know :/  | 
            ||
| 130 | 		foreach ( $items as $key => $item ) { | 
            ||
| 131 | // Suspending cache addition help prevent overloading in memory cache of large sites.  | 
            ||
| 132 | wp_suspend_cache_addition( true );  | 
            ||
| 133 | /**  | 
            ||
| 134 | * Modify the data within an action before it is serialized and sent to the server  | 
            ||
| 135 | * For example, during full sync this expands Post ID's into full Post objects,  | 
            ||
| 136 | * so that we don't have to serialize the whole object into the queue.  | 
            ||
| 137 | *  | 
            ||
| 138 | * @since 4.2.0  | 
            ||
| 139 | *  | 
            ||
| 140 | * @param array The action parameters  | 
            ||
| 141 | * @param int The ID of the user who triggered the action  | 
            ||
| 142 | */  | 
            ||
| 143 | $item_value = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] );  | 
            ||
| 144 | wp_suspend_cache_addition( false );  | 
            ||
| 145 | 			if ( $item_value === false ) { | 
            ||
| 146 | $skipped_items_ids[] = $key;  | 
            ||
| 147 | continue;  | 
            ||
| 148 | }  | 
            ||
| 149 | 			if ( $item_value instanceof Traversable ) { | 
            ||
| 150 | 				foreach ( $item_value as $value ) { | 
            ||
| 151 | $new_item[0] = $item[0];  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 152 | |||
| 153 | $new_item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], array( $value ), $item[2] );  | 
            ||
| 154 | $new_item[2] = $item[2];  | 
            ||
| 155 | $new_item[3] = $item[3];  | 
            ||
| 156 | $new_item[4] = $item[4];  | 
            ||
| 157 | |||
| 158 | $encoded_item = $this->codec->encode( $new_item );  | 
            ||
| 159 | |||
| 160 | $upload_size += strlen( $encoded_item );  | 
            ||
| 161 | |||
| 162 | 					if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { | 
            ||
| 163 | break 2;  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | $last_item = end( $value );  | 
            ||
| 167 | $items_to_send[ $key . '-from-' . $last_item ] = $encoded_item;  | 
            ||
| 168 | }  | 
            ||
| 169 | 			} else { | 
            ||
| 170 | |||
| 171 | $item[1] = $item_value;  | 
            ||
| 172 | |||
| 173 | $encoded_item = $this->codec->encode( $item );  | 
            ||
| 174 | |||
| 175 | $upload_size += strlen( $encoded_item );  | 
            ||
| 176 | |||
| 177 | 				if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { | 
            ||
| 178 | break;  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | $items_to_send[ $key ] = $encoded_item;  | 
            ||
| 182 | }  | 
            ||
| 183 | }  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * Fires when data is ready to send to the server.  | 
            ||
| 187 | * Return false or WP_Error to abort the sync (e.g. if there's an error)  | 
            ||
| 188 | * The items will be automatically re-sent later  | 
            ||
| 189 | *  | 
            ||
| 190 | * @since 4.2.0  | 
            ||
| 191 | *  | 
            ||
| 192 | * @param array $data The action buffer  | 
            ||
| 193 | * @param string $codec The codec name used to encode the data  | 
            ||
| 194 | * @param double $time The current time  | 
            ||
| 195 | 		 * @param string $queue The queue used to send ('sync' or 'full_sync') | 
            ||
| 196 | */  | 
            ||
| 197 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id );  | 
            ||
| 198 | |||
| 199 | 		if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { | 
            ||
| 200 | $checked_in_item_ids = $queue->checkin( $buffer );  | 
            ||
| 201 | |||
| 202 | 			if ( is_wp_error( $checked_in_item_ids ) ) { | 
            ||
| 203 | error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() );  | 
            ||
| 204 | $queue->force_checkin();  | 
            ||
| 205 | }  | 
            ||
| 206 | |||
| 207 | 			if ( is_wp_error( $processed_item_ids ) ) { | 
            ||
| 208 | return $processed_item_ids;  | 
            ||
| 209 | }  | 
            ||
| 210 | |||
| 211 | // returning a WP_Error is a sign to the caller that we should wait a while  | 
            ||
| 212 | // before syncing again  | 
            ||
| 213 | return new WP_Error( 'server_error' );  | 
            ||
| 214 | |||
| 215 | 		} else { | 
            ||
| 216 | |||
| 217 | // detect if the last item ID was an error  | 
            ||
| 218 | $had_wp_error = is_wp_error( end( $processed_item_ids ) );  | 
            ||
| 219 | |||
| 220 | 			if ( $had_wp_error ) { | 
            ||
| 221 | $wp_error = array_pop( $processed_item_ids );  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | $items_to_update = array();  | 
            ||
| 225 | $last_processed_item_id = end( $processed_item_ids );  | 
            ||
| 226 | |||
| 227 | 			if ( preg_match( '/^jpsq_full_sync-(\d+\.\d+)-(\d+)-(\d+)-(\d+)$/', $last_processed_item_id, $matches ) ) { | 
            ||
| 228 | // detect if it was an item that we expanded during sending  | 
            ||
| 229 | $parent_item_key = chop( $last_processed_item_id, '-' . $matches[4] );  | 
            ||
| 230 | $items_to_update[ $parent_item_key ] = $items[ $parent_item_key ];  | 
            ||
| 231 | $items_to_update[ $parent_item_key ][1] = $matches[4];  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | // also checkin any items that were skipped  | 
            ||
| 235 | 			if ( count( $skipped_items_ids ) > 0 ) { | 
            ||
| 236 | $processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids );  | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) );  | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Allows us to keep track of all the actions that have been sent.  | 
            ||
| 243 | * Allows us to calculate the progress of specific actions.  | 
            ||
| 244 | *  | 
            ||
| 245 | * @since 4.2.0  | 
            ||
| 246 | *  | 
            ||
| 247 | * @param array $processed_actions The actions that we send successfully.  | 
            ||
| 248 | */  | 
            ||
| 249 | do_action( 'jetpack_sync_processed_actions', $processed_items );  | 
            ||
| 250 | |||
| 251 | $queue->close( $buffer, $processed_item_ids, $items_to_update );  | 
            ||
| 252 | |||
| 253 | // returning a WP_Error is a sign to the caller that we should wait a while  | 
            ||
| 254 | // before syncing again  | 
            ||
| 255 | 			if ( $had_wp_error ) { | 
            ||
| 256 | return $wp_error;  | 
            ||
| 257 | }  | 
            ||
| 258 | }  | 
            ||
| 259 | |||
| 260 | return true;  | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 356 | 
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.