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 |
||
16 | class Jetpack_XMLRPC_Methods { |
||
17 | |||
18 | /** |
||
19 | * Initialize the main hooks. |
||
20 | */ |
||
21 | public static function init() { |
||
25 | |||
26 | /** |
||
27 | * Adds Jetpack specific methods to the methods added by the Connection package. |
||
28 | * |
||
29 | * @param array $methods Methods added by the Connection package. |
||
30 | */ |
||
31 | public static function xmlrpc_methods( $methods ) { |
||
42 | |||
43 | /** |
||
44 | * Returns what features are available. Uses the slug of the module files. |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | View Code Duplication | public static function features_available() { |
|
57 | |||
58 | /** |
||
59 | * Returns what features are enabled. Uses the slug of the modules files. |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | View Code Duplication | public static function features_enabled() { |
|
72 | |||
73 | /** |
||
74 | * Filters the result of test_connection XMLRPC method |
||
75 | * |
||
76 | * @return string The current Jetpack version number |
||
77 | */ |
||
78 | public static function test_connection() { |
||
81 | |||
82 | /** |
||
83 | * Disconnect this blog from the connected wordpress.com account |
||
84 | * |
||
85 | * @return boolean |
||
86 | */ |
||
87 | public static function disconnect_blog() { |
||
102 | |||
103 | /** |
||
104 | * Returns a purchase token used for site-connected (non user-authenticated) checkout. |
||
105 | * |
||
106 | * @return array The current purchase token |
||
107 | */ |
||
108 | public static function get_purchase_token() { |
||
109 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
110 | if ( ! $blog_id ) { |
||
111 | return new WP_Error( 'site_not_registered', esc_html__( 'Site not registered.', 'jetpack' ) ); |
||
|
|||
112 | } |
||
113 | |||
114 | $purchase_token = Jetpack_Options::get_option( 'purchase_token', false ); |
||
115 | $response = array( |
||
116 | 'purchaseToken' => $purchase_token, |
||
117 | ); |
||
118 | |||
119 | return $response; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Deletes the purchaseToken Jetpack_Option |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public static function delete_purchase_token() { |
||
128 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
129 | if ( ! $blog_id ) { |
||
130 | return new WP_Error( 'site_not_registered', esc_html__( 'Site not registered.', 'jetpack' ) ); |
||
131 | } |
||
132 | |||
133 | return Jetpack_Options::delete_option( 'purchase_token' ); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Serve a JSON API request. |
||
138 | * |
||
139 | * @param array $args request arguments. |
||
140 | */ |
||
141 | public static function json_api( $args = array() ) { |
||
232 | } |
||
233 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.