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 |
||
11 | class CreatePost{ |
||
12 | |||
13 | /** |
||
14 | * metaboxes |
||
15 | * Easy access for the Metaboxes class. |
||
16 | * |
||
17 | * @var string |
||
18 | * @access private |
||
19 | */ |
||
20 | private $metaboxes; |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Constructor to load in the Metaboxes class. |
||
25 | * |
||
26 | * @see Metaboxes |
||
27 | */ |
||
28 | public function __construct(){ |
||
33 | |||
34 | /** |
||
35 | * Create test data posts. |
||
36 | * |
||
37 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
38 | * a number of posts to create. We then fetch the supports & metaboxes |
||
39 | * for that cpt and feed them into a function to create each post individually. |
||
40 | * |
||
41 | * @access private |
||
42 | * |
||
43 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
44 | * |
||
45 | * @param string $slug a custom post type ID. |
||
46 | * @param boolean $echo Whether or not to echo. Optional. |
||
47 | * @param int $num Optional. Number of posts to create. |
||
48 | */ |
||
49 | public function create_post_type_content( $slug, $echo = false, $num = '' ){ |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Creates the individual test data post. |
||
81 | * |
||
82 | * Create individual posts for testing with. Gathers basic information such |
||
83 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
84 | * adds metaboxes if applicable . |
||
85 | * |
||
86 | * @access private |
||
87 | * |
||
88 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
||
89 | * |
||
90 | * @param string $slug a custom post type ID. |
||
91 | * @param array $supports Features that the post type supports. |
||
92 | * @param array $supports All CMB2 metaboxes attached to the post type. |
||
93 | */ |
||
94 | private function create_test_object( $slug, $supports, $metaboxes ){ |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Assemble supports statements for a particular post type. |
||
164 | * |
||
165 | * @access private |
||
166 | * |
||
167 | * @see post_type_supports |
||
168 | * |
||
169 | * @param string $slug a custom post type ID. |
||
170 | * @return array Array of necessary supports booleans. |
||
171 | */ |
||
172 | private function get_cpt_supports( $slug ){ |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Assigns taxonomies to the new post. |
||
187 | * |
||
188 | * Loop through every taxonomy type associated with a custom post type & |
||
189 | * assign the post to a random item out of each taxonomy. Taxonomies must |
||
190 | * have at least one term in them for this to work. |
||
191 | * |
||
192 | * @access private |
||
193 | * |
||
194 | * @param int $post_id a custom post type ID. |
||
195 | * @param array $taxonomies taxonomies assigned to this cpt. |
||
196 | * @return object WP Error if there is one. |
||
197 | */ |
||
198 | private function assign_terms( $post_id, $taxonomies ){ |
||
239 | |||
240 | } |
||
241 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..