Completed
Pull Request — master (#10915)
by Mike
07:30
created

WC_Product_Factory::get_product()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 24
rs 8.5125
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Product Factory Class
9
 *
10
 * The WooCommerce product factory creating the right product object.
11
 *
12
 * @class 		WC_Product_Factory
13
 * @version		2.3.0
14
 * @package		WooCommerce/Classes
15
 * @category	Class
16
 * @author 		WooThemes
17
 */
18
class WC_Product_Factory {
19
20
	/**
21
	 * Get a product.
22
	 *
23
	 * @param bool $the_product (default: false)
24
	 * @param array $args (default: array())
25
	 * @return WC_Product|bool false if the product cannot be loaded
26
	 */
27
	public function get_product( $the_product = false, $args = array() ) {
28
		try {
29
			$the_product = $this->get_product_object( $the_product );
30
31
			if ( ! $the_product ) {
32
				throw new Exception( 'Product object does not exist', 422 );
33
			}
34
35
			$classname = $this->get_product_class( $the_product, $args );
0 ignored issues
show
Bug introduced by
It seems like $the_product defined by $this->get_product_object($the_product) on line 29 can also be of type boolean; however, WC_Product_Factory::get_product_class() does only seem to accept object<WP_Post>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
37
			if ( ! $classname ) {
38
				throw new Exception( 'Missing classname', 422 );
39
			}
40
41
			if ( ! class_exists( $classname ) ) {
42
				$classname = 'WC_Product_Simple';
43
			}
44
45
			return new $classname( $the_product, $args );
46
47
		} catch ( Exception $e ) {
48
			return false;
49
		}
50
	}
51
52
	/**
53
	 * Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class.
54
	 * @param  string $product_type
55
	 * @return string|false
56
	 */
57
	private function get_classname_from_product_type( $product_type ) {
58
		return $product_type ? 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ) : false;
59
	}
60
61
	/**
62
	 * Get the product class name.
63
	 * @param  WP_Post $the_product
64
	 * @param  array $args (default: array())
65
	 * @return string
66
	 */
67
	private function get_product_class( $the_product, $args = array() ) {
68
		$product_id = absint( $the_product->ID );
69
		$post_type  = $the_product->post_type;
70
71
		if ( 'product' === $post_type ) {
72
			if ( isset( $args['product_type'] ) ) {
73
				$product_type = $args['product_type'];
74
			} else {
75
				$terms        = get_the_terms( $the_product, 'product_type' );
76
				$product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
77
			}
78
		} elseif( 'product_variation' === $post_type ) {
79
			$product_type = 'variation';
80
		} else {
81
			$product_type = false;
82
		}
83
84
		$classname = $this->get_classname_from_product_type( $product_type );
85
86
		// Filter classname so that the class can be overridden if extended.
87
		return apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
88
	}
89
90
	/**
91
	 * Get the product object.
92
	 * @param  mixed $the_product
93
	 * @uses   WP_Post
94
	 * @return WP_Post|bool false on failure
95
	 */
96
	private function get_product_object( $the_product ) {
97
		if ( false === $the_product ) {
98
			$the_product = $GLOBALS['post'];
99
		} elseif ( is_numeric( $the_product ) ) {
100
			$the_product = get_post( $the_product );
101
		} elseif ( $the_product instanceof WC_Product ) {
102
			$the_product = get_post( $the_product->id );
103
		} elseif ( ! ( $the_product instanceof WP_Post ) ) {
1 ignored issue
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
104
			$the_product = false;
105
		}
106
107
		return apply_filters( 'woocommerce_product_object', $the_product );
108
	}
109
}
110