Problem/Motivation

I encountered a scenario in a contrib module where the developer used the ShipmentManager service to call selectDefaultRate(ShipmentInterface $shipment, array $rates). Due to site config, an empty array was passed to $rates, which resulted in the function returning FALSE. Because the module was expecting a response of type ShippingRate, it threw a fatal error.

As a plugin developer, this case is easy to miss because the documentation for selectDefaultRate() says that it will always return a ShippingRate.

Steps to reproduce

Load a valid shipment from your site, then try running \Drupal::services(commerce_shipment.shipment_manager)->selectDefaultRate($my_shipment, [])

Proposed resolution

Update the documentation in ShipmentManagerInterface to indicate that the function selectDefaultRate can return FALSE if it received an empty array.

Remaining tasks

Consider updating the selectDefaultRate such that it responds in a more useful way if given an empty or invalid array.

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rwanth created an issue. See original summary.

rwanth’s picture

Issue summary: View changes

rwanth’s picture

Status: Active » Needs review

Failing test is unrelated to this MR, coming from testAddShippingInModal.

jsacksick’s picture

Well because in theory selectDefaultRate() should be called with a rates array? If you aren't passing rates to the method, then I think this is what needs to be fixed instead?

rwanth’s picture

I agree, that's why I think the proper course here is just document how the methods responds, so that it's clear to developers what will happen if they did pass something incorrect.

jsacksick’s picture

I'm suggesting we document that it can return NULL instead.
And we return NULL if $default_rate is FALSE.

Something like the following:
return $default_rate ?: NULL;

It feels weird to return a boolean.

jsacksick’s picture

Status: Needs review » Needs work
rwanth’s picture

Status: Needs work » Needs review

Makes sense to me! Updated the MR.

knyshuk.vova’s picture

Status: Needs review » Reviewed & tested by the community

I have reproduced the issue using commerce_shipping 8.x-2.15 and commerce_shipping_pickup_api 1.0.3 :
TypeError: Drupal\commerce_shipping_pickup_api\Plugin\Commerce\CheckoutPane\PickupCapableShippingInformation::isPickupSelected(): Argument #4 ($default_rate) must be of type ?Drupal\commerce_shipping\ShippingRate, bool given, called in /app/web/modules/contrib/commerce_shipping_pickup_api/src/Plugin/Commerce/CheckoutPane/PickupCapableShippingInformation.php on line 365

I can confirm that the code in MR fixes the issue.
+1 RTBC

jsacksick’s picture

So... selectDefaultRate() should return NULL, not FALSE.

And @knyshuk.vova: regarding the backtrace you shared, the real problem here is the commerce_shipping_pickup_api module calling selectDefaultRate() with an empty rates array.
THe module should make sure there are rates returned before calling the method. You don't call the method to select a default rate, if there are no rates. That doesn't make sense, so a bug report should be opened there.

jsacksick’s picture

jsacksick’s picture

But actually, sorry, the MR reflects what I said we should do instead, didn't see the updated change... So all good with me, going to merge the MR.

  • jsacksick committed 9f4f697f on 3.x authored by rwanth
    fix: #3562463 Undocumented return type: ShipmentManager...
jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.