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 |
||
| 3 | class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 4 | // /sites/%s/tags -> $blog_id |
||
| 5 | // /sites/%s/categories -> $blog_id |
||
| 6 | function callback( $path = '', $blog_id = 0 ) { |
||
| 21 | |||
| 22 | View Code Duplication | function process_args( $args ) { |
|
| 23 | if ( $args['number'] < 1 ) { |
||
| 24 | $args['number'] = 100; |
||
| 25 | } elseif ( 1000 < $args['number'] ) { |
||
| 26 | return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 ); |
||
| 27 | } |
||
| 28 | |||
| 29 | if ( isset( $args['page'] ) ) { |
||
| 30 | if ( $args['page'] < 1 ) { |
||
| 31 | $args['page'] = 1; |
||
| 32 | } |
||
| 33 | |||
| 34 | $args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
||
| 35 | unset( $args['page'] ); |
||
| 36 | } |
||
| 37 | |||
| 38 | if ( $args['offset'] < 0 ) { |
||
| 39 | $args['offset'] = 0; |
||
| 40 | } |
||
| 41 | |||
| 42 | $args['orderby'] = $args['order_by']; |
||
| 43 | unset( $args['order_by'] ); |
||
| 44 | |||
| 45 | unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] ); |
||
| 46 | return $args; |
||
| 47 | } |
||
| 48 | |||
| 49 | View Code Duplication | function categories( $args ) { |
|
| 66 | |||
| 67 | View Code Duplication | function tags( $args ) { |
|
| 84 | } |
||
| 85 |