Completed
Push — master ( ba5b71...784334 )
by Leon
02:11
created

woocommerce-template.php ➔ is_discontinued()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
/**
3
 * WooCommerce Discontinued Products Template Functions
4
 *
5
 * @package woocommerce
6
 * @since 1.0.0
7
 */
8
9
if ( ! function_exists( 'dp_is_discontinued' ) ) {
10
11
	/**
12
	 * Is discontiued.
13
	 * Check if product is discontinued.
14
	 *
15
	 * @since 1.0.0
16
	 * @param int $product_id Optional. ID of the product to check.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $product_id not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
17
	 */
18
	function dp_is_discontinued( $product_id = null ) {
0 ignored issues
show
Coding Style introduced by
function dp_is_discontinued() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
20
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
21
		$product_id = $product_id !== null ? $product_id : $post->ID;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
		$is_discontinued = get_post_meta( $product_id, '_is_discontinued', true );
23
		return $is_discontinued === 'yes';
24
	}
25
}
26
27
if ( ! function_exists( 'dp_alternative_products' ) ) {
28
29
	/**
30
	 * Alternative Products.
31
	 * Output buttons to alternative products.
32
	 *
33
	 * @since 1.0.0
34
	 */
35
	function dp_alternative_products() {
36
37
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
38
		$alt_products = get_post_meta( $post->ID, '_alt_products', true );
39
		$alt_products = is_array( $alt_products ) ? $alt_products : array();
40
		$text = _( 'This product has been discontinued.' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
		$notice = empty( $alt_products ) ? $text : $text . ' ' . _( 'You may be interested in:' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42
		?>
43
		<h4><?php echo esc_html( $notice ); ?></h4>
44
		<?php
45
		foreach ( $alt_products as $alt_product ) {
46
			?>
47
			<a href="<?php echo esc_url( get_permalink( $alt_product ) ); ?>" class="button"><?php echo get_the_title( $alt_product ); ?></a>
48
			<?php
49
		}
50
	}
51
}
52