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 | abstract class Jetpack_JSON_API_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 9 | |||
| 10 | protected $needed_capabilities; |
||
| 11 | protected $expected_actions = array(); |
||
| 12 | protected $action; |
||
| 13 | |||
| 14 | |||
| 15 | public function callback( $path = '', $blog_id = 0, $object = null ) { |
||
| 16 | if ( is_wp_error( $error = $this->validate_call( $blog_id, $this->needed_capabilities ) ) ) { |
||
| 17 | return $error; |
||
| 18 | } |
||
| 19 | |||
| 20 | if ( is_wp_error( $error = $this->validate_input( $object ) ) ) { |
||
| 21 | return $error; |
||
| 22 | } |
||
| 23 | |||
| 24 | if ( ! empty( $this->action ) ) { |
||
| 25 | if( is_wp_error( $error = call_user_func( array( $this, $this->action ) ) ) ) { |
||
| 26 | return $error; |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | return $this->result(); |
||
| 31 | } |
||
| 32 | |||
| 33 | abstract protected function result(); |
||
| 34 | |||
| 35 | protected function validate_input( $object ) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Switches to the blog and checks current user capabilities. |
||
| 59 | * @return bool|WP_Error a WP_Error object or true if things are good. |
||
| 60 | */ |
||
| 61 | protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param $capability |
||
| 80 | * |
||
| 81 | * @return bool|WP_Error |
||
| 82 | */ |
||
| 83 | protected function check_capability( $capability ) { |
||
| 114 | |||
| 115 | } |
||
| 116 |