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_Endpoints { |
||
20 | |||
21 | /** |
||
22 | * Initialize REST routes. |
||
23 | */ |
||
24 | public function initialize_rest_api() { |
||
60 | |||
61 | /** |
||
62 | * Handles identity crisis mitigation, confirming safe mode for this site. |
||
63 | * |
||
64 | * @since 4.4.0 |
||
65 | * |
||
66 | * @return bool | WP_Error True if option is properly set. |
||
67 | */ |
||
68 | View Code Duplication | public static function confirm_safe_mode() { |
|
84 | |||
85 | /** |
||
86 | * Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url. |
||
87 | * |
||
88 | * @since 4.4.0 |
||
89 | * |
||
90 | * @return bool | WP_Error True if option is properly set. |
||
91 | */ |
||
92 | View Code Duplication | public static function migrate_stats_and_subscribers() { |
|
114 | |||
115 | /** |
||
116 | * This IDC resolution will disconnect the site and re-connect to a completely new |
||
117 | * and separate shadow site than the original. |
||
118 | * |
||
119 | * It will first will disconnect the site without phoning home as to not disturb the production site. |
||
120 | * It then builds a fresh connection URL and sends it back along with the response. |
||
121 | * |
||
122 | * @since 4.4.0 |
||
123 | * @return bool|WP_Error |
||
124 | */ |
||
125 | public static function start_fresh_connection() { |
||
130 | |||
131 | /** |
||
132 | * Verify that user can mitigate an identity crisis. |
||
133 | * |
||
134 | * @since 4.4.0 |
||
135 | * |
||
136 | * @return bool Whether user has capability 'jetpack_disconnect'. |
||
137 | */ |
||
138 | View Code Duplication | public static function identity_crisis_mitigation_permission_check() { |
|
145 | |||
146 | } |
||
147 |
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.