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 |
||
| 7 | class Sync_Post_Hooks extends Abstract_Sync_Hooks { |
||
| 8 | /** |
||
| 9 | * @var \Wordlift_Log_Service |
||
| 10 | */ |
||
| 11 | private $log; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Sync_Service |
||
| 15 | */ |
||
| 16 | private $sync_service; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Sync_Object_Adapter_Factory |
||
| 20 | */ |
||
| 21 | private $sync_object_factory; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Sync_Post_Hooks constructor. |
||
| 25 | * |
||
| 26 | * @param Sync_Service $sync_service |
||
| 27 | * @param Sync_Object_Adapter_Factory $sync_object_factory |
||
| 28 | */ |
||
| 29 | View Code Duplication | function __construct( $sync_service, $sync_object_factory ) { |
|
| 39 | |||
| 40 | private function register_hooks() { |
||
| 51 | |||
| 52 | public function save_post( $post_id ) { |
||
| 61 | |||
| 62 | public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) { |
||
| 80 | |||
| 81 | private function sync( $post_id ) { |
||
| 84 | |||
| 85 | public function do_sync( $post_id ) { |
||
| 100 | |||
| 101 | public function delete_post( $post_id ) { |
||
| 104 | |||
| 105 | public function do_delete( $post_id ) { |
||
| 112 | |||
| 113 | } |
||
| 114 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.