Complex classes like WC_Structured_Data 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_Structured_Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WC_Structured_Data { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var null|array $_data |
||
| 20 | */ |
||
| 21 | private $_data; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sets `$this->_data`. |
||
| 42 | * |
||
| 43 | * @param array $data |
||
| 44 | * @param bool $reset (default: false) |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public function set_data( $data, $reset = false ) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Gets `$this->_data`. |
||
| 65 | * |
||
| 66 | * @return array $data |
||
| 67 | */ |
||
| 68 | public function get_data() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Structures and returns data. |
||
| 74 | * |
||
| 75 | * List of types available by default for specific request: |
||
| 76 | * |
||
| 77 | * 'product', |
||
| 78 | * 'review', |
||
| 79 | * 'breadcrumblist', |
||
| 80 | * 'website', |
||
| 81 | * 'order', |
||
| 82 | * |
||
| 83 | * @param bool|array|string $requested_types (default: false) |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function get_structured_data( $requested_types = false ) { |
||
| 87 | $data = $this->get_data(); |
||
| 88 | |||
| 89 | if ( empty( $data ) || ( $requested_types && ! is_array( $requested_types ) && ! is_string( $requested_types ) || is_null( $requested_types ) ) ) { |
||
| 90 | return array(); |
||
| 91 | } elseif ( $requested_types && is_string( $requested_types ) ) { |
||
| 92 | $requested_types = array( $requested_types ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Puts together the values of same type of structured data. |
||
| 96 | foreach ( $data as $value ) { |
||
| 97 | $structured_data[ strtolower( $value['@type'] ) ][] = $value; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Wraps the multiple values of each type of structured data inside a graph. Then adds context to each type of value. |
||
| 101 | foreach ( $structured_data as $type => $value ) { |
||
| 102 | $structured_data[ $type ] = count( $value ) > 1 ? array( '@graph' => $value ) : $value[0]; |
||
| 103 | $structured_data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'http://schema.org/' ), $structured_data, $type, $value ) + $structured_data[ $type ]; |
||
| 104 | } |
||
| 105 | |||
| 106 | // If requested types, picks them up. Finally changes the associative array to an indexed one. |
||
| 107 | $structured_data = $requested_types ? array_values( array_intersect_key( $structured_data, array_flip( $requested_types ) ) ) : array_values( $structured_data ); |
||
| 108 | |||
| 109 | if ( ! empty( $structured_data ) ) { |
||
| 110 | $structured_data = count( $structured_data ) > 1 ? array( '@graph' => $structured_data ) : $structured_data[0]; |
||
| 111 | } |
||
| 112 | |||
| 113 | return $structured_data; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sanitizes, encodes and outputs structured data. |
||
| 118 | * |
||
| 119 | * @uses `wp_footer` action hook |
||
| 120 | * @uses `woocommerce_email_order_details` action hook |
||
| 121 | * @param bool|array|string $requested_types (default: true) |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function output_structured_data( $requested_types = true ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Sanitizes data. |
||
| 146 | * |
||
| 147 | * @param array $data |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function sanitize_data( $data ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Generates, sanitizes, encodes and outputs specific structured data type. |
||
| 164 | * |
||
| 165 | * @param string $type |
||
| 166 | * @param mixed $object (default: null) |
||
| 167 | * @param mixed $param_1 (default: null) |
||
| 168 | * @param mixed $param_2 (default: null) |
||
| 169 | * @param mixed $param_3 (default: null) |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function generate_output_structured_data( $type, $object = null, $param_1 = null, $param_2 = null, $param_3 = null ) { |
||
| 185 | |||
| 186 | /* |
||
| 187 | |-------------------------------------------------------------------------- |
||
| 188 | | Generators |
||
| 189 | |-------------------------------------------------------------------------- |
||
| 190 | | |
||
| 191 | | Methods for generating specific structured data types: |
||
| 192 | | |
||
| 193 | | - Product |
||
| 194 | | - Review |
||
| 195 | | - BreadcrumbList |
||
| 196 | | - WebSite |
||
| 197 | | - Order |
||
| 198 | | |
||
| 199 | | The generated data is stored into `$this->_data`. |
||
| 200 | | See the methods above for handling `$this->_data`. |
||
| 201 | | |
||
| 202 | */ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Generates Product structured data. |
||
| 206 | * |
||
| 207 | * @uses `woocommerce_single_product_summary` action hook |
||
| 208 | * @uses `woocommerce_shop_loop` action hook |
||
| 209 | * @param bool|object $product (default: false) |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function generate_product_data( $product = false ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Generates Review structured data. |
||
| 270 | * |
||
| 271 | * @uses `woocommerce_review_meta` action hook |
||
| 272 | * @param object $comment |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function generate_review_data( $comment ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Generates BreadcrumbList structured data. |
||
| 302 | * |
||
| 303 | * @uses `woocommerce_breadcrumb` action hook |
||
| 304 | * @param object $breadcrumbs |
||
| 305 | * @return bool|void |
||
| 306 | */ |
||
| 307 | public function generate_breadcrumblist_data( $breadcrumbs ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Generates WebSite structured data. |
||
| 336 | * |
||
| 337 | * @uses `woocommerce_before_main_content` action hook |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function generate_website_data() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Generates Order structured data. |
||
| 355 | * |
||
| 356 | * @uses `woocommerce_email_order_details` action hook |
||
| 357 | * @param object $order |
||
| 358 | * @param bool $sent_to_admin (default: false) |
||
| 359 | * @param bool $plain_text (default: false) |
||
| 360 | * @return bool|void |
||
| 361 | */ |
||
| 362 | public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) { |
||
| 474 | } |
||
| 475 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.