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 ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add the limit clause if specified. |
||
| 115 | * |
||
| 116 | * @since 3.15.0 |
||
| 117 | * |
||
| 118 | * @param null|int $limit The maximum number of results. |
||
| 119 | * |
||
| 120 | * @return string The limit clause (empty if no limit has been specified). |
||
| 121 | */ |
||
| 122 | private static function limit( $limit = null ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Map the provided ids into entities (i.e. return the id if it's an entity |
||
| 133 | * or get the entities if it's a post). |
||
| 134 | * |
||
| 135 | * @since 3.15.0 |
||
| 136 | * |
||
| 137 | * @param int|array $object_id An array of posts/entities' ids. |
||
| 138 | * |
||
| 139 | * @return array An array of entities' ids. |
||
| 140 | */ |
||
| 141 | private function article_id_to_entity_id( $object_id ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Add the WHERE clause. |
||
| 159 | * |
||
| 160 | * @since 3.15.0 |
||
| 161 | * |
||
| 162 | * @param int|array $object_id An array of {@link WP_Post}s' ids. |
||
| 163 | * |
||
| 164 | * @return string The WHERE clause. |
||
| 165 | */ |
||
| 166 | private static function where_object_id( $object_id ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Add the exclude clause. |
||
| 173 | * |
||
| 174 | * @since 3.15.0 |
||
| 175 | * |
||
| 176 | * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
||
| 177 | * |
||
| 178 | * @return string The exclude clause. |
||
| 179 | */ |
||
| 180 | private static function and_article_not_in( $exclude ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add the include clause. |
||
| 187 | * |
||
| 188 | * @since 3.15.0 |
||
| 189 | * |
||
| 190 | * @param null|int|array $include An array of {@link WP_Post}s' ids. |
||
| 191 | * |
||
| 192 | * @return string An empty string if $include is null otherwise the include |
||
| 193 | * clause. |
||
| 194 | */ |
||
| 195 | private static function and_article_in( $include = null ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
||
| 206 | * |
||
| 207 | * @since 3.15.0 |
||
| 208 | * |
||
| 209 | * @param int $object_id The object {@link WP_Post}'s id. |
||
| 210 | * @param string $fields The fields to return, 'ids' to only return ids or |
||
| 211 | * '*' to return all fields, by default '*'. |
||
| 212 | * @param null|string $status The status, by default all. |
||
| 213 | * |
||
| 214 | * @return array|object|null Database query results |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Get the entities referenced by the specified {@link WP_Post}. |
||
| 245 | * |
||
| 246 | * @since 3.15.0 |
||
| 247 | * |
||
| 248 | * @param int $subject_id The {@link WP_Post}'s id. |
||
| 249 | * @param string $fields The fields to return, 'ids' to only return ids or |
||
| 250 | * '*' to return all fields, by default '*'. |
||
| 251 | * @param null|string $predicate The predicate (who|what|...), by default all. |
||
| 252 | * @param null|string $status The status, by default all. |
||
| 253 | * |
||
| 254 | * @return array|object|null Database query results |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Add the `post_status` clause. |
||
| 286 | * |
||
| 287 | * @since 3.15.0 |
||
| 288 | * |
||
| 289 | * @param null|string|array $status The status values. |
||
| 290 | * |
||
| 291 | * @return string An empty string if $status is null, otherwise the status clause. |
||
| 292 | */ |
||
| 293 | View Code Duplication | private static function and_status( $status = null ) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Add the `predicate` clause. |
||
| 304 | * |
||
| 305 | * @since 3.15.0 |
||
| 306 | * |
||
| 307 | * @param null|string|array $predicate An array of predicates. |
||
| 308 | * |
||
| 309 | * @return string An empty string if $predicate is null otherwise the predicate |
||
| 310 | * clause. |
||
| 311 | */ |
||
| 312 | View Code Duplication | private static function and_predicate( $predicate = null ) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * The select fields. |
||
| 323 | * |
||
| 324 | * @since 3.15.0 |
||
| 325 | * |
||
| 326 | * @param string $fields Either 'ids' or '*', by default '*'. |
||
| 327 | * |
||
| 328 | * @return string The `id` field if `ids` otherwise `*`. |
||
| 329 | */ |
||
| 330 | private static function fields( $fields = '*' ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * The inner join clause for articles. |
||
| 338 | * |
||
| 339 | * @since 3.15.0 |
||
| 340 | * |
||
| 341 | * @return string The articles inner join clause. |
||
| 342 | */ |
||
| 343 | private static function inner_join_is_article() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * The inner join clause for non-articles. |
||
| 364 | * |
||
| 365 | * @since 3.15.0 |
||
| 366 | * |
||
| 367 | * @return string The non-articles inner join clause. |
||
| 368 | */ |
||
| 369 | private static function inner_join_is_not_article() { |
||
| 387 | |||
| 388 | } |
||
| 389 |
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