| Conditions | 16 |
| Paths | 17 |
| Total Lines | 48 |
| Code Lines | 28 |
| Lines | 12 |
| Ratio | 25 % |
| 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 |
||
| 29 | function new_taxonomy( $path, $blog_id, $taxonomy_type ) { |
||
| 30 | $args = $this->query_args(); |
||
| 31 | $input = $this->input(); |
||
| 32 | View Code Duplication | if ( !is_array( $input ) || !$input || !strlen( $input['name'] ) ) { |
|
| 33 | return new WP_Error( 'invalid_input', 'Unknown data passed', 400 ); |
||
| 34 | } |
||
| 35 | |||
| 36 | $user = wp_get_current_user(); |
||
| 37 | View Code Duplication | if ( !$user || is_wp_error( $user ) || !$user->ID ) { |
|
| 38 | return new WP_Error( 'authorization_required', 'An active access token must be used to manage taxonomies.', 403 ); |
||
| 39 | } |
||
| 40 | |||
| 41 | $tax = get_taxonomy( $taxonomy_type ); |
||
| 42 | if ( !current_user_can( $tax->cap->edit_terms ) ) { |
||
| 43 | return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 ); |
||
| 44 | } |
||
| 45 | |||
| 46 | if ( 'category' !== $taxonomy_type || ! isset( $input['parent'] ) ) |
||
| 47 | $input['parent'] = 0; |
||
| 48 | |||
| 49 | View Code Duplication | if ( $term = get_term_by( 'name', $input['name'], $taxonomy_type ) ) { |
|
| 50 | // the same name is allowed as long as the parents are different |
||
| 51 | if ( $input['parent'] === $term->parent ) { |
||
| 52 | return new WP_Error( 'duplicate', 'A taxonomy with that name already exists', 400 ); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | $data = wp_insert_term( addslashes( $input['name'] ), $taxonomy_type, |
||
| 57 | array( |
||
| 58 | 'description' => isset( $input['description'] ) ? addslashes( $input['description'] ) : '', |
||
| 59 | 'parent' => $input['parent'] |
||
| 60 | ) |
||
| 61 | ); |
||
| 62 | |||
| 63 | if ( is_wp_error( $data ) ) |
||
| 64 | return $data; |
||
| 65 | |||
| 66 | $taxonomy = get_term_by( 'id', $data['term_id'], $taxonomy_type ); |
||
| 67 | |||
| 68 | $return = $this->get_taxonomy( $taxonomy->slug, $taxonomy_type, $args['context'] ); |
||
| 69 | if ( !$return || is_wp_error( $return ) ) { |
||
| 70 | return $return; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 74 | do_action( 'wpcom_json_api_objects', 'taxonomies' ); |
||
| 75 | return $return; |
||
| 76 | } |
||
| 77 | |||
| 155 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.