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 |
||
| 3 | class Jetpack_JSON_API_Themes_Modify_Endpoint extends Jetpack_JSON_API_Themes_Endpoint { |
||
| 4 | // POST /sites/%s/themes/%s |
||
| 5 | // POST /sites/%s/themes |
||
| 6 | |||
| 7 | protected $needed_capabilities = 'update_themes'; |
||
| 8 | protected $action = 'default_action'; |
||
| 9 | protected $expected_actions = array( 'update', 'update_translations' ); |
||
| 10 | |||
| 11 | View Code Duplication | public function default_action() { |
|
| 30 | |||
| 31 | function autoupdate_on() { |
||
| 36 | |||
| 37 | function autoupdate_off() { |
||
| 42 | |||
| 43 | function autoupdate_translations_on() { |
||
| 48 | |||
| 49 | function autoupdate_translations_off() { |
||
| 54 | |||
| 55 | function update() { |
||
| 56 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 57 | |||
| 58 | // Clear the cache. |
||
| 59 | wp_update_themes(); |
||
| 60 | |||
| 61 | foreach ( $this->themes as $theme ) { |
||
| 62 | /** |
||
| 63 | * Pre-upgrade action |
||
| 64 | * |
||
| 65 | * @since 3.9.3 |
||
| 66 | * |
||
| 67 | * @param object $theme WP_Theme object |
||
| 68 | * @param array $themes Array of theme objects |
||
| 69 | */ |
||
| 70 | do_action('jetpack_pre_theme_upgrade', $theme, $this->themes); |
||
| 71 | // Objects created inside the for loop to clean the messages for each theme |
||
| 72 | $skin = new Automatic_Upgrader_Skin(); |
||
| 73 | $upgrader = new Theme_Upgrader( $skin ); |
||
| 74 | $upgrader->init(); |
||
| 75 | $result = $upgrader->upgrade( $theme ); |
||
| 76 | $this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages(); |
||
| 77 | } |
||
| 78 | |||
| 79 | View Code Duplication | if ( ! $this->bulk && ! $result ) { |
|
|
|
|||
| 80 | return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 ); |
||
| 81 | } |
||
| 82 | |||
| 83 | return true; |
||
| 84 | } |
||
| 85 | |||
| 86 | function update_translations() { |
||
| 131 | |||
| 132 | } |
||
| 133 |
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: