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
Issue fork commerce_shipping-3562463
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
Comment #2
rwanthComment #4
rwanthFailing test is unrelated to this MR, coming from testAddShippingInModal.
Comment #5
jsacksick commentedWell 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?
Comment #6
rwanthI 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.
Comment #7
jsacksick commentedI'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.
Comment #8
jsacksick commentedComment #9
rwanthMakes sense to me! Updated the MR.
Comment #10
knyshuk.vova commentedI 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 365I can confirm that the code in MR fixes the issue.
+1 RTBC
Comment #11
jsacksick commentedSo... 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.
Comment #12
jsacksick commentedSo this is the actual issue: #3562466: Fatal error when no default rate is found.
Comment #13
jsacksick commentedBut 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.
Comment #15
jsacksick commented