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 |
||
| 18 | class Wordlift_Entity_Uri_Service { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * A {@link Wordlift_Log_Service} instance. |
||
| 22 | * |
||
| 23 | * @since 3.16.3 |
||
| 24 | * @access private |
||
| 25 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 26 | */ |
||
| 27 | private $log; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The {@link Wordlift_Configuration_Service} instance. |
||
| 31 | * |
||
| 32 | * @since 3.16.3 |
||
| 33 | * @access private |
||
| 34 | * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 35 | */ |
||
| 36 | private $configuration_service; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * An array of URIs to post ID valid for the current request. |
||
| 40 | * |
||
| 41 | * @since 3.16.3 |
||
| 42 | * @access private |
||
| 43 | * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
||
| 44 | */ |
||
| 45 | protected $uri_to_post; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create a {@link Wordlift_Entity_Uri_Service} instance. |
||
| 49 | * |
||
| 50 | * @since 3.16.3 |
||
| 51 | * |
||
| 52 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 53 | */ |
||
| 54 | public function __construct( $configuration_service ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Preload the provided URIs in the local cache. |
||
| 64 | * |
||
| 65 | * This function will populate the local `$uri_to_post` array by running a |
||
| 66 | * single query with all the URIs and returning the mappings in the array. |
||
| 67 | * |
||
| 68 | * @since 3.16.3 |
||
| 69 | * |
||
| 70 | * @param array $uris An array of URIs. |
||
| 71 | */ |
||
| 72 | public function preload_uris( $uris ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Reset the URI to post local cache. |
||
| 140 | * |
||
| 141 | * @since 3.16.3 |
||
| 142 | */ |
||
| 143 | public function reset_uris() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
||
| 151 | * |
||
| 152 | * @since 3.2.0 |
||
| 153 | * |
||
| 154 | * @param string $uri The entity URI. |
||
| 155 | * |
||
| 156 | * @return WP_Post|null A WP_Post instance or null if not found. |
||
| 157 | */ |
||
| 158 | public function get_entity( $uri ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Determines whether a given uri is an internal uri or not. |
||
| 213 | * |
||
| 214 | * @since 3.16.3 |
||
| 215 | * |
||
| 216 | * @param int $uri An uri. |
||
| 217 | * |
||
| 218 | * @return true if the uri internal to the current dataset otherwise false. |
||
| 219 | */ |
||
| 220 | public function is_internal( $uri ) { |
||
| 224 | |||
| 225 | } |
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.