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 |
||
| 17 | class WC_Order_Item extends WC_Data implements ArrayAccess { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Order Data array. This is the core order data exposed in APIs since 2.7.0. |
||
| 21 | * @since 2.7.0 |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $_data = array( |
||
| 25 | 'order_id' => 0, |
||
| 26 | 'id' => 0, // order_item_id |
||
| 27 | 'name' => '', |
||
| 28 | 'type' => '', |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * May store an order to prevent retriving it multiple times. |
||
| 33 | * @var object |
||
| 34 | */ |
||
| 35 | protected $_order; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Stores meta in cache for future reads. |
||
| 39 | * A group must be set to to enable caching. |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $_cache_group = 'order_itemmeta'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Meta type. This should match up with |
||
| 46 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. |
||
| 47 | * WP defines 'post', 'user', 'comment', and 'term'. |
||
| 48 | */ |
||
| 49 | protected $_meta_type = 'order_item'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor. |
||
| 53 | * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
||
| 54 | */ |
||
| 55 | public function __construct( $read = 0 ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Type checking |
||
| 71 | * @param string|array $Type |
||
|
|
|||
| 72 | * @return boolean |
||
| 73 | */ |
||
| 74 | public function is_type( $type ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get quantity. |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function get_quantity() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get parent order object. |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | public function get_order() { |
||
| 96 | |||
| 97 | /* |
||
| 98 | |-------------------------------------------------------------------------- |
||
| 99 | | Getters |
||
| 100 | |-------------------------------------------------------------------------- |
||
| 101 | */ |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get order item ID. |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | public function get_id() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get order ID this meta belongs to. |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function get_order_id() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get order item name. |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function get_name() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get order item type. |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function get_type() { |
||
| 134 | |||
| 135 | /* |
||
| 136 | |-------------------------------------------------------------------------- |
||
| 137 | | Setters |
||
| 138 | |-------------------------------------------------------------------------- |
||
| 139 | */ |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set ID |
||
| 143 | * @param int $value |
||
| 144 | * @throws WC_Data_Exception |
||
| 145 | */ |
||
| 146 | public function set_id( $value ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set order ID. |
||
| 152 | * @param int $value |
||
| 153 | * @throws WC_Data_Exception |
||
| 154 | */ |
||
| 155 | public function set_order_id( $value ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set order item name. |
||
| 161 | * @param string $value |
||
| 162 | * @throws WC_Data_Exception |
||
| 163 | */ |
||
| 164 | public function set_name( $value ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set order item type. |
||
| 170 | * @param string $value |
||
| 171 | * @throws WC_Data_Exception |
||
| 172 | */ |
||
| 173 | protected function set_type( $value ) { |
||
| 176 | |||
| 177 | /* |
||
| 178 | |-------------------------------------------------------------------------- |
||
| 179 | | CRUD methods |
||
| 180 | |-------------------------------------------------------------------------- |
||
| 181 | | |
||
| 182 | | Methods which create, read, update and delete data from the database. |
||
| 183 | | |
||
| 184 | */ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Insert data into the database. |
||
| 188 | * @since 2.7.0 |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function create() { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Update data in the database. |
||
| 205 | * @since 2.7.0 |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function update() { |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Read from the database. |
||
| 221 | * @since 2.7.0 |
||
| 222 | * @param int|object $item ID of object to read, or already queried object. |
||
| 223 | */ |
||
| 224 | public function read( $item ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Save data to the database. |
||
| 252 | * @since 2.7.0 |
||
| 253 | * @return int Item ID |
||
| 254 | */ |
||
| 255 | public function save() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Delete data from the database. |
||
| 268 | * @since 2.7.0 |
||
| 269 | */ |
||
| 270 | public function delete() { |
||
| 279 | |||
| 280 | /* |
||
| 281 | |-------------------------------------------------------------------------- |
||
| 282 | | Meta Data Handling |
||
| 283 | |-------------------------------------------------------------------------- |
||
| 284 | */ |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Expands things like term slugs before return. |
||
| 288 | * @param string $hideprefix (default: _) |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
| 323 | |||
| 324 | /* |
||
| 325 | |-------------------------------------------------------------------------- |
||
| 326 | | Array Access Methods |
||
| 327 | |-------------------------------------------------------------------------- |
||
| 328 | | |
||
| 329 | | For backwards compat with legacy arrays. |
||
| 330 | | |
||
| 331 | */ |
||
| 332 | |||
| 333 | /** |
||
| 334 | * offsetSet for ArrayAccess |
||
| 335 | * @param string $offset |
||
| 336 | * @param mixed $value |
||
| 337 | */ |
||
| 338 | public function offsetSet( $offset, $value ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * offsetUnset for ArrayAccess |
||
| 355 | * @param string $offset |
||
| 356 | */ |
||
| 357 | public function offsetUnset( $offset ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * offsetExists for ArrayAccess |
||
| 372 | * @param string $offset |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function offsetExists( $offset ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * offsetGet for ArrayAccess |
||
| 384 | * @param string $offset |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | public function offsetGet( $offset ) { |
||
| 411 | } |
||
| 412 |
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.