Use instance of checks for detecting WCProduct

[MAILPOET-2586]
This commit is contained in:
Rostislav Wolny
2019-12-19 13:27:44 +01:00
committed by Jack Kitterhing
parent 73e9cc3517
commit d3ce76f88d
6 changed files with 21 additions and 5 deletions

View File

@ -110,7 +110,7 @@ class PurchasedInCategory {
$ordered_product_categories = [];
foreach ($order_details->get_items() as $order_item_product) {
$product = $order_item_product->get_product();
if (!$product || !is_callable([$product, 'get_category_ids'])) {
if (!$product instanceof \WC_Product) {
continue;
}
$ordered_product_categories = array_merge($ordered_product_categories, $product->get_category_ids());

View File

@ -118,7 +118,7 @@ class PurchasedProduct {
}
$ordered_products = array_map(function($product) {
return is_callable([$product, 'get_product_id']) ? $product->get_product_id() : null;
return ($product instanceof \WC_Order_Item_Product) ? $product->get_product_id() : null;
}, $order_details->get_items());
$ordered_products = array_values(array_filter($ordered_products));

View File

@ -2,7 +2,7 @@
<ruleset name="MailPoet">
<config name="installed_paths" value="tasks/code_sniffer/vendor/squizlabs/php_codesniffer,tasks/code_sniffer/vendor/phpcompatibility/php-compatibility,tasks/code_sniffer/vendor/slevomat/coding-standard"/>
<description>MailPoet specific rule set</description>
<exclude-pattern>*/phpstan/woocommerce.php</exclude-pattern>
<!-- Namespaces -->
<rule ref="PSR2.Namespaces.NamespaceDeclaration"/>
<rule ref="SlevomatCodingStandard.Namespaces.NamespaceDeclaration"/>

View File

@ -64,3 +64,15 @@ class WC_DateTime extends \DateTime {
*/
class WC_Order { // phpcs:ignore
}
/**
* @method int get_product_id(string $context = 'view')
*/
class WC_Order_Item_Product { // phpcs:ignore
}
/**
* @method int[] get_category_ids(string $context = 'view')
*/
class WC_Product { // phpcs:ignore
}

View File

@ -221,13 +221,13 @@ class PurchasedProductTest extends \MailPoetTest {
'get_items' => function() use ($incorrect_product_id, $product_id) {
return [
Stub::make(
new ItemDetails(),
\WC_Order_Item_Product::class,
[
'get_product_id' => $incorrect_product_id,
]
),
Stub::make(
new ItemDetails(),
\WC_Order_Item_Product::class,
[
'get_product_id' => $product_id,
]

View File

@ -231,6 +231,10 @@ if (!function_exists('WC')) {
function WC() {
return new WooCommerce;
}
class WC_Order_Item_Product { // phpcs:ignore
function get_product_id() { // phpcs:ignore
}
}
}
include '_fixtures.php';