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 |
||
12 | class LSX_Schema_Utils { |
||
13 | /** |
||
14 | * Determines whether a given post type should have Review schema. |
||
15 | * |
||
16 | * @param string $post_type Post type to check. |
||
17 | * @param string $comparison_type Post type to check against. |
||
18 | * |
||
19 | * @return bool True if it has schema, false if not. |
||
20 | */ |
||
21 | public static function is_type( $post_type = null, $comparison_type = null ) { |
||
36 | /** |
||
37 | * Retrieve a users Schema ID. |
||
38 | * |
||
39 | * @param string $place_id The Name of the Reviewer you need a for. |
||
40 | * @param string $type the type of the place. |
||
41 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
42 | * |
||
43 | * @return string The user's schema ID. |
||
44 | */ |
||
45 | public static function get_places_schema_id( $place_id, $type, $context ) { |
||
49 | /** |
||
50 | * Retrieve a users Schema ID. |
||
51 | * |
||
52 | * @param string $name The Name of the Reviewer you need a for. |
||
53 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
54 | * |
||
55 | * @return string The user's schema ID. |
||
56 | */ |
||
57 | public static function get_subtrip_schema_id( $name, $context ) { |
||
61 | /** |
||
62 | * Retrieve an offer Schema ID. |
||
63 | * |
||
64 | * @param string $id post ID of the place being added. |
||
65 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
66 | * @param string $local if the Schema is local true / false. |
||
67 | * |
||
68 | * @return string The user's schema ID. |
||
69 | */ |
||
70 | View Code Duplication | public static function get_offer_schema_id( $id, $context, $local = false ) { |
|
80 | /** |
||
81 | * Retrieve an review Schema ID. |
||
82 | * |
||
83 | * @param string $id post ID of the place being added. |
||
84 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
85 | * @param string $local if the Schema is local true / false. |
||
86 | * |
||
87 | * @return string The user's schema ID. |
||
88 | */ |
||
89 | View Code Duplication | public static function get_review_schema_id( $id, $context, $local = false ) { |
|
99 | /** |
||
100 | * Retrieve an Article Schema ID. |
||
101 | * |
||
102 | * @param string $id post ID of the place being added. |
||
103 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
104 | * @param string $local if the Schema is local true / false. |
||
105 | * |
||
106 | * @return string The user's schema ID. |
||
107 | */ |
||
108 | public static function get_article_schema_id( $id, $context, $local = false ) { |
||
116 | /** |
||
117 | * Retrieve a users Schema ID. |
||
118 | * |
||
119 | * @param string $name The Name of the Reviewer you need a for. |
||
120 | * @param WPSEO_Schema_Context $context A value object with context variables. |
||
121 | * |
||
122 | * @return string The user's schema ID. |
||
123 | */ |
||
124 | public static function get_author_schema_id( $name, $email, $context ) { |
||
127 | /** |
||
128 | * Generates the place graph piece for the subtrip / Itinerary arrays. |
||
129 | * |
||
130 | * @param array $data subTrip / itinerary data. |
||
131 | * @param string $type The type in data to save the terms in. |
||
132 | * @param string $post_id The post ID of the current Place to add. |
||
133 | * @param WPSEO_Schema_Context $context The post ID of the current Place to add. |
||
134 | * @param string $contained_in The @id of the containedIn place. |
||
135 | * |
||
136 | * @return mixed array $data Place data. |
||
137 | */ |
||
138 | public static function add_place( $data, $type, $post_id, $context, $contained_in = false ) { |
||
156 | /** |
||
157 | * Adds an image node if the post has a featured image. |
||
158 | * |
||
159 | * @param array $data The Review data. |
||
160 | * @param WPSEO_Schema_Context $context The post ID of the current Place to add. |
||
161 | * |
||
162 | * @return array $data The Review data. |
||
163 | */ |
||
164 | public static function add_image( $data, $context ) { |
||
172 | /** |
||
173 | * Generates the itemReviewed schema |
||
174 | * |
||
175 | * @param array $items The array of IDS. |
||
176 | * @param string $type The schema type. |
||
177 | * @return array $schema An array of the schema markup. |
||
178 | */ |
||
179 | public static function get_item_reviewed( $items = array(), $type = '' ) { |
||
196 | /** |
||
197 | * Adds a term or multiple terms, comma separated, to a field. |
||
198 | * |
||
199 | * @param array $data Review data. |
||
200 | * @param string $post_id The ID of the item to fetch terms |
||
201 | * @param string $key The key in data to save the terms in. |
||
202 | * @param string $taxonomy The taxonomy to retrieve the terms from. |
||
203 | * |
||
204 | * @return mixed array $data Review data. |
||
205 | */ |
||
206 | public static function add_terms( $data, $post_id, $key, $taxonomy ) { |
||
221 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.