Completed
Pull Request — develop (#1347)
by David
04:57 queued 01:46
created

Offer_Structured_Data   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 3
loc 42
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B entity_jsonld() 3 20 6

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
namespace Wordlift\Shipping_Data;
4
5
class Offer_Structured_Data {
6
7
	/**
8
	 * @var Shipping_Zones
9
	 */
10
	private $shipping_zones;
11
12
	/**
13
	 * Offer_Structured_Data constructor.
14
	 *
15
	 * @param Shipping_Zones $shipping_zones
16
	 */
17
	public function __construct( $shipping_zones ) {
18
19
		$this->shipping_zones = $shipping_zones;
20
21
		add_filter( 'wl_entity_jsonld', array( $this, 'entity_jsonld' ), 10, 2 );
22
23
	}
24
25
	public function entity_jsonld( $jsonld, $post_id ) {
26
27
		// Bail out if it's not a Product or the offers property isn't set.
28
		if ( ! in_array( 'Product', (array) $jsonld['@type'] ) || ! isset( $jsonld['offers'] ) ) {
29
			return $jsonld;
30
		}
31
32 View Code Duplication
		if ( ! is_array( $jsonld['offers'] ) || ! is_numeric( key( $jsonld['offers'] ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
			$jsonld['offers'] = array( $jsonld['offers'] );
34
		}
35
36
		$product = new Product( $post_id );
37
38
		foreach ( $jsonld['offers'] as &$offer ) {
39
			$this->shipping_zones->add_available_delivery_method( $offer );
40
			$this->shipping_zones->add_offer_shipping_details( $offer, $product );
41
		}
42
43
		return $jsonld;
44
	}
45
46
}
47