| Conditions | 10 |
| Paths | 52 |
| Total Lines | 40 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 35 | public function action_handler() { |
||
| 36 | $response = array( |
||
| 37 | 'success' => false, |
||
| 38 | 'error' => null, |
||
| 39 | ); |
||
| 40 | |||
| 41 | $action = isset( $_POST['action'] ) ? $_POST['action'] : ''; |
||
| 42 | $name = isset( $_POST['name'] ) ? $_POST['name'] : ''; |
||
|
1 ignored issue
–
show
|
|||
| 43 | $result = false; |
||
|
1 ignored issue
–
show
|
|||
| 44 | |||
| 45 | if ( empty( $action ) || empty( $name ) ) { |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | |||
| 49 | switch ( $action ) { |
||
| 50 | case 'carbon_add_sidebar': |
||
| 51 | $result = $this->add_sidebar( $name ); |
||
| 52 | break; |
||
| 53 | |||
| 54 | case 'carbon_remove_sidebar': |
||
| 55 | $result = $this->remove_sidebar( $name ); |
||
| 56 | break; |
||
| 57 | |||
| 58 | default: |
||
| 59 | $result = new \WP_Error( 'unknown-action', __( 'Unknown action attempted.', \Carbon_Fields\TEXT_DOMAIN ) ); |
||
| 60 | break; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ( is_wp_error( $result ) ) { |
||
| 64 | $response['error'] = $result->get_error_message(); |
||
| 65 | } else { |
||
| 66 | $response['success'] = (bool) $result; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
||
| 70 | wp_send_json( $response ); |
||
| 71 | } else { |
||
| 72 | return $response; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 165 |