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 |
||
| 12 | class Wordlift_Sparql_Service { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * A {@link Wordlift_Log_Service} instance. |
||
| 16 | * |
||
| 17 | * @since 3.6.0 |
||
| 18 | * @access private |
||
| 19 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 20 | */ |
||
| 21 | private $log; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The {@link Wordlift_Sparql_Service} singleton instance. |
||
| 25 | * |
||
| 26 | * @since 3.6.0 |
||
| 27 | * @access private |
||
| 28 | * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance. |
||
| 29 | */ |
||
| 30 | private static $instance; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a {@link Wordlift_Sparql_Service} instance. |
||
| 34 | * |
||
| 35 | * @since 3.6.0 |
||
| 36 | */ |
||
| 37 | public function __construct() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get the singleton instance of the {@link Wordlift_Sparql_Service}. |
||
| 47 | * |
||
| 48 | * @since 3.6.0 |
||
| 49 | * @return \Wordlift_Sparql_Service |
||
| 50 | */ |
||
| 51 | public static function get_instance() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Queue a SPARQL statement for execution. |
||
| 58 | * |
||
| 59 | * @since 3.6.0 |
||
| 60 | * |
||
| 61 | * @param string $stmt The SPARQL statement. |
||
| 62 | * @param bool $queue Whether to queue the statement for asynchronous |
||
| 63 | * execution. |
||
| 64 | */ |
||
| 65 | public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Run the SPARQL queries buffered for the specified request id. |
||
| 73 | * |
||
| 74 | * @since 3.13.2 |
||
| 75 | * |
||
| 76 | * @param string $request_id A unique request id. |
||
| 77 | */ |
||
| 78 | public function run_sparql_query( $request_id ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Queue a SPARQL statement for asynchronous execution. |
||
| 110 | * |
||
| 111 | * @since 3.13.2 |
||
| 112 | * |
||
| 113 | * @param string $stmt The SPARQL statement. |
||
| 114 | */ |
||
| 115 | public function queue( $stmt ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get a temporary filename where to store SPARQL queries. |
||
| 129 | * |
||
| 130 | * @since 3.13.2 |
||
| 131 | * |
||
| 132 | * @return string The filename. |
||
| 133 | * @throws Exception An exception is thrown if there are already 1.000 |
||
| 134 | * temporary files for this request. |
||
| 135 | */ |
||
| 136 | private function get_temporary_file_for_sparql() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Execute the SELECT query. |
||
| 161 | * |
||
| 162 | * @since 3.12.2 |
||
| 163 | * |
||
| 164 | * @param string $query The SELECT query to execute. |
||
| 165 | * |
||
| 166 | * @return WP_Error|array The response or WP_Error on failure. |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function select( $query ) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Formats the provided value according to the specified type in order to |
||
| 184 | * insert the value using SPARQL. The value is also escaped. |
||
| 185 | * |
||
| 186 | * @since 3.6.0 |
||
| 187 | * |
||
| 188 | * @param string $value The value. |
||
| 189 | * @param string $type The value type. |
||
| 190 | * |
||
| 191 | * @return string The formatted value for SPARQL statements. |
||
| 192 | */ |
||
| 193 | public function format( $value, $type ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Escapes an URI for a SPARQL statement. |
||
| 238 | * |
||
| 239 | * @since 3.6.0 |
||
| 240 | * |
||
| 241 | * @param string $uri The URI to escape. |
||
| 242 | * |
||
| 243 | * @return string The escaped URI. |
||
| 244 | */ |
||
| 245 | public static function escape_uri( $uri ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Escapes a string for a SPARQL statement. |
||
| 258 | * |
||
| 259 | * @since 3.6.0 |
||
| 260 | * |
||
| 261 | * @param string $string The string to escape. |
||
| 262 | * |
||
| 263 | * @return string The escaped string. |
||
| 264 | */ |
||
| 265 | public static function escape( $string ) { |
||
| 288 | |||
| 289 | } |
||
| 290 |
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.