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 Rest_Api_Endpoints { |
||
17 | |||
18 | /** |
||
19 | * Declare the JITM's REST API endpoints. |
||
20 | */ |
||
21 | public static function register_endpoints() { |
||
44 | |||
45 | /** |
||
46 | * Asks for a jitm, unless they've been disabled, in which case it returns an empty array |
||
47 | * |
||
48 | * @param WP_REST_Request $request The request object. |
||
49 | * |
||
50 | * @return array An array of jitms |
||
51 | */ |
||
52 | public static function get_jitm_message( $request ) { |
||
61 | |||
62 | /** |
||
63 | * Dismisses a jitm. |
||
64 | * |
||
65 | * @param WP_REST_Request $request The request object. |
||
66 | * |
||
67 | * @return bool Always True |
||
68 | */ |
||
69 | public static function delete_jitm_message( $request ) { |
||
78 | |||
79 | /** |
||
80 | * Verify that the user can dismiss JITM messages. |
||
81 | * |
||
82 | * @return bool|WP_Error True if user is able to dismiss JITM messages. |
||
83 | */ |
||
84 | View Code Duplication | public static function delete_jitm_message_permission_callback() { |
|
91 | |||
92 | } |
||
93 |
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.