Conditions | 10 |
Paths | 8 |
Total Lines | 43 |
Code Lines | 26 |
Lines | 3 |
Ratio | 6.98 % |
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 |
||
52 | protected function validate_themes() { |
||
53 | View Code Duplication | if ( empty( $this->themes ) || ! is_array( $this->themes ) ) { |
|
54 | return new WP_Error( 'missing_themes', __( 'No themes found.', 'jetpack' ) ); |
||
55 | } |
||
56 | foreach( $this->themes as $index => $theme ) { |
||
57 | |||
58 | if ( self::is_installed_theme( $theme ) ) { |
||
59 | return new WP_Error( 'theme_already_installed', __( 'The theme is already installed', 'jetpack' ) ); |
||
60 | } |
||
61 | |||
62 | if ( wp_endswith( $theme, '-wpcom' ) ) { |
||
63 | $file = self::download_wpcom_theme_to_file( $theme ); |
||
64 | if ( is_wp_error( $file ) ) { |
||
65 | return $file; |
||
66 | } |
||
67 | |||
68 | $this->download_links[ $theme ] = $file; |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | $params = (object) array( 'slug' => $theme ); |
||
73 | $url = 'https://api.wordpress.org/themes/info/1.0/'; |
||
74 | $args = array( |
||
75 | 'body' => array( |
||
76 | 'action' => 'theme_information', |
||
77 | 'request' => serialize( $params ), |
||
78 | ) |
||
79 | ); |
||
80 | $response = wp_remote_post( $url, $args ); |
||
81 | $theme_data = unserialize( $response['body'] ); |
||
82 | if ( is_wp_error( $theme_data ) ) { |
||
83 | return $theme_data; |
||
84 | } |
||
85 | |||
86 | if ( ! is_object( $theme_data ) && !isset( $theme_data->download_link ) ) { |
||
87 | return new WP_Error( 'theme_not_found', __( 'This theme does not exist', 'jetpack' ) , 404 ); |
||
88 | } |
||
89 | |||
90 | $this->download_links[ $theme ] = $theme_data->download_link; |
||
91 | |||
92 | } |
||
93 | return true; |
||
94 | } |
||
95 | |||
122 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.