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 |
||
| 12 | class WPCOM_REST_API_V2_Endpoint_User_Option extends WP_REST_Controller { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Namespace prefix. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $namespace = 'wpcom/v2'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Endpoint base route. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | public $rest_base = 'user-option'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * WPCOM_REST_API_V2_Endpoint_User_Option constructor. |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Register routes. |
||
| 37 | */ |
||
| 38 | public function register_routes() { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Checks if a given request has access to admin menus. |
||
| 65 | * |
||
| 66 | * @param WP_REST_Request $request Full details about the request. |
||
| 67 | * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function get_option_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Retrieves the admin menu. |
||
| 87 | * |
||
| 88 | * @param WP_REST_Request $request Full details about the request. |
||
| 89 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
||
| 90 | */ |
||
| 91 | public function get_option( $request ) { |
||
| 115 | } |
||
| 116 | |||
| 118 |
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.