Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

Jetpack_WooCommerce_Analytics_Utils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 75 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 24
loc 32
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C get_product_categories_concatenated() 24 24 8

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
 * Jetpack_WooCommerce_Analytics_Options provides a single interface to module options
4
 *
5
 * @package Jetpack
6
 * @author Automattic
7
 */
8
9
/**
10
 * Bail if accessed directly
11
 */
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Class Jetpack_WooCommerce_Analytics_Utils
18
 * Utility function for WooCommerce Analytics
19
 */
20
class Jetpack_WooCommerce_Analytics_Utils {
21
	/**
22
	 * Gets product categories or varation attributes as a formatted concatenated string
23
	 *
24
	 * @param array $product WC_Product.
25
	 * @return string
26
	 */
27 View Code Duplication
	public static function get_product_categories_concatenated( $product ) {
28
		if ( ! class_exists( 'WooCommerce' ) ) {
29
			return '';
30
		}
31
32
		if ( ! $product ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $product of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
33
			return '';
34
		}
35
36
		$variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
0 ignored issues
show
Bug introduced by
The method is_type cannot be called on $product (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method get_id cannot be called on $product (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
37
		if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
38
			$line = wc_get_formatted_variation( $variation_data, true );
39
		} else {
40
			$out        = array();
41
			$categories = get_the_terms( $product->get_id(), 'product_cat' );
0 ignored issues
show
Bug introduced by
The method get_id cannot be called on $product (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
42
			if ( $categories ) {
43
				foreach ( $categories as $category ) {
44
					$out[] = $category->name;
45
				}
46
			}
47
			$line = join( '/', $out );
48
		}
49
		return $line;
50
	}
51
}
52