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 Post extends Abs\Type { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * metabox_types |
||
| 20 | * Easy access for the MetaboxTypes class. |
||
| 21 | * |
||
| 22 | * @var object |
||
| 23 | * @access private |
||
| 24 | */ |
||
| 25 | private $metabox_types; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * metabox_values |
||
| 29 | * Easy access for the MetaboxValues class. |
||
| 30 | * |
||
| 31 | * @var object |
||
| 32 | * @access private |
||
| 33 | */ |
||
| 34 | private $metabox_values; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * type |
||
| 38 | * Defines type slug for use elsewhere in the plugin |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * @access protected |
||
| 42 | */ |
||
| 43 | protected $type = 'post'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor to load in the Metaboxes class. |
||
| 47 | * |
||
| 48 | * @see MetaboxTypes, MetaboxValues |
||
| 49 | */ |
||
| 50 | public function __construct() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create test data posts. |
||
| 59 | * |
||
| 60 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
| 61 | * a number of posts to create. We then fetch the supports & metaboxes |
||
| 62 | * for that cpt and feed them into a function to create each post individually. |
||
| 63 | * |
||
| 64 | * @access private |
||
| 65 | * |
||
| 66 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
| 67 | * |
||
| 68 | * @param string $slug a custom post type ID. |
||
| 69 | * @param boolean $connection Whether or not we're connected to the Internet. |
||
| 70 | * @param int $num Optional. Number of posts to create. |
||
|
|
|||
| 71 | */ |
||
| 72 | public function create_objects( $slug, $connection, $num = '' ) { |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates the individual test data post. |
||
| 105 | * |
||
| 106 | * Create individual posts for testing with. Gathers basic information such |
||
| 107 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
| 108 | * adds metaboxes if applicable . |
||
| 109 | * |
||
| 110 | * @access private |
||
| 111 | * |
||
| 112 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->get_values |
||
| 113 | * |
||
| 114 | * @param string $slug a custom post type ID. |
||
| 115 | * @param array $supports Features that the post type supports. |
||
| 116 | * @param array $supports All CMB2 metaboxes attached to the post type. |
||
| 117 | */ |
||
| 118 | private function create_test_object( $slug, $supports, $metaboxes ) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Assemble supports statements for a particular post type. |
||
| 193 | * |
||
| 194 | * @access private |
||
| 195 | * |
||
| 196 | * @see post_type_supports |
||
| 197 | * |
||
| 198 | * @param string $slug a custom post type ID. |
||
| 199 | * @return array Array of necessary supports booleans. |
||
| 200 | */ |
||
| 201 | private function get_cpt_supports( $slug ) { |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Assigns taxonomies to the new post. |
||
| 217 | * |
||
| 218 | * Loop through every taxonomy type associated with a custom post type & |
||
| 219 | * assign the post to a random item out of each taxonomy. Taxonomies must |
||
| 220 | * have at least one term in them for this to work. |
||
| 221 | * |
||
| 222 | * @access private |
||
| 223 | * |
||
| 224 | * @param int $post_id a custom post type ID. |
||
| 225 | * @param array $taxonomies taxonomies assigned to this cpt. |
||
| 226 | * @return object WP Error if there is one. |
||
| 227 | */ |
||
| 228 | private function assign_terms( $post_id, $taxonomies ) { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Delete all test data, regardless of type, within posts. |
||
| 273 | * |
||
| 274 | * @see Delete |
||
| 275 | */ |
||
| 276 | public function delete_all() { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Delete test data posts. |
||
| 298 | * |
||
| 299 | * This function will search for all posts of a particular post type ($slug) |
||
| 300 | * and delete them all using a particular cmb flag that we set when creating |
||
| 301 | * the posts. Validates the user first. |
||
| 302 | * |
||
| 303 | * @see WP_Query, wp_delete_post |
||
| 304 | * |
||
| 305 | * @param string $slug a custom post type ID. |
||
| 306 | */ |
||
| 307 | public function delete( $slug ) { |
||
| 308 | |||
| 309 | $delete = new Delete; |
||
| 310 | |||
| 311 | // Make sure that the current user is logged in & has full permissions. |
||
| 312 | if ( ! $delete->user_can_delete() ) { |
||
| 313 | return; |
||
| 314 | } |
||
| 315 | |||
| 316 | // Check that $cptslg has a string. |
||
| 317 | if ( empty( $slug ) ) { |
||
| 318 | return; |
||
| 319 | } |
||
| 320 | |||
| 321 | // Find our test data by the unique flag we set when we created the data |
||
| 322 | $query = array( |
||
| 323 | 'post_type' => $slug, |
||
| 324 | 'posts_per_page' => 500, |
||
| 325 | 'meta_query' => array( |
||
| 326 | 'relation' => 'OR', |
||
| 327 | array( |
||
| 328 | 'key' => 'dummypress_test_data', |
||
| 329 | 'value' => '__test__', |
||
| 330 | 'compare' => '=' |
||
| 331 | ), |
||
| 332 | array( |
||
| 333 | 'key' => 'evans_test_content', |
||
| 334 | 'value' => '__test__', |
||
| 335 | 'compare' => '=' |
||
| 336 | ), |
||
| 337 | ), |
||
| 338 | ); |
||
| 339 | |||
| 340 | $objects = new \WP_Query( $query ); |
||
| 341 | |||
| 342 | if ( $objects->have_posts() ) { |
||
| 343 | |||
| 344 | $events = array(); |
||
| 345 | |||
| 346 | while ( $objects->have_posts() ) : $objects->the_post(); |
||
| 347 | |||
| 348 | // Find any media associated with the test post and delete it as well |
||
| 349 | $this->delete_associated_media( get_the_id() ); |
||
| 350 | |||
| 351 | // Double check our set user meta value |
||
| 352 | if ( '__test__' != get_post_meta( get_the_id(), 'dummypress_test_data', true ) && '__test__' != get_post_meta( get_the_id(), 'evans_test_content', true ) ) { |
||
| 353 | continue; |
||
| 354 | } |
||
| 355 | |||
| 356 | $events[] = array( |
||
| 357 | 'action' => 'deleted', |
||
| 358 | 'oid' => get_the_id(), |
||
| 359 | 'type' => get_post_type( get_the_id() ), |
||
| 360 | 'link' => '' |
||
| 361 | ); |
||
| 362 | |||
| 363 | // Force delete the post |
||
| 364 | wp_delete_post( get_the_id(), true ); |
||
| 365 | |||
| 366 | endwhile; |
||
| 367 | |||
| 368 | $obj = get_post_type_object( $slug ); |
||
| 369 | |||
| 370 | $events[] = array( |
||
| 371 | 'action' => 'general', |
||
| 372 | 'message' => __( 'Deleted', 'dummybot' ) . ' ' . $obj->labels->all_items |
||
| 373 | ); |
||
| 374 | |||
| 375 | return $events; |
||
| 376 | |||
| 377 | } |
||
| 378 | |||
| 379 | } |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Find and delete attachments associated with a post ID. |
||
| 384 | * |
||
| 385 | * This function finds each attachment that is associated with a post ID |
||
| 386 | * and deletes it completely from the site. This is to prevent leftover |
||
| 387 | * random images from sitting on the site forever. |
||
| 388 | * |
||
| 389 | * @access private |
||
| 390 | * |
||
| 391 | * @see get_attached_media, wp_delete_attachment |
||
| 392 | * |
||
| 393 | * @param int $pid a custom post type ID. |
||
| 394 | */ |
||
| 395 | private function delete_associated_media( $pid ) { |
||
| 422 | |||
| 423 | |||
| 424 | } |
||
| 425 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.