WC_Order_Item_Meta::get_formatted()   C
last analyzed

Complexity

Conditions 12
Paths 3

Size

Total Lines 35
Code Lines 19

Duplication

Lines 6
Ratio 17.14 %

Importance

Changes 0
Metric Value
cc 12
dl 6
loc 35
rs 5.1612
c 0
b 0
f 0
eloc 19
nc 3
nop 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Order Item Meta
9
 *
10
 * A Simple class for managing order item meta so plugins add it in the correct format.
11
 *
12
 * @class 		order_item_meta
13
 * @version		2.4
14
 * @package		WooCommerce/Classes
15
 * @author 		WooThemes
16
 */
17
class WC_Order_Item_Meta {
18
19
	/** @var bool For handling backwards comp */
20
	private $legacy = false;
21
22
	/** @var Array Order item */
23
	private $item   = null;
24
25
	/** @var Array Post meta data */
26
	public $meta    = null;
27
28
	/** @var Product object */
29
	public $product = null;
30
31
	/**
32
	 * Constructor.
33
	 *
34
	 * @param array $item defaults to array()
35
	 * @param \WC_Product $product defaults to null
36
	 * @return \WC_Order_Item_Meta instance
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
	 */
38
	public function __construct( $item = array(), $product = null ) {
39
		// Backwards (pre 2.4) compat
40
		if ( ! isset( $item['item_meta'] ) ) {
41
			$this->legacy = true;
42
			$this->meta   = array_filter( (array) $item );
43
			return;
44
		}
45
46
		$this->item    = $item;
47
		$this->meta    = array_filter( (array) $item['item_meta'] );
48
		$this->product = $product;
0 ignored issues
show
Documentation Bug introduced by
It seems like $product can also be of type object<WC_Product>. However, the property $product is declared as type object<Product>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
	}
50
51
	/**
52
	 * Display meta in a formatted list.
53
	 *
54
	 * @param bool $flat (default: false)
55
	 * @param bool $return (default: false)
56
	 * @param string $hideprefix (default: _)
57
	 * @param  string $delimiter Delimiter used to separate items when $flat is true
58
	 * @return string|void
59
	 */
60
	public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
61
		$output         = '';
62
		$formatted_meta = $this->get_formatted( $hideprefix );
63
64
		if ( ! empty( $formatted_meta ) ) {
65
			$meta_list = array();
66
67
			foreach ( $formatted_meta as $meta ) {
68
				if ( $flat ) {
69
					$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
70
				} else {
71
					$meta_list[] = '
72
						<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
73
						<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
74
					';
75
				}
76
			}
77
78
			if ( ! empty( $meta_list ) ) {
79
				if ( $flat ) {
80
					$output .= implode( $delimiter, $meta_list );
81
				} else {
82
					$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
83
				}
84
			}
85
		}
86
87
		$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this );
88
89
		if ( $return ) {
90
			return $output;
91
		} else {
92
			echo $output;
93
		}
94
	}
95
96
	/**
97
	 * Return an array of formatted item meta in format e.g.
98
	 *
99
	 * array(
100
	 *   'pa_size' => array(
101
	 *     'label' => 'Size',
102
	 *     'value' => 'Medium',
103
	 *   )
104
	 * )
105
	 *
106
	 * @since 2.4
107
	 * @param string $hideprefix exclude meta when key is prefixed with this, defaults to `_`
108
	 * @return array
109
	 */
110
	public function get_formatted( $hideprefix = '_' ) {
111
		if ( $this->legacy ) {
112
			return $this->get_formatted_legacy( $hideprefix );
113
		}
114
115
		$formatted_meta = array();
116
117
		if ( ! empty( $this->item['item_meta_array'] ) ) {
118
			foreach ( $this->item['item_meta_array'] as $meta_id => $meta ) {
119
				if ( "" === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) {
120
					continue;
121
				}
122
123
				$attribute_key = urldecode( str_replace( 'attribute_', '', $meta->key ) );
124
				$meta_value    = $meta->value;
125
126
				// If this is a term slug, get the term's nice name
127 View Code Duplication
				if ( taxonomy_exists( $attribute_key ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
					$term = get_term_by( 'slug', $meta_value, $attribute_key );
129
130
					if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
131
						$meta_value = $term->name;
132
					}
133
				}
134
135
				$formatted_meta[ $meta_id ] = array(
136
					'key'   => $meta->key,
137
					'label' => wc_attribute_label( $attribute_key, $this->product ),
138
					'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),
139
				);
140
			}
141
		}
142
143
		return apply_filters( 'woocommerce_order_items_meta_get_formatted', $formatted_meta, $this );
144
	}
145
146
	/**
147
	 * Return an array of formatted item meta in format e.g.
148
	 * Handles @deprecated args.
149
	 * @return array
150
	 */
151
	public function get_formatted_legacy( $hideprefix = '_' ) {
152
		if ( ! is_ajax() ) {
153
			_deprecated_function( 'get_formatted_legacy', '2.4', 'Item Meta Data is being called with legacy arguments' );
154
		}
155
156
		$formatted_meta = array();
157
158
		foreach ( $this->meta as $meta_key => $meta_values ) {
159
			if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) ) {
160
				continue;
161
			}
162
			foreach ( (array) $meta_values as $meta_value ) {
163
				// Skip serialised meta
164
				if ( is_serialized( $meta_value ) ) {
165
					continue;
166
				}
167
168
				$attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) );
169
170
				// If this is a term slug, get the term's nice name
171 View Code Duplication
				if ( taxonomy_exists( $attribute_key ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
					$term = get_term_by( 'slug', $meta_value, $attribute_key );
173
					if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
174
						$meta_value = $term->name;
175
					}
176
				}
177
178
				// Unique key required
179
				$formatted_meta_key = $meta_key;
180
				$loop               = 0;
181
				while ( isset( $formatted_meta[ $formatted_meta_key ] ) ) {
182
					$loop ++;
183
					$formatted_meta_key = $meta_key . '-' . $loop;
184
				}
185
186
				$formatted_meta[ $formatted_meta_key ] = array(
187
					'key'   => $meta_key,
188
					'label' => wc_attribute_label( $attribute_key, $this->product ),
189
					'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),
190
				);
191
			}
192
		}
193
194
		return $formatted_meta;
195
	}
196
}
197