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 array Partially structured data |
||
| 20 | */ |
||
| 21 | private $data; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Checks if the passed $json variable is an array and stores it into $this->data... |
||
| 25 | * |
||
| 26 | * @param array $json Partially structured data |
||
| 27 | * @return bool Returns false If the param $json is not an array |
||
| 28 | */ |
||
| 29 | private function set_data( $json ) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Structures and returns the data... |
||
| 39 | * |
||
| 40 | * @return array If data is set, returns the structured data, otherwise returns empty array |
||
| 41 | */ |
||
| 42 | private function get_data() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Contructor |
||
| 86 | */ |
||
| 87 | public function __construct() { |
||
| 88 | add_action( 'woocommerce_before_main_content', array( $this, 'generate_shop_data' ) ); |
||
| 89 | add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumb_data' ), 10, 1 ); |
||
| 90 | add_action( 'woocommerce_before_shop_loop_item', array( $this, 'generate_product_category_data' ) ); |
||
| 91 | add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ) ); |
||
| 92 | add_action( 'woocommerce_review_meta', array( $this, 'generate_product_review_data' ), 10, 1 ); |
||
| 93 | add_action( 'wp_footer', array( $this, 'enqueue_data' ) ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Sanitizes, encodes and echoes the structured data into `wp_footer` action hook. |
||
| 98 | */ |
||
| 99 | public function enqueue_data() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Function for sanitizing the structured data. |
||
| 110 | * |
||
| 111 | * @param array $data |
||
| 112 | * @return array $sanitized_data |
||
| 113 | */ |
||
| 114 | private function sanitize_data( $data ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Generates the product category structured data... |
||
| 124 | * Hooked into the `woocommerce_before_shop_loop_item` action hook... |
||
| 125 | */ |
||
| 126 | public function generate_product_category_data() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Generates the product structured data... |
||
| 136 | * Hooked into the `woocommerce_single_product_summary` action hook... |
||
| 137 | * Applies the `woocommerce_structured_data_product` filter hook for clean structured data customization... |
||
| 138 | */ |
||
| 139 | public function generate_product_data() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Generates the product review structured data... |
||
| 187 | * Hooked into the `woocommerce_review_meta` action hook... |
||
| 188 | * Applies the `woocommerce_structured_data_product_review` filter hook for clean structured data customization... |
||
| 189 | * |
||
| 190 | * @param object $comment From `woocommerce_review_meta` action hook |
||
| 191 | */ |
||
| 192 | public function generate_product_review_data( $comment ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Generates the breadcrumbs structured data... |
||
| 212 | * Hooked into the `woocommerce_breadcrumb` action hook... |
||
| 213 | * Applies the `woocommerce_structured_data_breadcrumb` filter hook for clean structured data customization... |
||
| 214 | * |
||
| 215 | * @param array $args From `woocommerce_breadcrumb` action hook |
||
| 216 | */ |
||
| 217 | public function generate_breadcrumb_data( $args ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Generates the shop related structured data... |
||
| 252 | * Hooked into the `woocommerce_before_main_content` action hook... |
||
| 253 | * Applies the `woocommerce_structured_data_shop` filter hook for clean structured data customization... |
||
| 254 | */ |
||
| 255 | public function generate_shop_data() { |
||
| 271 | } |
||
| 272 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.