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_Linked_Data_Service { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * A {@link Wordlift_Log_Service} instance. |
||
| 23 | * |
||
| 24 | * @since 3.15.0 |
||
| 25 | * @access private |
||
| 26 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 27 | */ |
||
| 28 | private $log; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The {@link Wordlift_Entity_Service} instance. |
||
| 32 | * |
||
| 33 | * @since 3.15.0 |
||
| 34 | * @access private |
||
| 35 | * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
||
| 36 | */ |
||
| 37 | private $entity_service; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The {@link Wordlift_Entity_Type_Service} instance. |
||
| 41 | * |
||
| 42 | * @since 3.15.0 |
||
| 43 | * @access private |
||
| 44 | * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
||
| 45 | */ |
||
| 46 | private $entity_type_service; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The {@link Wordlift_Schema_Service} instance. |
||
| 50 | * |
||
| 51 | * @since 3.15.0 |
||
| 52 | * @access private |
||
| 53 | * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
||
| 54 | */ |
||
| 55 | private $schema_service; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The {@link Wordlift_Linked_Data_Service} singleton instance. |
||
| 59 | * |
||
| 60 | * @since 3.15.0 |
||
| 61 | * @access private |
||
| 62 | * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance. |
||
| 63 | */ |
||
| 64 | private static $instance; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The {@link Wordlift_Sparql_Service} instance. |
||
| 68 | * |
||
| 69 | * @since 3.15.0 |
||
| 70 | * @access private |
||
| 71 | * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
||
| 72 | */ |
||
| 73 | private $sparql_service; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Create a {@link Wordlift_Linked_Data_Service} instance. |
||
| 77 | * |
||
| 78 | * @since 3.15.0 |
||
| 79 | * |
||
| 80 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
||
| 81 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
||
| 82 | * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
||
| 83 | * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get the singleton instance of {@link Wordlift_Linked_Data_Service}. |
||
| 100 | * |
||
| 101 | * @since 3.15.0 |
||
| 102 | * |
||
| 103 | * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>. |
||
| 104 | */ |
||
| 105 | public static function get_instance() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Push a {@link WP_Post} to the Linked Data store. |
||
| 112 | * |
||
| 113 | * If the {@link WP_Post} is an entity and it's not of the `Article` type, |
||
| 114 | * then it is pushed to the remote Linked Data store. |
||
| 115 | * |
||
| 116 | * @since 3.15.0 |
||
| 117 | * |
||
| 118 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 119 | */ |
||
| 120 | public function push( $post_id ) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Remove the specified {@link WP_Post} from the Linked Data. |
||
| 144 | * |
||
| 145 | * @since 3.15.0 |
||
| 146 | * |
||
| 147 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 148 | */ |
||
| 149 | public function remove( $post_id ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Push an entity to the Linked Data store. |
||
| 160 | * |
||
| 161 | * @since 3.15.0 |
||
| 162 | * |
||
| 163 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 164 | */ |
||
| 165 | private function do_push( $post_id ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Check if an entity's {@link WP_Post} has a valid URI. |
||
| 205 | * |
||
| 206 | * @since 3.15.0 |
||
| 207 | * |
||
| 208 | * @param int $post_id The entity's {@link WP_Post}'s id. |
||
| 209 | * |
||
| 210 | * @return bool True if the URI is valid otherwise false. |
||
| 211 | */ |
||
| 212 | private function has_valid_uri( $post_id ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the delete statements. |
||
| 233 | * |
||
| 234 | * @since 3.15.0 |
||
| 235 | * |
||
| 236 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 237 | * |
||
| 238 | * @return array An array of delete statements. |
||
| 239 | */ |
||
| 240 | private function get_delete_statements( $post_id ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the SPARQL insert tuples ( ?s ?p ?o ) for the specified {@link WP_Post}. |
||
| 269 | * |
||
| 270 | * @since 3.15.0 |
||
| 271 | * |
||
| 272 | * @param int $post_id The {@link WP_Post}'s id. |
||
| 273 | * |
||
| 274 | * @return array An array of insert tuples. |
||
| 275 | */ |
||
| 276 | private function get_insert_tuples( $post_id ) { |
||
| 296 | |||
| 297 | } |
||
| 298 |
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.