Conditions | 19 |
Paths | 40 |
Total Lines | 54 |
Code Lines | 36 |
Lines | 3 |
Ratio | 5.56 % |
Changes | 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 |
||
68 | public function update_user( $user_id, $blog_id ) { |
||
69 | $input = $this->input(); |
||
70 | $user['ID'] = $user_id; |
||
|
|||
71 | $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; |
||
72 | |||
73 | if ( get_current_user_id() == $user_id && isset( $input['roles'] ) ) { |
||
74 | return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 ); |
||
75 | } |
||
76 | |||
77 | if ( $is_wpcom && $user_id !== get_current_user_id() && $user_id == wpcom_get_blog_owner( $blog_id ) ) { |
||
78 | return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 ); |
||
79 | } |
||
80 | |||
81 | if ( ! $is_wpcom ) { |
||
82 | foreach ( $input as $key => $value ) { |
||
83 | if ( ! is_array( $value ) ) { |
||
84 | $value = trim( $value ); |
||
85 | } |
||
86 | $value = wp_unslash( $value ); |
||
87 | switch ( $key ) { |
||
88 | case 'first_name': |
||
89 | case 'last_name': |
||
90 | $user[ $key ] = $value; |
||
91 | break; |
||
92 | case 'display_name': |
||
93 | case 'name': |
||
94 | $user[ 'display_name' ] = $value; |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if ( isset( $input[ 'roles' ] ) ) { |
||
101 | // For now, we only use the first role in the array. |
||
102 | if ( is_array( $input['roles'] ) ) { |
||
103 | $user['role'] = $input['roles'][0]; |
||
104 | } else if ( is_string( $input['roles'] ) ) { |
||
105 | $user['role'] = $input['roles']; |
||
106 | } else { |
||
107 | return new WP_Error( 'invalid_input', __( 'The roles property must be a string or an array.', 'jetpack' ), 400 ); |
||
108 | } |
||
109 | |||
110 | $editable_roles = array_keys( get_editable_roles() ); |
||
111 | View Code Duplication | if ( ! in_array( $user['role'], $editable_roles ) ) { |
|
112 | return new WP_Error( 'invalid_input', sprintf( __( '%s is not a valid role.', 'jetpack' ), $editable_roles ), 400 ); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $result = wp_update_user( $user ); |
||
117 | if ( is_wp_error( $result ) ) { |
||
118 | return $result; |
||
119 | } |
||
120 | return $this->get_user( $user_id ); |
||
121 | } |
||
122 | |||
124 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.