Automattic / woocommerce-payments

Accept payments via credit card. Manage transactions within WordPress.
https://wordpress.org/plugins/woocommerce-payments/
Other
174 stars 69 forks source link

Warning due to unhandled possible return value #9341

Open ahegyes opened 2 months ago

ahegyes commented 2 months ago

Describe the bug

When going through checkout without a payment method, the plugin throws the following warning:

[30-Jul-2024 18:58:42 UTC] PHP Warning:  Attempt to read property "id" on false in /srv/htdocs/wp-content/plugins/woocommerce-payments/includes/class-woopay-tracker.php on line 513

The line has changed since then: https://github.com/Automattic/woocommerce-payments/blob/244b7c937b86ba902a74fc21050d7ff1618bf690/includes/class-woopay-tracker.php#L543

To Reproduce

This is the code in question:

$payment_gateway = wc_get_payment_gateway_by_order( $order_id );
$properties      = [ 'payment_title' => 'other' ];

// If the order was placed using WooCommerce Payments, record the payment title using Tracks.
if ( strpos( $payment_gateway->id, 'woocommerce_payments' ) === 0 ) {

Basically, the wc_get_payment_gateway_by_order function can also return the value false which is not handled here. The if statement should be enhanced either with a check against false or for WC_Payment_Gateway (instanceof, e.g.).

Actual behavior

A warning is logged because the returned value wasn't expected.

Expected behavior

Ideally, there should be no warning logged when that happens.

glagonikas commented 2 days ago

@c-shultz @c-shultz @pierorocca as there's now a PR for this, can you please label the issue accordingly so it can be reviewed and hopefully merged?