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:
Complex classes like WC_Order_Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Order_Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WC_Order_Item implements ArrayAccess, WC_Data { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Data array, with defaults. |
||
| 18 | * @since 2.6.0 |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $_data = array( |
||
| 22 | 'order_id' => 0, |
||
| 23 | 'order_item_id' => 0, |
||
| 24 | 'name' => '', |
||
| 25 | 'type' => '', |
||
| 26 | 'meta_data' => array(), |
||
| 27 | ); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * offsetSet for ArrayAccess |
||
| 31 | * @param string $offset |
||
| 32 | * @param mixed $value |
||
| 33 | */ |
||
| 34 | public function offsetSet( $offset, $value ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * offsetUnset for ArrayAccess |
||
| 43 | * @param string $offset |
||
| 44 | */ |
||
| 45 | public function offsetUnset( $offset ) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * offsetExists for ArrayAccess |
||
| 54 | * @param string $offset |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function offsetExists( $offset ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * offsetGet for ArrayAccess |
||
| 66 | * @param string $offset |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function offsetGet( $offset ) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructor. |
||
| 82 | * @param int|object $order_item ID to load from the DB (optional) or already queried data. |
||
|
|
|||
| 83 | */ |
||
| 84 | public function __construct( $item = 0 ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set data based on input item. |
||
| 96 | * @access private |
||
| 97 | */ |
||
| 98 | private function set_all( $item ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Change data to JSON format. |
||
| 106 | * @return string Data in JSON format. |
||
| 107 | */ |
||
| 108 | public function __toString() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Type checking |
||
| 114 | * @param string $Type |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | public function is_type( $type ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Internal meta keys we don't want exposed as part of meta_data. |
||
| 123 | * @return array() |
||
| 124 | */ |
||
| 125 | protected function get_internal_meta_keys() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get qty. |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | public function get_qty() { |
||
| 136 | |||
| 137 | /* |
||
| 138 | |-------------------------------------------------------------------------- |
||
| 139 | | Getters |
||
| 140 | |-------------------------------------------------------------------------- |
||
| 141 | */ |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get all class data in array format. |
||
| 145 | * @since 2.6.0 |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function get_data() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get order ID this meta belongs to. |
||
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | public function get_id() { |
||
| 157 | return absint( $this->_data['order_id'] ); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get order item ID. |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function get_order_item_id() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get order item name. |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function get_name() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get order item type. |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function get_type() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get meta data |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function get_meta_data() { |
||
| 191 | |||
| 192 | /* |
||
| 193 | |-------------------------------------------------------------------------- |
||
| 194 | | Setters |
||
| 195 | |-------------------------------------------------------------------------- |
||
| 196 | */ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set order ID. |
||
| 200 | * @param int $value |
||
| 201 | */ |
||
| 202 | public function set_order_id( $value ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set order item ID. |
||
| 208 | * @param int $value |
||
| 209 | */ |
||
| 210 | public function set_order_item_id( $value ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set order item name. |
||
| 216 | * @param string $value |
||
| 217 | */ |
||
| 218 | public function set_name( $value ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set order item type. |
||
| 224 | * @param string $value |
||
| 225 | */ |
||
| 226 | public function set_type( $value ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set meta data. |
||
| 233 | * @param array $data Key/Value pairs |
||
| 234 | */ |
||
| 235 | public function set_meta_data( $data ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set meta data. |
||
| 243 | * @param array $data Key/Value pairs |
||
| 244 | */ |
||
| 245 | public function add_meta_data( $key, $value, $unique = false ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Update meta data by key or ID, if provided. |
||
| 258 | * @param string $key |
||
| 259 | * @param string $value |
||
| 260 | * @param int $meta_id |
||
| 261 | */ |
||
| 262 | public function update_meta_data( $key, $value, $meta_id = '' ) { |
||
| 272 | |||
| 273 | /* |
||
| 274 | |-------------------------------------------------------------------------- |
||
| 275 | | CRUD methods |
||
| 276 | |-------------------------------------------------------------------------- |
||
| 277 | | |
||
| 278 | | Methods which create, read, update and delete data from the database. |
||
| 279 | | |
||
| 280 | */ |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Insert data into the database. |
||
| 284 | * @since 2.6.0 |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function create() { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Update data in the database. |
||
| 298 | * @since 2.6.0 |
||
| 299 | */ |
||
| 300 | View Code Duplication | public function update() { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Read from the database. |
||
| 311 | * @since 2.6.0 |
||
| 312 | * @param int|object $item ID of object to read, or already queried object. |
||
| 313 | */ |
||
| 314 | public function read( $item ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Read Meta Data from the database. Ignore any internal properties. |
||
| 336 | */ |
||
| 337 | protected function read_meta_data() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Save data to the database. |
||
| 362 | * @since 2.6.0 |
||
| 363 | */ |
||
| 364 | public function save() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Delete data from the database. |
||
| 374 | * @since 2.6.0 |
||
| 375 | */ |
||
| 376 | public function delete() { |
||
| 380 | } |
||
| 381 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.