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:
Complex classes like Analysis_Response_Ops often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Analysis_Response_Ops, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Analysis_Response_Ops { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The analysis response json. |
||
| 19 | * |
||
| 20 | * @since 3.21.5 |
||
| 21 | * @access private |
||
| 22 | * @var mixed $json Holds the analysis response json. |
||
| 23 | */ |
||
| 24 | private $json; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Holds the {@link Wordlift_Entity_Uri_Service}. |
||
| 28 | * |
||
| 29 | * @since 3.21.5 |
||
| 30 | * @access private |
||
| 31 | * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
||
| 32 | */ |
||
| 33 | private $entity_uri_service; |
||
| 34 | |||
| 35 | private $entity_service; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \Wordlift_Entity_Type_Service |
||
| 39 | */ |
||
| 40 | private $entity_type_service; |
||
| 41 | /** |
||
| 42 | * @var \Wordlift_Post_Image_Storage |
||
| 43 | */ |
||
| 44 | private $post_image_storage; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Entity_Helper |
||
| 48 | */ |
||
| 49 | private $entity_helper; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Analysis_Response_Ops constructor. |
||
| 53 | * |
||
| 54 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
||
| 55 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service}. |
||
| 56 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service}. |
||
| 57 | * @param \Wordlift_Post_Image_Storage $post_image_storage A {@link Wordlift_Post_Image_Storage} instance. |
||
| 58 | * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
||
| 59 | * @param mixed $json The analysis response json. |
||
| 60 | * |
||
| 61 | * @since 3.21.5 |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $entity_helper, $json ) { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
||
| 76 | * |
||
| 77 | * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
||
| 78 | * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
||
| 79 | * matches from the local vocabulary. |
||
| 80 | * |
||
| 81 | * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
||
| 82 | * |
||
| 83 | * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
||
| 84 | */ |
||
| 85 | public function make_entities_local() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add occurrences by parsing the provided html content. |
||
| 132 | * |
||
| 133 | * @param string $content The html content with annotations. |
||
| 134 | * |
||
| 135 | * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
||
| 136 | * |
||
| 137 | * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
||
| 138 | * and disambiguated. |
||
| 139 | * |
||
| 140 | * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
||
| 141 | */ |
||
| 142 | public function add_occurrences( $content ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add local entities |
||
| 241 | * |
||
| 242 | * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
||
| 243 | * |
||
| 244 | * @since 3.27.6 |
||
| 245 | * |
||
| 246 | * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
||
| 247 | */ |
||
| 248 | public function add_local_entities(){ |
||
| 283 | |||
| 284 | private function get_local_entity( $uri ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Return the JSON response. |
||
| 321 | * |
||
| 322 | * @return mixed The JSON response. |
||
| 323 | * @since 3.24.2 |
||
| 324 | */ |
||
| 325 | public function get_json() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the string representation of the JSON. |
||
| 332 | * |
||
| 333 | * @return false|string The string representation or false in case of error. |
||
| 334 | */ |
||
| 335 | public function to_string() { |
||
| 343 | |||
| 344 | } |
||
| 345 |
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.