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 |
||
21 | class Licensing { |
||
22 | /** |
||
23 | * Name of the WordPress option that holds all known Jetpack licenses. |
||
24 | * |
||
25 | * @const string |
||
26 | */ |
||
27 | const LICENSES_OPTION_NAME = 'jetpack_licenses'; |
||
28 | |||
29 | /** |
||
30 | * Name of the WordPress transient that holds the last license attaching error, if any. |
||
31 | * |
||
32 | * @const string |
||
33 | */ |
||
34 | const ERROR_TRANSIENT_NAME = 'jetpack_licenses_error'; |
||
35 | |||
36 | /** |
||
37 | * Holds the singleton instance of this class. |
||
38 | * |
||
39 | * @var self |
||
40 | */ |
||
41 | protected static $instance = false; |
||
42 | |||
43 | /** |
||
44 | * Singleton. |
||
45 | * |
||
46 | * @static |
||
47 | */ |
||
48 | public static function instance() { |
||
55 | |||
56 | /** |
||
57 | * Initialize. |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function initialize() { |
||
65 | |||
66 | /** |
||
67 | * Get Jetpack connection manager instance. |
||
68 | * |
||
69 | * @return Connection_Manager |
||
70 | */ |
||
71 | protected function connection() { |
||
80 | |||
81 | /** |
||
82 | * Get the last license attach request error that has occurred, if any. |
||
83 | * |
||
84 | * @return string Human-readable error message or an empty string. |
||
85 | */ |
||
86 | public function last_error() { |
||
89 | |||
90 | /** |
||
91 | * Log an error to be surfaced to the user at a later time. |
||
92 | * |
||
93 | * @param string $error Human-readable error message. |
||
94 | * @return void |
||
95 | */ |
||
96 | public function log_error( $error ) { |
||
100 | |||
101 | /** |
||
102 | * Get all stored licenses. |
||
103 | * |
||
104 | * @return string[] License keys. |
||
105 | */ |
||
106 | public function stored_licenses() { |
||
114 | |||
115 | /** |
||
116 | * Append a license |
||
117 | * |
||
118 | * @param string $license A jetpack license key. |
||
119 | * @return bool True if the option was updated with the new license, false otherwise. |
||
120 | */ |
||
121 | public function append_license( $license ) { |
||
128 | |||
129 | /** |
||
130 | * Make an authenticated WP.com XMLRPC multicall request to attach the provided license keys. |
||
131 | * |
||
132 | * @param string[] $licenses License keys to attach. |
||
133 | * @return Jetpack_IXR_ClientMulticall |
||
134 | */ |
||
135 | protected function attach_licenses_request( array $licenses ) { |
||
146 | |||
147 | /** |
||
148 | * Attach the given licenses. |
||
149 | * |
||
150 | * @param string[] $licenses Licenses to attach. |
||
151 | * @return array|WP_Error Results for each license (which may include WP_Error instances) or a WP_Error instance. |
||
152 | */ |
||
153 | public function attach_licenses( array $licenses ) { |
||
183 | |||
184 | /** |
||
185 | * Attach all stored licenses. |
||
186 | * |
||
187 | * @return array|WP_Error Results for each license (which may include WP_Error instances) or a WP_Error instance. |
||
188 | */ |
||
189 | public function attach_stored_licenses() { |
||
223 | |||
224 | /** |
||
225 | * Attach all stored licenses during connection flow for the connection owner. |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | public function attach_stored_licenses_on_connection() { |
||
234 | |||
235 | /** |
||
236 | * Is the current user allowed to use the Licensing Input UI? |
||
237 | * |
||
238 | * @since 9.6.0 |
||
239 | * @return bool |
||
240 | */ |
||
241 | public static function is_licensing_input_enabled() { |
||
251 | } |
||
252 |
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.