| Conditions | 14 |
| Paths | 1400 |
| Total Lines | 80 |
| Lines | 6 |
| Ratio | 7.5 % |
| 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 |
||
| 174 | public function update( $data ) { |
||
| 175 | $model_name = $this->model_name; |
||
| 176 | $data = (array) $data; |
||
| 177 | $req_method = ( ! empty( $data['id'] ) ) ? 'put' : 'post'; |
||
| 178 | $args_cb = array( |
||
| 179 | 'model_name' => $model_name, |
||
| 180 | 'req_method' => $req_method, |
||
| 181 | 'meta_key' => $this->meta_key, |
||
| 182 | ); |
||
| 183 | |||
| 184 | // Vérifie l'existence du type. |
||
| 185 | if ( empty( $data['type'] ) ) { |
||
| 186 | $data['type'] = $this->get_type(); |
||
| 187 | } |
||
| 188 | |||
| 189 | if ( empty( $data['id'] ) ) { |
||
| 190 | |||
| 191 | if ( ! empty( $data['author_id'] ) ) { |
||
| 192 | $user = get_userdata( $data['author_id'] ); |
||
| 193 | } else { |
||
| 194 | $user = wp_get_current_user(); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ( $user->exists() ) { |
||
| 198 | if ( empty( $data['author_id'] ) ) { |
||
| 199 | $data['author_id'] = $user->ID; |
||
| 200 | } |
||
| 201 | |||
| 202 | if ( empty( $data['author_nicename'] ) ) { |
||
| 203 | $data['author_nicename'] = $user->display_name; |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( empty( $data['author_email'] ) ) { |
||
| 207 | $data['author_email'] = $user->user_email; |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( empty( $data['author_url'] ) ) { |
||
| 211 | $data['author_url'] = $user->user_url; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $data = apply_filters( 'eo_model_comment_before_' . $req_method, $data, $args_cb ); |
||
| 217 | // Il ne faut pas lancer plusieurs fois pour ping. |
||
| 218 | if ( 'ping' !== $this->get_type() ) { |
||
| 219 | $data = apply_filters( 'eo_model_' . $this->get_type() . '_before_' . $req_method, $data, $args_cb ); |
||
| 220 | } |
||
| 221 | $args_cb['data'] = $data; |
||
| 222 | |||
| 223 | $object = new $model_name( $data, $req_method ); |
||
| 224 | |||
| 225 | if ( empty( $object->data['id'] ) ) { |
||
| 226 | add_filter( 'duplicate_comment_id', '__return_false' ); |
||
| 227 | add_filter( 'pre_comment_approved', function( $approved, $comment_data ) { |
||
| 228 | return $comment_data['comment_approved']; |
||
| 229 | }, 10, 2 ); |
||
| 230 | $inserted_comment = wp_insert_comment( $object->convert_to_wordpress() ); |
||
| 231 | if ( is_wp_error( $inserted_comment ) ) { |
||
| 232 | return $inserted_comment; |
||
| 233 | } |
||
| 234 | |||
| 235 | $object->data['id'] = $inserted_comment; |
||
| 236 | } else { |
||
| 237 | wp_update_comment( $object->convert_to_wordpress() ); |
||
| 238 | } |
||
| 239 | |||
| 240 | $object = apply_filters( 'eo_model_comment_after_' . $req_method, $object, $args_cb ); |
||
| 241 | |||
| 242 | $object = $this->get( array( |
||
| 243 | 'id' => $object->data['id'], |
||
| 244 | 'status' => array( '1', 'trash' ), |
||
| 245 | ), true ); |
||
| 246 | |||
| 247 | // Il ne faut pas lancer plusieurs fois pour ping. |
||
| 248 | if ( 'ping' !== $this->get_type() ) { |
||
| 249 | $object = apply_filters( 'eo_model_' . $this->get_type() . '_after_' . $req_method, $object, $args_cb ); |
||
| 250 | } |
||
| 251 | |||
| 252 | return $object; |
||
| 253 | } |
||
| 254 | |||
| 257 |