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 |
||
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 ) { |
|
36 | |||
37 | /** |
||
38 | * Type checking |
||
39 | * @param string|array $Type |
||
40 | * @return boolean |
||
41 | */ |
||
42 | public function is_type( $type ) { |
||
45 | |||
46 | /** |
||
47 | * Get item type. |
||
48 | * @return string |
||
49 | */ |
||
50 | public function get_type() { |
||
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 ) { |
|
82 | |||
83 | /** |
||
84 | * offsetUnset for ArrayAccess |
||
85 | * @param string $offset |
||
86 | */ |
||
87 | View Code Duplication | public function offsetUnset( $offset ) { |
|
99 | |||
100 | /** |
||
101 | * offsetExists for ArrayAccess |
||
102 | * @param string $offset |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function offsetExists( $offset ) { |
||
111 | |||
112 | /** |
||
113 | * Get legacy item_meta_array. |
||
114 | * @return arrau |
||
115 | */ |
||
116 | private function get_item_meta_array() { |
||
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 ) { |
||
145 | } |
||
146 |
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.