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 |
||
19 | class REST_Controller { |
||
20 | /** |
||
21 | * Registers the REST routes for Backup. |
||
22 | * |
||
23 | * @access public |
||
24 | * @static |
||
25 | */ |
||
26 | public static function register_rest_routes() { |
||
63 | |||
64 | /** |
||
65 | * The Backup Helper Script should only be installed / removed via site-level authentication. |
||
66 | * This means that the corresponding endpoints can only be accessible from WPCOM. |
||
67 | * |
||
68 | * @access public |
||
69 | * @static |
||
70 | * |
||
71 | * @return bool|WP_Error True if a blog token was used to sign the request, WP_Error otherwise. |
||
72 | */ |
||
73 | View Code Duplication | public static function backup_helper_script_permissions_callback() { |
|
85 | |||
86 | /** |
||
87 | * Install the Backup Helper Script. |
||
88 | * |
||
89 | * @access public |
||
90 | * @static |
||
91 | * |
||
92 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
93 | * @return array|WP_Error Returns the result of Helper Script installation. Returns one of: |
||
94 | * - WP_Error on failure, or |
||
95 | * - An array with installation info on success: |
||
96 | * 'path' (string) The sinstallation path. |
||
97 | * 'url' (string) The access url. |
||
98 | * 'abspath' (string) The abspath. |
||
99 | */ |
||
100 | public static function install_backup_helper_script( $request ) { |
||
119 | |||
120 | /** |
||
121 | * Delete a Backup Helper Script. |
||
122 | * |
||
123 | * @access public |
||
124 | * @static |
||
125 | * |
||
126 | * @param WP_REST_Request $request The request sent to the WP REST API. |
||
127 | * @return array An array with 'success' key indicating the result of the delete operation. |
||
128 | */ |
||
129 | public static function delete_backup_helper_script( $request ) { |
||
141 | } |
||
142 |
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.