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 |
||
| 9 | abstract class Jetpack_JSON_API_Themes_Endpoint extends Jetpack_JSON_API_Endpoint { |
||
| 10 | |||
| 11 | protected $themes = array(); |
||
| 12 | |||
| 13 | protected $bulk = true; |
||
| 14 | protected $log; |
||
| 15 | protected $current_theme_id; |
||
| 16 | |||
| 17 | static $_response_format = array( |
||
|
|
|||
| 18 | 'id' => '(string) The theme\'s ID.', |
||
| 19 | 'screenshot' => '(string) A theme screenshot URL', |
||
| 20 | 'name' => '(string) The name of the theme.', |
||
| 21 | 'theme_uri' => '(string) The URI of the theme\'s webpage.', |
||
| 22 | 'description' => '(string) A description of the theme.', |
||
| 23 | 'author' => '(string) The author of the theme.', |
||
| 24 | 'author_uri' => '(string) The website of the theme author.', |
||
| 25 | 'tags' => '(array) Tags indicating styles and features of the theme.', |
||
| 26 | 'log' => '(array) An array of log strings', |
||
| 27 | 'autoupdate' => '(bool) Whether the theme is automatically updated', |
||
| 28 | 'autoupdate_translation' => '(bool) Whether the theme is automatically updating translations', |
||
| 29 | ); |
||
| 30 | |||
| 31 | protected function result() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Walks through either the submitted theme or list of themes and creates the global array |
||
| 45 | * @param $theme |
||
| 46 | * |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | View Code Duplication | protected function validate_input( $theme ) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Walks through submitted themes to make sure they are valid |
||
| 76 | * @return bool|WP_Error |
||
| 77 | */ |
||
| 78 | protected function validate_themes() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Format a theme for the public API |
||
| 89 | * @param object $theme WP_Theme object |
||
| 90 | * @return array Named array of theme info used by the API |
||
| 91 | */ |
||
| 92 | protected function format_theme( $theme ) { |
||
| 93 | |||
| 94 | if ( ! ( $theme instanceof WP_Theme ) ) { |
||
| 95 | $theme = wp_get_theme( $theme ); |
||
| 96 | } |
||
| 97 | |||
| 98 | $fields = array( |
||
| 99 | 'name' => 'Name', |
||
| 100 | 'theme_uri' => 'ThemeURI', |
||
| 101 | 'description' => 'Description', |
||
| 102 | 'author' => 'Author', |
||
| 103 | 'author_uri' => 'AuthorURI', |
||
| 104 | 'tags' => 'Tags', |
||
| 105 | 'version' => 'Version' |
||
| 106 | ); |
||
| 107 | |||
| 108 | $id = $theme->get_stylesheet(); |
||
| 109 | $formatted_theme = array( |
||
| 110 | 'id' => $id, |
||
| 111 | 'screenshot' => jetpack_photon_url( $theme->get_screenshot(), array(), 'network_path' ), |
||
| 112 | 'active' => $id === $this->current_theme_id, |
||
| 113 | ); |
||
| 114 | |||
| 115 | foreach( $fields as $key => $field ) { |
||
| 116 | $formatted_theme[ $key ] = $theme->get( $field ); |
||
| 117 | } |
||
| 118 | |||
| 119 | $update_themes = get_site_transient( 'update_themes' ); |
||
| 120 | $formatted_theme['update'] = ( isset( $update_themes->response[ $id ] ) ) ? $update_themes->response[ $id ] : null; |
||
| 121 | |||
| 122 | $autoupdate = in_array( $id, Jetpack_Options::get_option( 'autoupdate_themes', array() ) ); |
||
| 123 | $formatted_theme['autoupdate'] = $autoupdate; |
||
| 124 | |||
| 125 | $autoupdate_translation = in_array( $id, Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ) ); |
||
| 126 | $formatted_theme['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
||
| 127 | |||
| 128 | if ( isset( $this->log[ $id ] ) ) { |
||
| 129 | $formatted_theme['log'] = $this->log[ $id ]; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $formatted_theme; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Checks the query_args our collection endpoint was passed to ensure that it's in the proper bounds. |
||
| 137 | * @return bool|WP_Error a WP_Error object if the args are out of bounds, true if things are good. |
||
| 138 | */ |
||
| 139 | protected function check_query_args() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Format a list of themes for public display, using the supplied offset and limit args |
||
| 150 | * @uses WPCOM_JSON_API_Endpoint::query_args() |
||
| 151 | * @return array Public API theme objects |
||
| 152 | */ |
||
| 153 | View Code Duplication | protected function get_themes() { |
|
| 168 | |||
| 169 | } |
||
| 170 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.