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 |
||
15 | class Term extends Abs\Type { |
||
16 | |||
17 | /** |
||
18 | * type |
||
19 | * Defines type slug for use elsewhere in the plugin |
||
20 | * |
||
21 | * @var string |
||
22 | * @access protected |
||
23 | */ |
||
24 | protected $type = 'term'; |
||
25 | |||
26 | /** |
||
27 | * Create test data posts. |
||
28 | * |
||
29 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
30 | * a number of posts to create. We then fetch the supports & metaboxes |
||
31 | * for that cpt and feed them into a function to create each post individually. |
||
32 | * |
||
33 | * @access private |
||
34 | * |
||
35 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
36 | * |
||
37 | * @param string $slug a custom post type ID. |
||
38 | * @param int $num Optional. Number of posts to create. |
||
|
|||
39 | */ |
||
40 | View Code Duplication | public function create_objects( $slug, $connection, $num = '' ) { |
|
65 | |||
66 | |||
67 | /** |
||
68 | * Creates the individual test data object. |
||
69 | * |
||
70 | * Create individual posts for testing with. Gathers basic information such |
||
71 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
72 | * adds metaboxes if applicable . |
||
73 | * |
||
74 | * @access private |
||
75 | * |
||
76 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
||
77 | * |
||
78 | * @param string $slug a custom post type ID. |
||
79 | */ |
||
80 | private function create_test_object( $slug ) { |
||
114 | |||
115 | |||
116 | |||
117 | /** |
||
118 | * Delete all test data, regardless of type, within terms. |
||
119 | * |
||
120 | * @see Delete |
||
121 | */ |
||
122 | public function delete_all() { |
||
140 | |||
141 | |||
142 | /** |
||
143 | * Delete test data terms. |
||
144 | * |
||
145 | * This function will search for all terms of a particular taxonomy ($slug) |
||
146 | * and delete them all using a particular term_meta flag that we set when creating |
||
147 | * the posts. Validates the user first. |
||
148 | * |
||
149 | * @see WP_Query, wp_delete_post |
||
150 | * |
||
151 | * @param string $slug a custom post type ID. |
||
152 | */ |
||
153 | public function delete( $slug ) { |
||
222 | |||
223 | } |
||
224 |
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.