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 | * metaboxes |
||
20 | * Easy access for the Metaboxes class. |
||
21 | * |
||
22 | * @var string |
||
23 | * @access private |
||
24 | */ |
||
25 | private $metaboxes; |
||
26 | |||
27 | protected $type = 'post'; |
||
28 | |||
29 | /** |
||
30 | * Constructor to load in the Metaboxes class. |
||
31 | * |
||
32 | * @see Metaboxes |
||
33 | */ |
||
34 | public function __construct(){ |
||
39 | |||
40 | /** |
||
41 | * Create test data posts. |
||
42 | * |
||
43 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
44 | * a number of posts to create. We then fetch the supports & metaboxes |
||
45 | * for that cpt and feed them into a function to create each post individually. |
||
46 | * |
||
47 | * @access private |
||
48 | * |
||
49 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
50 | * |
||
51 | * @param string $slug a custom post type ID. |
||
52 | * @param boolean $connection Whether or not we're connected to the Internet. |
||
53 | * @param boolean $echo Whether or not to echo. Optional. |
||
54 | * @param int $num Optional. Number of posts to create. |
||
55 | */ |
||
56 | public function create_objects( $slug, $connection, $echo = false, $num = '' ){ |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Creates the individual test data post. |
||
91 | * |
||
92 | * Create individual posts for testing with. Gathers basic information such |
||
93 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
94 | * adds metaboxes if applicable . |
||
95 | * |
||
96 | * @access private |
||
97 | * |
||
98 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
||
99 | * |
||
100 | * @param string $slug a custom post type ID. |
||
101 | * @param array $supports Features that the post type supports. |
||
102 | * @param array $supports All CMB2 metaboxes attached to the post type. |
||
103 | */ |
||
104 | private function create_test_object( $slug, $supports, $metaboxes ){ |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Assemble supports statements for a particular post type. |
||
179 | * |
||
180 | * @access private |
||
181 | * |
||
182 | * @see post_type_supports |
||
183 | * |
||
184 | * @param string $slug a custom post type ID. |
||
185 | * @return array Array of necessary supports booleans. |
||
186 | */ |
||
187 | private function get_cpt_supports( $slug ){ |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Assigns taxonomies to the new post. |
||
203 | * |
||
204 | * Loop through every taxonomy type associated with a custom post type & |
||
205 | * assign the post to a random item out of each taxonomy. Taxonomies must |
||
206 | * have at least one term in them for this to work. |
||
207 | * |
||
208 | * @access private |
||
209 | * |
||
210 | * @param int $post_id a custom post type ID. |
||
211 | * @param array $taxonomies taxonomies assigned to this cpt. |
||
212 | * @return object WP Error if there is one. |
||
213 | */ |
||
214 | private function assign_terms( $post_id, $taxonomies ){ |
||
255 | |||
256 | /** |
||
257 | * Delete test data posts. |
||
258 | * |
||
259 | * This function will search for all posts of a particular post type ($slug) |
||
260 | * and delete them all using a particular cmb flag that we set when creating |
||
261 | * the posts. Validates the user first. |
||
262 | * |
||
263 | * @see WP_Query, wp_delete_post |
||
264 | * |
||
265 | * @param string $slug a custom post type ID. |
||
266 | * @param boolean $echo Whether or not to echo the result |
||
267 | */ |
||
268 | public function delete( $slug, $echo = false ){ |
||
332 | |||
333 | |||
334 | /** |
||
335 | * Find and delete attachments associated with a post ID. |
||
336 | * |
||
337 | * This function finds each attachment that is associated with a post ID |
||
338 | * and deletes it completely from the site. This is to prevent leftover |
||
339 | * random images from sitting on the site forever. |
||
340 | * |
||
341 | * @access private |
||
342 | * |
||
343 | * @see get_attached_media, wp_delete_attachment |
||
344 | * |
||
345 | * @param int $pid a custom post type ID. |
||
346 | */ |
||
347 | private function delete_associated_media( $pid ){ |
||
374 | |||
375 | |||
376 | } |
||
377 |
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..