| Conditions | 12 |
| Paths | 25 |
| Total Lines | 43 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 113 | protected function parse_attributes_field( $raw_attributes, $object = null ) { |
||
| 114 | $attributes = array(); |
||
| 115 | $parent = wc_get_product( $object->get_parent_id() ); |
||
| 116 | $parent_attributes = $parent->get_attributes(); |
||
| 117 | |||
| 118 | foreach ( $raw_attributes as $attribute ) { |
||
| 119 | $attribute_id = 0; |
||
| 120 | $attribute_name = ''; |
||
| 121 | |||
| 122 | // Check ID for global attributes or name for product attributes. |
||
| 123 | if ( ! empty( $attribute['id'] ) ) { |
||
| 124 | $attribute_id = absint( $attribute['id'] ); |
||
| 125 | $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); |
||
| 126 | } elseif ( ! empty( $attribute['name'] ) ) { |
||
| 127 | $attribute_name = sanitize_title( $attribute['name'] ); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ( ! $attribute_id && ! $attribute_name ) { |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) { |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | $attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() ); |
||
| 139 | $attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; |
||
| 140 | |||
| 141 | if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) { |
||
| 142 | // If dealing with a taxonomy, we need to get the slug from the name posted to the API. |
||
| 143 | $term = get_term_by( 'name', $attribute_value, $attribute_name ); |
||
| 144 | |||
| 145 | if ( $term && ! is_wp_error( $term ) ) { |
||
| 146 | $attribute_value = $term->slug; |
||
| 147 | } else { |
||
| 148 | $attribute_value = sanitize_title( $attribute_value ); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $attributes[ $attribute_key ] = $attribute_value; |
||
| 153 | } |
||
| 154 | |||
| 155 | return $attributes; |
||
| 156 | } |
||
| 189 |