| Conditions | 9 |
| Paths | 9 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 53 | public function register( Registerable $registerable ): void { |
||
| 54 | if ( ! is_a( $registerable, Additional_Meta_Data_Controller::class ) ) { |
||
| 55 | throw new Exception( 'Registerable must be an instance of Additional_Meta_Data_Controller' ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Additional_Meta_Data $registerable, Validation call below catches no Additional_Meta_Data Registerables |
||
| 60 | */ |
||
| 61 | $meta_data = $this->filter_meta_data( $registerable->meta_data( array() ) ); |
||
| 62 | |||
| 63 | // Iterate through all meta data and register them. |
||
| 64 | foreach ( $meta_data as $meta_data_item ) { |
||
| 65 | switch ( $meta_data_item->get_meta_type() ) { |
||
| 66 | case 'post': |
||
| 67 | // Throw if post type not defined. |
||
| 68 | if ( null === $meta_data_item->get_subtype() ) { |
||
| 69 | throw new Exception( |
||
| 70 | sprintf( |
||
| 71 | 'A post type must be defined when attempting to register post meta with meta key : %s', |
||
| 72 | esc_attr( $meta_data_item->get_meta_key() ) |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->meta_data_registrar->register_for_post_type( |
||
| 78 | $meta_data_item, |
||
| 79 | $meta_data_item->get_subtype() |
||
| 80 | ); |
||
| 81 | break; |
||
| 82 | |||
| 83 | case 'term': |
||
| 84 | // Throw if Taxonomy not defined. |
||
| 85 | if ( null === $meta_data_item->get_subtype() ) { |
||
| 86 | throw new Exception( |
||
| 87 | sprintf( |
||
| 88 | 'A taxonomy must be defined when attempting to register tern meta with meta key : %s', |
||
| 89 | esc_attr( $meta_data_item->get_meta_key() ) |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->meta_data_registrar->register_for_term( |
||
| 95 | $meta_data_item, |
||
| 96 | $meta_data_item->get_subtype() |
||
| 97 | ); |
||
| 98 | break; |
||
| 99 | |||
| 100 | case 'user': |
||
| 101 | $this->meta_data_registrar->register_for_user( |
||
| 102 | $meta_data_item |
||
| 103 | ); |
||
| 104 | break; |
||
| 105 | |||
| 106 | case 'comment': |
||
| 107 | $this->meta_data_registrar->register_for_comment( |
||
| 108 | $meta_data_item |
||
| 109 | ); |
||
| 110 | break; |
||
| 111 | |||
| 112 | default: |
||
| 113 | throw new Exception( 'Unexpected meta type' ); |
||
| 114 | } |
||
| 133 |