| Conditions | 13 |
| Paths | 320 |
| Total Lines | 50 |
| Code Lines | 38 |
| Lines | 3 |
| Ratio | 6 % |
| 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 |
||
| 55 | public function disconnect( $args, $assoc_args ) { |
||
| 56 | if ( ! Jetpack::is_active() ) { |
||
| 57 | WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 61 | View Code Duplication | if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) { |
|
| 62 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ( in_array( $action, array( 'user' ) ) ) { |
||
| 66 | if ( isset( $args[1] ) ) { |
||
| 67 | $user_id = $args[1]; |
||
| 68 | if ( ctype_digit( $user_id ) ) { |
||
| 69 | $field = 'id'; |
||
| 70 | $user_id = (int) $user_id; |
||
| 71 | } elseif ( is_email( $user_id ) ) { |
||
| 72 | $field = 'email'; |
||
| 73 | $user_id = sanitize_user( $user_id, true ); |
||
| 74 | } else { |
||
| 75 | $field = 'login'; |
||
| 76 | $user_id = sanitize_user( $user_id, true ); |
||
| 77 | } |
||
| 78 | if ( ! $user = get_user_by( $field, $user_id ) ) { |
||
| 79 | WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) ); |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | WP_CLI::error( __( 'Please specify a user.', 'jetpack' ) ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | switch ( $action ) { |
||
| 87 | case 'blog': |
||
| 88 | Jetpack::log( 'disconnect' ); |
||
| 89 | Jetpack::disconnect(); |
||
| 90 | WP_CLI::success( __( 'Jetpack has been successfully disconnected.', 'jetpack' ) ); |
||
| 91 | break; |
||
| 92 | case 'user': |
||
| 93 | if ( Jetpack::unlink_user( $user->ID ) ) { |
||
| 94 | Jetpack::log( 'unlink', $user->ID ); |
||
|
|
|||
| 95 | WP_CLI::success( sprintf( __( '%s has been successfully disconnected.', 'jetpack' ), $action ) ); |
||
| 96 | } else { |
||
| 97 | WP_CLI::error( sprintf( __( '%s could not be disconnected. Are you sure they\'re connected currently?', 'jetpack' ), "{$user->login} <{$user->email}>" ) ); |
||
| 98 | } |
||
| 99 | break; |
||
| 100 | case 'prompt': |
||
| 101 | WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) ); |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 178 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: