| Conditions | 10 |
| Paths | 9 |
| Total Lines | 44 |
| Code Lines | 27 |
| Lines | 23 |
| Ratio | 52.27 % |
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 |
||
| 20 | static function user_role_change( $user_id ) { |
||
| 21 | if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) { |
||
| 22 | $current_user_id = get_current_user_id(); |
||
| 23 | wp_set_current_user( $user_id ); |
||
| 24 | $role = Jetpack::translate_current_user_to_role(); |
||
| 25 | $signed_role = Jetpack::sign_role( $role ); |
||
| 26 | wp_set_current_user( $current_user_id ); |
||
| 27 | |||
| 28 | $master_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 29 | $master_user_id = absint( $master_token->external_user_id ); |
||
| 30 | |||
| 31 | if ( ! $master_user_id ) { |
||
| 32 | return; |
||
| 33 | } // this shouldn't happen |
||
| 34 | |||
| 35 | Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role ); |
||
| 36 | //@todo retry on failure |
||
| 37 | |||
| 38 | //try to choose a new master if we're demoting the current one |
||
| 39 | View Code Duplication | if ( $user_id == $master_user_id && 'administrator' != $role ) { |
|
| 40 | $query = new WP_User_Query( |
||
| 41 | array( |
||
| 42 | 'fields' => array( 'id' ), |
||
| 43 | 'role' => 'administrator', |
||
| 44 | 'orderby' => 'id', |
||
| 45 | 'exclude' => array( $master_user_id ), |
||
| 46 | ) |
||
| 47 | ); |
||
| 48 | $new_master = false; |
||
| 49 | foreach ( $query->results as $result ) { |
||
| 50 | $uid = absint( $result->id ); |
||
| 51 | if ( $uid && Jetpack::is_user_connected( $uid ) ) { |
||
| 52 | $new_master = $uid; |
||
| 53 | break; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( $new_master ) { |
||
| 58 | Jetpack_Options::update_option( 'master_user', $new_master ); |
||
| 59 | } |
||
| 60 | // else disconnect..? |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.