Completed
Pull Request — master (#11889)
by Mike
12:12
created

WC_Item   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 129
Duplicated Lines 32.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 42
loc 129
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 4
A is_type() 0 3 2
A get_type() 0 3 1
A offsetSet() 14 14 4
A offsetUnset() 12 12 4
A offsetExists() 3 6 4
A get_item_meta_array() 0 7 2
B offsetGet() 0 15 5

How to fix   Duplicated Code   

Duplicated Code

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
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Line Item
8
 *
9
 * A class which represents an item within an order or cart.
10
 * Uses ArrayAccess to be BW compatible with previously public properties.
11
 *
12
 * @version     2.7.0
13
 * @since       2.7.0
14
 * @package     WooCommerce/Classes
15
 * @author      WooThemes
16
 */
17
class WC_Item extends WC_Data implements ArrayAccess {
18
19
	/**
20
	 * Constructor.
21
	 * @param int|object|array $read ID to load from the DB (optional) or already queried data.
22
	 */
23 View Code Duplication
	public function __construct( $read = 0 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
24
		parent::__construct( $read );
25
26
		if ( $read instanceof WC_Item ) {
27
			if ( $this->is_type( $read->get_type() ) ) {
28
				$this->set_props( $read->get_data() );
29
			}
30
		} elseif ( is_array( $read ) ) {
31
			$this->set_props( $read );
32
		} else {
33
			$this->read( $read );
34
		}
35
	}
36
37
	/**
38
	 * Type checking
39
	 * @param  string|array  $Type
0 ignored issues
show
Bug introduced by
There is no parameter named $Type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
	 * @return boolean
41
	 */
42
	public function is_type( $type ) {
43
		return is_array( $type ) ? in_array( $this->get_type(), $type ) : $type === $this->get_type();
44
	}
45
46
	/**
47
	 * Get item type.
48
	 * @return string
49
	 */
50
	public function get_type() {
51
		return '';
52
	}
53
54
	/*
55
	|--------------------------------------------------------------------------
56
	| Array Access Methods
57
	|--------------------------------------------------------------------------
58
	|
59
	| For backwards compat with legacy arrays.
60
	|
61
	*/
62
63
	/**
64
	 * offsetSet for ArrayAccess
65
	 * @param string $offset
66
	 * @param mixed $value
67
	 */
68 View Code Duplication
	public function offsetSet( $offset, $value ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
		if ( 'item_meta_array' === $offset ) {
70
			foreach ( $value as $meta_id => $meta ) {
71
				$this->update_meta_data( $meta->key, $meta->value, $meta_id );
72
			}
73
			return;
74
		}
75
76
		if ( array_key_exists( $offset, $this->data ) ) {
77
			$this->data[ $offset ] = $value;
78
		}
79
80
		$this->update_meta_data( '_' . $offset, $value );
81
	}
82
83
	/**
84
	 * offsetUnset for ArrayAccess
85
	 * @param string $offset
86
	 */
87 View Code Duplication
	public function offsetUnset( $offset ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
		if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) {
89
			$this->meta_data = array();
90
			return;
91
		}
92
93
		if ( array_key_exists( $offset, $this->data ) ) {
94
			unset( $this->data[ $offset ] );
95
		}
96
97
		$this->delete_meta_data( '_' . $offset );
0 ignored issues
show
Documentation introduced by
'_' . $offset is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
	}
99
100
	/**
101
	 * offsetExists for ArrayAccess
102
	 * @param string $offset
103
	 * @return bool
104
	 */
105
	public function offsetExists( $offset ) {
106 View Code Duplication
		if ( 'item_meta_array' === $offset || 'item_meta' === $offset || array_key_exists( $offset, $this->data ) ) {
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...
107
			return true;
108
		}
109
		return array_key_exists( '_' . $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) );
110
	}
111
112
	/**
113
	 * Get legacy item_meta_array.
114
	 * @return arrau
115
	 */
116
	private function get_item_meta_array() {
117
		$return = array();
118
		foreach ( $this->meta_data as $meta ) {
119
			$return[ $meta->id ] = $meta;
120
		}
121
		return $return;
122
	}
123
124
	/**
125
	 * offsetGet for ArrayAccess.
126
	 * Item meta was expanded in previous versions, with prefixes removed. This maintains support.
127
	 * @param string $offset
128
	 * @return mixed
129
	 */
130
	public function offsetGet( $offset ) {
131
		$meta_values = wp_list_pluck( $this->meta_data, 'value', 'key' );
132
133
		if ( 'item_meta_array' === $offset ) {
134
			return $this->get_item_meta_array();
135
		} elseif ( 'item_meta' === $offset ) {
136
			return $meta_values;
137
		} elseif ( array_key_exists( $offset, $this->data ) ) {
138
			return $this->data[ $offset ];
139
		} elseif ( array_key_exists( '_' . $offset, $meta_values ) ) {
140
			return $meta_values[ '_' . $offset ];
141
		}
142
143
		return null;
144
	}
145
}
146