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 { |
||
| 8 | /** |
||
| 9 | * @var \Wordlift_Log_Service |
||
| 10 | */ |
||
| 11 | private $log; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Sync_Service |
||
| 15 | */ |
||
| 16 | private $sync_service; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Sync_Post_Hooks constructor. |
||
| 20 | * |
||
| 21 | * @param Sync_Service $sync_service |
||
| 22 | */ |
||
| 23 | function __construct( $sync_service ) { |
||
| 32 | |||
| 33 | private function register_hooks() { |
||
| 44 | |||
| 45 | public function save_post( $post_id ) { |
||
| 50 | |||
| 51 | public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) { |
||
| 64 | |||
| 65 | private function sync( $post_id ) { |
||
| 74 | |||
| 75 | View Code Duplication | public function delete_post( $post_id ) { |
|
| 84 | |||
| 85 | } |
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.