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 User extends Abs\Type { |
||
17 | |||
18 | /** |
||
19 | * type |
||
20 | * Defines type slug for use elsewhere in the plugin |
||
21 | * |
||
22 | * @var string |
||
23 | * @access protected |
||
24 | */ |
||
25 | protected $type = 'user'; |
||
26 | |||
27 | /** |
||
28 | * Create test data posts. |
||
29 | * |
||
30 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
31 | * a number of posts to create. We then fetch the supports & metaboxes |
||
32 | * for that cpt and feed them into a function to create each post individually. |
||
33 | * |
||
34 | * @access private |
||
35 | * |
||
36 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
37 | * |
||
38 | * @param string $slug a custom post type ID. |
||
39 | * @param boolean $connection Whether or not we're connected to the Internet. |
||
40 | * @param int $num Optional. Number of posts to create. |
||
|
|||
41 | */ |
||
42 | View Code Duplication | public function create_objects( $slug, $connection, $num = '' ) { |
|
67 | |||
68 | |||
69 | /** |
||
70 | * Creates the individual test data user. |
||
71 | * |
||
72 | * Create individual posts for testing with. Gathers basic information such |
||
73 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
74 | * adds metaboxes if applicable . |
||
75 | * |
||
76 | * @access private |
||
77 | * |
||
78 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
||
79 | * |
||
80 | * @param string $slug a custom post type ID. |
||
81 | */ |
||
82 | private function create_test_object( $slug ) { |
||
83 | |||
84 | if ( ! is_user_logged_in() ) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | $name = apply_filters( "tc_{$slug}_user_name", TestContent::name() ); |
||
89 | |||
90 | // First, insert our post |
||
91 | $userdata = array( |
||
92 | 'user_pass' => wp_generate_password( 12, true, true ), |
||
93 | 'user_login' => strtolower( $name['first'] . $name['last'] ) . rand( 10, 100 ), |
||
94 | 'user_email' => apply_filters( "tc_{$slug}_user_email", TestContent::email( true ) ), |
||
95 | 'display_name' => strtolower( $name['first'] . $name['last'] ), |
||
96 | 'first_name' => $name['first'], |
||
97 | 'last_name' => $name['last'], |
||
98 | 'description' => TestContent::title(), |
||
99 | 'user_registered' => date( 'Y-m-d H:i:s' ), |
||
100 | 'role' => $slug, |
||
101 | ); |
||
102 | |||
103 | // Insert the user |
||
104 | $user_id = wp_insert_user( apply_filters( "tc_{$slug}_user_arguments", $userdata ) ); |
||
105 | |||
106 | // Then, set a test content flag on the new post for later deletion |
||
107 | add_user_meta( $user_id, 'dummypress_test_data', '__test__', true ); |
||
108 | |||
109 | // Check if we have errors and return them or created message |
||
110 | View Code Duplication | if ( is_wp_error( $user_id ) ) { |
|
111 | error_log( $user_id->get_error_message() ); |
||
112 | return $user_id; |
||
113 | } else { |
||
114 | return array( |
||
115 | 'action' => 'created', |
||
116 | 'object' => 'user', |
||
117 | 'oid' => $user_id, |
||
118 | 'type' => $slug, |
||
119 | 'link_edit' => admin_url( '/user-edit.php?user_id=' . $user_id ), |
||
120 | 'link_view' => get_author_posts_url( $user_id ) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | } |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Get all roles and set a cleaner array. |
||
129 | * |
||
130 | * @see get_editable_roles |
||
131 | * |
||
132 | * @global object $wp_roles WP Roles obbject |
||
133 | * |
||
134 | * @return array Array of roles for use in creation and deletion |
||
135 | */ |
||
136 | public function get_roles() { |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Delete all test data, regardless of type, within posts. |
||
170 | * |
||
171 | * @see Delete |
||
172 | */ |
||
173 | public function delete_all() { |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Delete test data users. |
||
202 | * |
||
203 | * This function will search for all posts of a particular post type ($slug) |
||
204 | * and delete them all using a particular cmb flag that we set when creating |
||
205 | * the posts. Validates the user first. |
||
206 | * |
||
207 | * @see WP_Query, wp_delete_post |
||
208 | * |
||
209 | * @param string $slug a custom post type ID. |
||
210 | */ |
||
211 | public function delete( $slug ) { |
||
285 | |||
286 | } |
||
287 |
This check looks for
@param
annotations 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.