| Conditions | 14 |
| Paths | 224 |
| Total Lines | 48 |
| Code Lines | 37 |
| Lines | 15 |
| Ratio | 31.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 |
||
| 128 | public function module( $args, $assoc_args ) { |
||
| 129 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 130 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'activate', 'deactivate', 'toggle' ) ) ) { |
|
| 131 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( in_array( $action, array( 'activate', 'deactivate', 'toggle' ) ) ) { |
||
| 135 | if ( isset( $args[1] ) ) { |
||
| 136 | $module_slug = $args[1]; |
||
| 137 | if ( ! Jetpack::is_module( $module_slug ) ) { |
||
| 138 | WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) ); |
||
| 139 | } |
||
| 140 | if ( 'toggle' == $action ) { |
||
| 141 | $action = Jetpack::is_module_active( $module_slug ) ? 'deactivate' : 'activate'; |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) ); |
||
| 145 | $action = 'list'; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | switch ( $action ) { |
||
| 150 | case 'list': |
||
| 151 | WP_CLI::line( __( 'Available Modules:', 'jetpack' ) ); |
||
| 152 | $modules = Jetpack::get_available_modules(); |
||
| 153 | sort( $modules ); |
||
| 154 | foreach( $modules as $module_slug ) { |
||
| 155 | $active = Jetpack::is_module_active( $module_slug ) ? __( 'Active', 'jetpack' ) : __( 'Inactive', 'jetpack' ); |
||
| 156 | WP_CLI::line( "\t" . str_pad( $module_slug, 24 ) . $active ); |
||
| 157 | } |
||
| 158 | break; |
||
| 159 | View Code Duplication | case 'activate': |
|
| 160 | $module = Jetpack::get_module( $module_slug ); |
||
| 161 | Jetpack::log( 'activate', $module_slug ); |
||
| 162 | Jetpack::activate_module( $module_slug, false ); |
||
| 163 | WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) ); |
||
| 164 | break; |
||
| 165 | View Code Duplication | case 'deactivate': |
|
| 166 | $module = Jetpack::get_module( $module_slug ); |
||
| 167 | Jetpack::log( 'deactivate', $module_slug ); |
||
| 168 | Jetpack::deactivate_module( $module_slug ); |
||
| 169 | WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) ); |
||
| 170 | break; |
||
| 171 | case 'toggle': |
||
| 172 | // Will never happen, should have been handled above and changed to activate or deactivate. |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 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: