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 |
||
| 17 | class Entity_Rest_Endpoint { |
||
| 18 | |||
| 19 | const SAME_AS_META_KEY = 'entity_same_as'; |
||
| 20 | const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
||
| 21 | const DESCRIPTION_META_KEY = 'entity_description'; |
||
| 22 | const TYPE_META_KEY = 'entity_type'; |
||
| 23 | const EXTERNAL_ENTITY_META_KEY = '_wl_is_external'; |
||
| 24 | const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui'; |
||
| 25 | |||
| 26 | |||
| 27 | public function register_routes() { |
||
| 28 | $that = $this; |
||
| 29 | add_action( 'rest_api_init', |
||
| 30 | function () use ( $that ) { |
||
| 31 | $that->register_accept_route(); |
||
| 32 | $that->register_undo_route(); |
||
| 33 | $that->register_nomatch_route(); |
||
| 34 | $that->register_reject_route(); |
||
| 35 | } ); |
||
| 36 | } |
||
| 37 | |||
| 38 | |||
| 39 | View Code Duplication | public function accept_entity( $request ) { |
|
|
|
|||
| 40 | $data = $request->get_params(); |
||
| 41 | $term_id = (int) $data['term_id']; |
||
| 42 | $entity_data = (array) $data['entity']; |
||
| 43 | $entity = Entity_List_Factory::get_instance( $term_id ); |
||
| 44 | $entity->save_jsonld_data( $entity_data ); |
||
| 45 | update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 ); |
||
| 46 | Ttl_Cache::flush_all(); |
||
| 47 | |||
| 48 | return $term_id; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function undo( $request ) { |
||
| 52 | $data = $request->get_params(); |
||
| 53 | $term_id = (int) $data['term_id']; |
||
| 54 | $entity = Entity_List_Factory::get_instance( $term_id ); |
||
| 55 | $entity->clear_data(); |
||
| 56 | delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING ); |
||
| 57 | Ttl_Cache::flush_all(); |
||
| 58 | |||
| 59 | return $term_id; |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | public function mark_as_no_match( $request ) { |
||
| 64 | $data = $request->get_params(); |
||
| 65 | $term_id = (int) $data['term_id']; |
||
| 66 | Ttl_Cache::flush_all(); |
||
| 67 | |||
| 68 | return update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 ); |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | View Code Duplication | private function register_undo_route() { |
|
| 93 | |||
| 94 | private function register_accept_route() { |
||
| 98 | |||
| 99 | private function register_reject_route() { |
||
| 103 | |||
| 104 | View Code Duplication | public function reject_entity( $request ) { |
|
| 105 | $data = $request->get_params(); |
||
| 106 | $term_id = (int) $data['term_id']; |
||
| 107 | $entity_data = (array) $data['entity']; |
||
| 113 | |||
| 114 | View Code Duplication | private function register_nomatch_route() { |
|
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $accept_route |
||
| 139 | */ |
||
| 140 | private function register_entity_accept_or_reject_route( $accept_route, $callback ) { |
||
| 169 | |||
| 170 | } |
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.