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 |
||
| 8 | class Sync_Service { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var \Wordlift_Log_Service |
||
| 12 | */ |
||
| 13 | private $log; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Api_Service |
||
| 17 | */ |
||
| 18 | private $api_service; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Jsonld_Service |
||
| 22 | */ |
||
| 23 | private $jsonld_service; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Sync_Background_Process |
||
| 27 | */ |
||
| 28 | private $sync_background_process; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor. |
||
| 32 | * |
||
| 33 | * @param $api_service Api_Service The {@link Api_Service} used to communicate with the remote APIs. |
||
| 34 | * @param $jsonld_service Jsonld_Service The {@link Jsonld_Service} used to generate the JSON-LD to post to the remote API. |
||
| 35 | */ |
||
| 36 | public function __construct( $api_service, $jsonld_service ) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Starts a new synchronization. |
||
| 50 | */ |
||
| 51 | public function start() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Request to cancel a background process. |
||
| 60 | */ |
||
| 61 | public function request_cancel() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the next post ID to synchronize or NULL if there are no posts to synchronize. |
||
| 69 | * |
||
| 70 | * @return string|null The post ID or NULL if there are no posts to synchronize. |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function next() { |
|
| 92 | |||
| 93 | View Code Duplication | public function count() { |
|
| 108 | |||
| 109 | public function info() { |
||
| 112 | |||
| 113 | public function sync_item( $post_id ) { |
||
| 141 | |||
| 142 | } |
||
| 143 |
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.