Conditions | 10 |
Paths | 2 |
Total Lines | 50 |
Code Lines | 29 |
Lines | 16 |
Ratio | 32 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
16 | public static function parse( $input, $is_option ) { |
||
17 | if ( empty( $input ) ) { |
||
18 | return null; |
||
19 | } |
||
20 | |||
21 | $parsed_data = array_map( function( $item ) { |
||
22 | |||
23 | if ( ! self::is_key_present( $item['id'], $item ) ) { |
||
24 | self::throw_exception( __( 'Please make sure you have provided ids', 'crb' ) ); |
||
25 | } |
||
26 | |||
27 | if ( ! self::is_key_present( $item['type'], $item ) ) { |
||
28 | self::throw_exception( __( 'Please make sure you have provided types', 'crb' ) ); |
||
29 | } |
||
30 | |||
31 | switch ( $item['type'] ) { |
||
32 | case 'user': |
||
33 | return 'user:user:' . $item['id']; |
||
34 | break; |
||
1 ignored issue
–
show
|
|||
35 | |||
36 | case 'comment': |
||
37 | return 'comment:comment:' . $item['id']; |
||
38 | break; |
||
1 ignored issue
–
show
|
|||
39 | |||
40 | View Code Duplication | case 'post': |
|
41 | |||
42 | if ( ! self::is_key_present( 'post_type', $item ) ) { |
||
43 | self::throw_exception( __( 'Please provide post_type', 'crb' ) ); |
||
44 | } |
||
45 | |||
46 | return 'post:' . $item['post_type'] . ':' . $item['id']; |
||
47 | break; |
||
1 ignored issue
–
show
|
|||
48 | |||
49 | View Code Duplication | case 'term': |
|
50 | |||
51 | if ( ! self::is_key_present( 'taxonomy', $item ) ) { |
||
52 | self::throw_exception( __( 'Please provide taxonomy', 'crb' ) ); |
||
53 | } |
||
54 | |||
55 | return 'term:' . $item['taxonomy'] . ':' . $item['id']; |
||
56 | break; |
||
1 ignored issue
–
show
|
|||
57 | |||
58 | default: |
||
59 | self::throw_exception( __( 'Unknown type used!', 'crb' ) ); |
||
60 | } |
||
61 | |||
62 | }, $input ); |
||
63 | |||
64 | return $parsed_data; |
||
65 | } |
||
66 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.