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 | * Data array, with defaults. |
||
| 21 | * @since 2.7.0 |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $_data = array( |
||
| 25 | 'order_id' => 0, |
||
| 26 | 'order_item_id' => 0, |
||
| 27 | 'name' => '', |
||
| 28 | 'type' => '', |
||
| 29 | ); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Stores meta in cache for future reads. |
||
| 33 | * A group must be set to to enable caching. |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $_cache_group = 'order_itemmeta'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Meta type. This should match up with |
||
| 40 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. |
||
| 41 | * WP defines 'post', 'user', 'comment', and 'term'. |
||
| 42 | */ |
||
| 43 | protected $_meta_type = 'order_item'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor. |
||
| 47 | * @param int|object|array $order_item ID to load from the DB (optional) or already queried data. |
||
|
|
|||
| 48 | */ |
||
| 49 | public function __construct( $item = 0 ) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Set all data based on input array. |
||
| 63 | * @param array $data |
||
| 64 | * @access private |
||
| 65 | */ |
||
| 66 | public function set_all( $data ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Type checking |
||
| 78 | * @param string|array $Type |
||
| 79 | * @return boolean |
||
| 80 | */ |
||
| 81 | public function is_type( $type ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get qty. |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | public function get_qty() { |
||
| 92 | |||
| 93 | /* |
||
| 94 | |-------------------------------------------------------------------------- |
||
| 95 | | Getters |
||
| 96 | |-------------------------------------------------------------------------- |
||
| 97 | */ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get order item ID. |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function get_id() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get order ID this meta belongs to. |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | public function get_order_id() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get order item ID this meta belongs to. |
||
| 117 | * @return int |
||
| 118 | */ |
||
| 119 | public function get_order_item_id() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get order item name. |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function get_name() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get order item type. |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function get_type() { |
||
| 138 | |||
| 139 | /* |
||
| 140 | |-------------------------------------------------------------------------- |
||
| 141 | | Setters |
||
| 142 | |-------------------------------------------------------------------------- |
||
| 143 | */ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set ID |
||
| 147 | * @param int $value |
||
| 148 | */ |
||
| 149 | public function set_id( $value ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set order ID. |
||
| 155 | * @param int $value |
||
| 156 | */ |
||
| 157 | public function set_order_id( $value ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Set order item ID. |
||
| 163 | * @param int $value |
||
| 164 | */ |
||
| 165 | public function set_order_item_id( $value ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set order item name. |
||
| 171 | * @param string $value |
||
| 172 | */ |
||
| 173 | public function set_name( $value ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set order item type. |
||
| 179 | * @param string $value |
||
| 180 | */ |
||
| 181 | public function set_type( $value ) { |
||
| 185 | |||
| 186 | /* |
||
| 187 | |-------------------------------------------------------------------------- |
||
| 188 | | CRUD methods |
||
| 189 | |-------------------------------------------------------------------------- |
||
| 190 | | |
||
| 191 | | Methods which create, read, update and delete data from the database. |
||
| 192 | | |
||
| 193 | */ |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Insert data into the database. |
||
| 197 | * @since 2.7.0 |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function create() { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Update data in the database. |
||
| 214 | * @since 2.7.0 |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function update() { |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Read from the database. |
||
| 230 | * @since 2.7.0 |
||
| 231 | * @param int|object $item ID of object to read, or already queried object. |
||
| 232 | */ |
||
| 233 | public function read( $item ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Save data to the database. |
||
| 255 | * @since 2.7.0 |
||
| 256 | * @return int Item ID |
||
| 257 | */ |
||
| 258 | public function save() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Delete data from the database. |
||
| 271 | * @since 2.7.0 |
||
| 272 | */ |
||
| 273 | public function delete() { |
||
| 282 | |||
| 283 | /* |
||
| 284 | |-------------------------------------------------------------------------- |
||
| 285 | | Meta Data Handling |
||
| 286 | |-------------------------------------------------------------------------- |
||
| 287 | */ |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Expands things like term slugs before return. |
||
| 291 | * @param string $hideprefix (default: _) |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
| 324 | |||
| 325 | /* |
||
| 326 | |-------------------------------------------------------------------------- |
||
| 327 | | Array Access Methods |
||
| 328 | |-------------------------------------------------------------------------- |
||
| 329 | | |
||
| 330 | | For backwards compat with legacy arrays. |
||
| 331 | | |
||
| 332 | */ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * offsetSet for ArrayAccess |
||
| 336 | * @param string $offset |
||
| 337 | * @param mixed $value |
||
| 338 | */ |
||
| 339 | public function offsetSet( $offset, $value ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * offsetUnset for ArrayAccess |
||
| 356 | * @param string $offset |
||
| 357 | */ |
||
| 358 | public function offsetUnset( $offset ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * offsetExists for ArrayAccess |
||
| 373 | * @param string $offset |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function offsetExists( $offset ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * offsetGet for ArrayAccess |
||
| 385 | * @param string $offset |
||
| 386 | * @return mixed |
||
| 387 | */ |
||
| 388 | public function offsetGet( $offset ) { |
||
| 412 | } |
||
| 413 |
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.