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 |
||
18 | class Jetpack_Publicize_Gutenberg { |
||
19 | |||
20 | /** |
||
21 | * Instance of Publicize used to access data gathering utility methods. |
||
22 | * |
||
23 | * @since 5.9.1 |
||
24 | * @var Publicize $publicize Instance of Jetpack Publicize class. |
||
25 | */ |
||
26 | private $publicize; |
||
27 | |||
28 | /** |
||
29 | * Constructor for Jetpack_Publicize_Gutenberg |
||
30 | * |
||
31 | * Set up hooks to extend legacy Publicize behavior. |
||
32 | * |
||
33 | * @since 5.9.1 |
||
34 | */ |
||
35 | public function __construct( $publicize ) { |
||
48 | |||
49 | /** |
||
50 | * Retrieve current list of connected social accounts. |
||
51 | * |
||
52 | * Gets current list of connected accounts and send them as |
||
53 | * JSON encoded data. |
||
54 | * |
||
55 | * @see Publicize_Base::get_publicize_conns_test_results() |
||
56 | * |
||
57 | * @since 5.9.1 |
||
58 | * |
||
59 | * @param WP_REST_Request $request Request instance from REST call. |
||
|
|||
60 | * |
||
61 | * @return string JSON encoded connection list data. |
||
62 | */ |
||
63 | public function rest_get_publicize_connections() { |
||
66 | |||
67 | /** |
||
68 | * Retrieve current list of connected social accounts for a given post. |
||
69 | * |
||
70 | * Gets current list of connected accounts and send them as |
||
71 | * JSON encoded data. |
||
72 | * |
||
73 | * @see Publicize::get_filtered_connection_data() |
||
74 | * |
||
75 | * @since 5.9.1 |
||
76 | * |
||
77 | * @param WP_REST_Request $request Request instance from REST call. |
||
78 | * |
||
79 | * @return string JSON encoded connection list data. |
||
80 | */ |
||
81 | public function rest_get_publicize_connections_for_post( $request ) { |
||
85 | |||
86 | /** |
||
87 | * Retrieve full list of available Publicize connection services |
||
88 | * send them as JSON encoded data. |
||
89 | * |
||
90 | * @see Publicize::get_available_service_data() |
||
91 | * |
||
92 | * @since 6.7.0 |
||
93 | * |
||
94 | * @return string JSON encoded connection services data. |
||
95 | */ |
||
96 | public function rest_get_publicize_available_services() { |
||
105 | |||
106 | /** |
||
107 | * Add rest field to 'post' for Publicize support |
||
108 | * |
||
109 | * Sets up 'publicize' schema to submit publicize sharing title |
||
110 | * and individual connection sharing enables/disables. This schema |
||
111 | * is registered with the 'post' endpoint REST endpoint so publicize |
||
112 | * options can be saved when a post is published. |
||
113 | * |
||
114 | * @since 5.9.1 |
||
115 | */ |
||
116 | public function add_publicize_rest_fields() { |
||
201 | |||
202 | /** |
||
203 | * Check user capability for getting Publicize connection list from endpoint. |
||
204 | * |
||
205 | * @since 5.9.1 |
||
206 | * |
||
207 | * @return boolean True if current user has 'publish_post' capability. |
||
208 | */ |
||
209 | public function rest_connections_permission_callback() { |
||
212 | |||
213 | /** |
||
214 | * Check post id validity for Publicize connection list REST endpoint. |
||
215 | * |
||
216 | * @since 5.9.1 |
||
217 | * |
||
218 | * @param mixed $param post_id parameter from REST call. |
||
219 | * |
||
220 | * @return boolean True if post_id is valid integer |
||
221 | */ |
||
222 | public function rest_connections_validate_post_id( $param ) { |
||
225 | |||
226 | /** |
||
227 | * Set up Publicize meta fields for publishing post. |
||
228 | * |
||
229 | * Process 'publicize' REST field to setup Publicize for publishing |
||
230 | * post. Sets post meta keys to enable/disable each connection for |
||
231 | * the post and sets publicize title meta key if a title message |
||
232 | * is provided. |
||
233 | * |
||
234 | * @since 5.9.1 |
||
235 | * |
||
236 | * @param stdClass $new_post_obj Updated post object about to be inserted view REST endpoint. |
||
237 | * @param WP_REST_Request $request Request object, possibly containing 'publicize' field {@see add_publicize_rest_fields()}. |
||
238 | * |
||
239 | * @return WP_Post Returns the original $new_post value unchanged. |
||
240 | */ |
||
241 | public function process_publicize_from_rest( $new_post_obj, $request ) { |
||
284 | |||
285 | /** |
||
286 | * Checks if a connection should be shared to. |
||
287 | * |
||
288 | * Checks $connection_id against $connections_array to see if the connection associated |
||
289 | * with $connection_id should be shared to. Will return true if $connection_id is in the |
||
290 | * array and 'should_share' property is set to true, and will default to false otherwise. |
||
291 | * |
||
292 | * @since 5.9.1 |
||
293 | * |
||
294 | * @param array $connections_array 'connections' from 'publicize' REST field {@see add_publicize_rest_fields()}. |
||
295 | * @param string $connection_id Connection identifier string that is unique for each connection. |
||
296 | * @return boolean True if connection should be shared to, false otherwise. |
||
297 | */ |
||
298 | private function connection_should_share( $connections_array, $connection_id ) { |
||
308 | |||
309 | /** |
||
310 | * Enqueue scripts when they are needed for the edit page |
||
311 | * |
||
312 | * Enqueues necessary scripts for edit page for Gutenberg |
||
313 | * editor only. |
||
314 | * |
||
315 | * @since 5.9.1 |
||
316 | * |
||
317 | * @param string $hook Current page url. |
||
318 | */ |
||
319 | public function post_page_enqueue( $hook ) { |
||
333 | } |
||
334 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.