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 |
||
| 16 | class Linked_Data_Autocomplete_Service implements Autocomplete_Service { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The {@link Wordlift_Configuration_Service} instance. |
||
| 20 | * |
||
| 21 | * @since 3.15.0 |
||
| 22 | * @access private |
||
| 23 | * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 24 | */ |
||
| 25 | private $configuration_service; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * A {@link Wordlift_Log_Service} instance. |
||
| 29 | * |
||
| 30 | * @since 3.15.0 |
||
| 31 | * @access private |
||
| 32 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 33 | */ |
||
| 34 | private $log; |
||
| 35 | private $entity_helper; |
||
| 36 | private $entity_uri_service; |
||
| 37 | /** |
||
| 38 | * @var \Wordlift_Entity_Service |
||
| 39 | */ |
||
| 40 | private $entity_service; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The {@link Class_Wordlift_Autocomplete_Service} instance. |
||
| 44 | * |
||
| 45 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
||
| 46 | * @param Entity_Helper $entity_helper |
||
| 47 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
||
| 48 | * @param \Wordlift_Entity_Service $entity_service |
||
| 49 | * |
||
| 50 | * @since 3.15.0 |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function __construct( $configuration_service, $entity_helper, $entity_uri_service, $entity_service ) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Make request to external API and return the response. |
||
| 65 | * |
||
| 66 | * @param string $query The search string. |
||
| 67 | * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also |
||
| 68 | * in Wikipedia. By default is "cloud". |
||
| 69 | * @param array|string $excludes The exclude parameter string. |
||
| 70 | * |
||
| 71 | * @return array $response The API response. |
||
| 72 | * @since 3.15.0 |
||
| 73 | * |
||
| 74 | */ |
||
| 75 | public function query( $query, $scope = 'cloud', $excludes = array() ) { |
||
| 110 | |||
| 111 | private function do_query( $query, $scope = 'cloud', $exclude = '' ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Build the autocomplete url. |
||
| 140 | * |
||
| 141 | * @param string $query The search string. |
||
| 142 | * @param array|string $exclude The exclude parameter. |
||
| 143 | * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also |
||
| 144 | * in Wikipedia. By default is "cloud". |
||
| 145 | * |
||
| 146 | * @return string Built url. |
||
| 147 | * @since 3.15.0 |
||
| 148 | * |
||
| 149 | */ |
||
| 150 | private function build_request_url( $query, $exclude, $scope ) { |
||
| 173 | |||
| 174 | private function post_to_autocomplete_result( $uri, $post ) { |
||
| 190 | |||
| 191 | } |
||
| 192 |
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.