| Conditions | 4 |
| Paths | 4 |
| Total Lines | 54 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 23 | function receive( $data, $token = null ) { |
||
| 24 | $start_time = microtime( true ); |
||
| 25 | if ( ! is_array( $data ) ) { |
||
| 26 | return new WP_Error( 'action_decoder_error', 'Events must be an array' ); |
||
| 27 | } |
||
| 28 | |||
| 29 | $events = wp_unslash( array_map( array( $this->codec, 'decode' ), $data ) ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Fires when an array of actions are received from a remote Jetpack site |
||
| 33 | * |
||
| 34 | * @since 4.1 |
||
| 35 | * |
||
| 36 | * @param array Array of actions received from the remote site |
||
| 37 | */ |
||
| 38 | do_action( "jetpack_sync_remote_actions", $events, $token ); |
||
| 39 | |||
| 40 | foreach ( $events as $key => $event ) { |
||
| 41 | list( $action_name, $args, $user_id, $timestamp ) = $event; |
||
| 42 | /** |
||
| 43 | * Fires when an action is received from a remote Jetpack site |
||
| 44 | * |
||
| 45 | * @since 4.1 |
||
| 46 | * |
||
| 47 | * @param string $action_name The name of the action executed on the remote site |
||
| 48 | * @param array $args The arguments passed to the action |
||
| 49 | * @param int $user_id The external_user_id who did the action |
||
| 50 | * @param double $timestamp Timestamp (in seconds) when the action occurred |
||
| 51 | * @param array $token The auth token used to invoke the API |
||
| 52 | */ |
||
| 53 | do_action( 'jetpack_sync_remote_action', $action_name, $args, $user_id, $timestamp, $token ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Fires when an action is received from a remote Jetpack site |
||
| 57 | * |
||
| 58 | * @since 4.1 |
||
| 59 | * |
||
| 60 | * @param array $args The arguments passed to the action |
||
| 61 | * @param int $user_id The external_user_id who did the action |
||
| 62 | * @param double $timestamp Timestamp (in seconds) when the action occurred |
||
| 63 | * @param array $token The auth token used to invoke the API |
||
| 64 | */ |
||
| 65 | do_action( 'jetpack_sync_' . $action_name, $args, $user_id, $timestamp, $token ); |
||
| 66 | |||
| 67 | $this->events_processed[] = $key; |
||
| 68 | |||
| 69 | // TODO this can be improved to be more intelligent |
||
| 70 | if ( microtime( true ) - $start_time > self::MAX_TIME_PER_REQUEST_IN_SECONDS ) { |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $this->events_processed; |
||
| 76 | } |
||
| 77 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: