Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
||
| 7 | |||
| 8 | // POST /sites/%s/plugins/%s/install |
||
| 9 | protected $needed_capabilities = 'install_plugins'; |
||
| 10 | protected $action = 'install'; |
||
| 11 | |||
| 12 | protected function install() { |
||
| 13 | foreach ( $this->plugins as $index => $slug ) { |
||
| 14 | |||
| 15 | $skin = new Jetpack_Automatic_Install_Skin(); |
||
| 16 | $upgrader = new Plugin_Upgrader( $skin ); |
||
| 17 | $zip_url = self::generate_wordpress_org_plugin_download_link( $slug ); |
||
| 18 | |||
| 19 | $result = $upgrader->install( $zip_url ); |
||
| 20 | |||
| 21 | if ( ! $this->bulk && is_wp_error( $result ) ) { |
||
| 22 | return $result; |
||
| 23 | } |
||
| 24 | |||
| 25 | $plugin = self::get_plugin_id_by_slug( $slug ); |
||
| 26 | $error_code = 'install_error'; |
||
| 27 | if ( ! $plugin ) { |
||
| 28 | $error = $this->log[ $slug ]['error'] = __( 'There was an error installing your plugin', 'jetpack' ); |
||
| 29 | } |
||
| 30 | |||
| 31 | if ( ! $this->bulk && ! $result ) { |
||
| 32 | $error_code = $upgrader->skin->get_main_error_code(); |
||
| 33 | $message = $upgrader->skin->get_main_error_message(); |
||
| 34 | $error = $this->log[ $slug ]['error'] = $message ? $message : __( 'An unknown error occurred during installation' , 'jetpack' ); |
||
| 35 | } |
||
| 36 | |||
| 37 | $this->log[ $plugin ][] = $upgrader->skin->get_upgrade_messages(); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ( ! $this->bulk && isset( $error ) ) { |
||
| 41 | |||
| 42 | if ( 'download_failed' === $error_code ) { |
||
|
|
|||
| 43 | // For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed. |
||
| 44 | $error_code = 'no_package'; |
||
| 45 | } |
||
| 46 | |||
| 47 | return new WP_Error( $error_code, $this->log[ $slug ]['error'], 400 ); |
||
| 48 | } |
||
| 49 | |||
| 50 | // replace the slug with the actual plugin id |
||
| 51 | $this->plugins[ $index ] = $plugin; |
||
| 52 | |||
| 53 | return true; |
||
| 54 | } |
||
| 55 | |||
| 56 | protected function validate_plugins() { |
||
| 69 | |||
| 70 | protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) { |
||
| 73 | |||
| 74 | protected static function get_plugin_id_by_slug( $slug ) { |
||
| 75 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 76 | $plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
| 77 | if ( ! is_array( $plugins ) ) { |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | foreach( $plugins as $plugin_file => $plugin_data ) { |
||
| 81 | if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) { |
||
| 82 | return $plugin_file; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | protected static function get_slug_from_file_path( $plugin_file ) { |
||
| 96 | } |
||
| 97 | /** |
||
| 98 | * Allows us to capture that the site doesn't have proper file system access. |
||
| 200 |
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: