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 |
||
| 19 | class Wordlift_Relation_Service { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The singleton instance. |
||
| 23 | * |
||
| 24 | * @since 3.15.0 |
||
| 25 | * @access private |
||
| 26 | * @var \Wordlift_Relation_Service $instance The singleton instance. |
||
| 27 | */ |
||
| 28 | private static $instance; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The relation table name in MySQL, set during instantiation. |
||
| 32 | * |
||
| 33 | * @since 3.15.0 |
||
| 34 | * @access private |
||
| 35 | * @var string $relation_table The relation table name. |
||
| 36 | */ |
||
| 37 | private $relation_table; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Create a {@link Wordlift_Relation_Service} instance. |
||
| 41 | * |
||
| 42 | * @since 3.15.0 |
||
| 43 | */ |
||
| 44 | public function __construct() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the singleton instance. |
||
| 56 | * |
||
| 57 | * @since 3.15.0 |
||
| 58 | * @access public |
||
| 59 | * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
||
| 60 | * singleton instance. |
||
| 61 | */ |
||
| 62 | public static function get_instance() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the articles referencing the specified entity {@link WP_Post}. |
||
| 69 | * |
||
| 70 | * @since 3.15.0 |
||
| 71 | * |
||
| 72 | * @param int|array $object_id The entity {@link WP_Post}'s id. |
||
| 73 | * @param string $fields The fields to return, 'ids' to only return ids or |
||
| 74 | * '*' to return all fields, by default '*'. |
||
| 75 | * @param null|string $predicate The predicate (who|what|...), by default all. |
||
| 76 | * @param null|string $status The status, by default all. |
||
| 77 | * @param array $excludes An array of ids to exclude from the results. |
||
| 78 | * @param null|int $limit The maximum number of results, by default |
||
| 79 | * no limit. |
||
| 80 | * @param null|array $include The {@link WP_Post}s' ids to include. |
||
| 81 | * |
||
| 82 | * @return array|object|null Database query results |
||
| 83 | */ |
||
| 84 | public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Add the limit clause if specified. |
||
| 116 | * |
||
| 117 | * @since 3.15.0 |
||
| 118 | * |
||
| 119 | * @param null|int $limit The maximum number of results. |
||
| 120 | * |
||
| 121 | * @return string The limit clause (empty if no limit has been specified). |
||
| 122 | */ |
||
| 123 | private static function limit( $limit = null ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Map the provided ids into entities (i.e. return the id if it's an entity |
||
| 134 | * or get the entities if it's a post). |
||
| 135 | * |
||
| 136 | * @since 3.15.0 |
||
| 137 | * |
||
| 138 | * @param int|array $object_id An array of posts/entities' ids. |
||
| 139 | * |
||
| 140 | * @return array An array of entities' ids. |
||
| 141 | */ |
||
| 142 | private function article_id_to_entity_id( $object_id ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add the WHERE clause. |
||
| 160 | * |
||
| 161 | * @since 3.15.0 |
||
| 162 | * |
||
| 163 | * @param int|array $object_id An array of {@link WP_Post}s' ids. |
||
| 164 | * |
||
| 165 | * @return string The WHERE clause. |
||
| 166 | */ |
||
| 167 | private static function where_object_id( $object_id ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add the exclude clause. |
||
| 174 | * |
||
| 175 | * @since 3.15.0 |
||
| 176 | * |
||
| 177 | * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
||
| 178 | * |
||
| 179 | * @return string The exclude clause. |
||
| 180 | */ |
||
| 181 | private static function and_article_not_in( $exclude ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Add the include clause. |
||
| 188 | * |
||
| 189 | * @since 3.15.0 |
||
| 190 | * |
||
| 191 | * @param null|int|array $include An array of {@link WP_Post}s' ids. |
||
| 192 | * |
||
| 193 | * @return string An empty string if $include is null otherwise the include |
||
| 194 | * clause. |
||
| 195 | */ |
||
| 196 | private static function and_article_in( $include = null ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
||
| 207 | * |
||
| 208 | * @since 3.15.0 |
||
| 209 | * |
||
| 210 | * @param int $object_id The object {@link WP_Post}'s id. |
||
| 211 | * @param string $fields The fields to return, 'ids' to only return ids or |
||
| 212 | * '*' to return all fields, by default '*'. |
||
| 213 | * @param null|string $status The status, by default all. |
||
| 214 | * |
||
| 215 | * @return array|object|null Database query results |
||
| 216 | */ |
||
| 217 | View Code Duplication | public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get the entities referenced by the specified {@link WP_Post}. |
||
| 246 | * |
||
| 247 | * @since 3.15.0 |
||
| 248 | * |
||
| 249 | * @param int $subject_id The {@link WP_Post}'s id. |
||
| 250 | * @param string $fields The fields to return, 'ids' to only return ids or |
||
| 251 | * '*' to return all fields, by default '*'. |
||
| 252 | * @param null|string $predicate The predicate (who|what|...), by default all. |
||
| 253 | * @param null|string $status The status, by default all. |
||
| 254 | * |
||
| 255 | * @return array|object|null Database query results |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Add the `post_status` clause. |
||
| 287 | * |
||
| 288 | * @since 3.15.0 |
||
| 289 | * |
||
| 290 | * @param null|string|array $status The status values. |
||
| 291 | * |
||
| 292 | * @return string An empty string if $status is null, otherwise the status clause. |
||
| 293 | */ |
||
| 294 | View Code Duplication | private static function and_status( $status = null ) { |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Add the `predicate` clause. |
||
| 305 | * |
||
| 306 | * @since 3.15.0 |
||
| 307 | * |
||
| 308 | * @param null|string|array $predicate An array of predicates. |
||
| 309 | * |
||
| 310 | * @return string An empty string if $predicate is null otherwise the predicate |
||
| 311 | * clause. |
||
| 312 | */ |
||
| 313 | View Code Duplication | private static function and_predicate( $predicate = null ) { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * The select fields. |
||
| 324 | * |
||
| 325 | * @since 3.15.0 |
||
| 326 | * |
||
| 327 | * @param string $fields Either 'ids' or '*', by default '*'. |
||
| 328 | * |
||
| 329 | * @return string The `id` field if `ids` otherwise `*`. |
||
| 330 | */ |
||
| 331 | private static function fields( $fields = '*' ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * The inner join clause for articles. |
||
| 339 | * |
||
| 340 | * @since 3.15.0 |
||
| 341 | * |
||
| 342 | * @return string The articles inner join clause. |
||
| 343 | */ |
||
| 344 | private static function inner_join_is_article() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * The inner join clause for non-articles. |
||
| 365 | * |
||
| 366 | * @since 3.15.0 |
||
| 367 | * |
||
| 368 | * @return string The non-articles inner join clause. |
||
| 369 | */ |
||
| 370 | private static function inner_join_is_not_article() { |
||
| 388 | |||
| 389 | } |
||
| 390 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state