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 | abstract class Jetpack_JSON_API_Modules_Endpoint extends Jetpack_JSON_API_Endpoint { |
||
7 | |||
8 | protected $modules = array(); |
||
9 | |||
10 | protected $bulk = true; |
||
11 | |||
12 | static $_response_format = array( |
||
|
|||
13 | 'id' => '(string) The module\'s ID', |
||
14 | 'active' => '(boolean) The module\'s status.', |
||
15 | 'name' => '(string) The module\'s name.', |
||
16 | 'description' => '(safehtml) The module\'s description.', |
||
17 | 'sort' => '(int) The module\'s display order.', |
||
18 | 'introduced' => '(string) The Jetpack version when the module was introduced.', |
||
19 | 'changed' => '(string) The Jetpack version when the module was changed.', |
||
20 | 'free' => '(boolean) The module\'s Free or Paid status.', |
||
21 | 'module_tags' => '(array) The module\'s tags.' |
||
22 | ); |
||
23 | |||
24 | protected function result() { |
||
25 | |||
26 | $modules = $this->get_modules(); |
||
27 | |||
28 | if ( ! $this->bulk && ! empty( $modules ) ) { |
||
29 | return array_pop( $modules ); |
||
30 | } |
||
31 | |||
32 | return array( 'modules' => $modules ); |
||
33 | |||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Walks through either the submitted modules or list of themes and creates the global array |
||
38 | * @param $theme |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | View Code Duplication | protected function validate_input( $module) { |
|
43 | $args = $this->input(); |
||
44 | // lets set what modules were requested, and validate them |
||
45 | if ( ! isset( $module ) || empty( $module ) ) { |
||
46 | |||
47 | if ( ! $args['modules'] || empty( $args['modules'] ) ) { |
||
48 | return new WP_Error( 'missing_module', __( 'You are required to specify a module.', 'jetpack' ), 400 ); |
||
49 | } |
||
50 | if ( is_array( $args['modules'] ) ) { |
||
51 | $this->modules = $args['modules']; |
||
52 | } else { |
||
53 | $this->modules[] = $args['modules']; |
||
54 | } |
||
55 | } else { |
||
56 | $this->modules[] = urldecode( $module ); |
||
57 | $this->bulk = false; |
||
58 | } |
||
59 | |||
60 | if ( is_wp_error( $error = $this->validate_modules() ) ) { |
||
61 | return $error; |
||
62 | } |
||
63 | |||
64 | return parent::validate_input( $module ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Walks through submitted themes to make sure they are valid |
||
69 | * @return bool|WP_Error |
||
70 | */ |
||
71 | protected function validate_modules() { |
||
72 | foreach ( $this->modules as $module ) { |
||
73 | if ( ! Jetpack::is_module( $module ) ) { |
||
74 | return new WP_Error( 'unknown_jetpack_module', sprintf( __( 'Module not found: `%s`.', 'jetpack' ), $module ), 404 ); |
||
75 | } |
||
76 | } |
||
77 | return true; |
||
78 | } |
||
79 | |||
80 | protected static function format_module( $module_slug ) { |
||
102 | |||
103 | /** |
||
104 | * Format a list of modules for public display, using the supplied offset and limit args |
||
105 | * @uses WPCOM_JSON_API_Endpoint::query_args() |
||
106 | * @return array Public API modules objects |
||
107 | */ |
||
108 | View Code Duplication | protected function get_modules() { |
|
120 | |||
121 | } |
||
122 |
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.