Ensure all calls to price getters handle NULL returns

Created on 7 September 2018, about 6 years ago
Updated 17 November 2023, 11 months ago

Problem/Motivation

The various price getters (orders, order items) may validly return NULL (to handle that we may not know currency etc). All code calling those methods should not assume that a Price will be returned.

Proposed resolution

Fix cases that assume a Price will be returned.

Ideally see if we can get some form of static analysis in test coverage to ensure we don't get future additions.

User interface changes

None.

API changes

None.

Data model changes

None.

Original report by nikita_tt

Here is actual method:

  /**
   * {@inheritdoc}
   */
  public function isVisible() {
    if ($this->order->getTotalPrice()->isZero()) {
      // Hide the pane for free orders, since they don't need a payment.
      return FALSE;
    }
    $payment_info_pane = $this->checkoutFlow->getPane('payment_information');
    if (!$payment_info_pane->isVisible() || $payment_info_pane->getStepId() == '_disabled') {
      // Hide the pane if the PaymentInformation pane has been disabled.
      return FALSE;
    }

    return TRUE;
  }

But sometimes $this->order->getTotalPrice() could be empty. See \Drupal\commerce_order\Entity\Order::getTotalPrice().

This can happen when an Order has been processed through Availability Checker and all order items were deleted.

🐛 Bug report
Status

Needs work

Version

2.0

Component

Payment

Created by

🇺🇦Ukraine nikita_tt

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024