Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #13319 , Incorrect method return value in \Magento\Shipping\Model\Carrier\AbstractCarrier::getTotalNumOfBoxes() #1

Closed
wants to merge 1 commit into from

Conversation

cedmudit
Copy link
Owner

@cedmudit cedmudit commented Feb 3, 2019

Fixed magento#13319 , Incorrect method return value in \Magento\Shipping\Model\Carrier\AbstractCarrier::getTotalNumOfBoxes()

added type casting and updated function comment..

Please see the method getTotalNumOfBoxes() in \Magento\Shipping\Model\Carrier\AbstractCarrier: https://github.com/magento/magento2/blob/2.2.2/app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php#L512-L531

Incoming $weight parameter is not checked to be integer. Moreover, even if it is, you can still get a float result when $weight > $maxPackageWeight since these are both arbitrary numbers.

This method is a member of AbstractCarrier class commonly used for 3-rd party implementations of the Shipmen Carrier SPI and might be used by 3rd-party developers in their modules.
In order to apply proper validation and\or rounding around the method's return results, it would be helpful to see the actual return value in PHPDocs which are commonly used by IDEs when providing hints, for example here: http://take.ms/9dnaD

Additionally, the method name is severely misleading: it implies the method is a getter, however it can modify object's internal state. Moreover, the value returned is in fact what seems to be an average weight of a box, not "Total number of boxes" as one might assume.

Backward Compatibility: Please note that Magento's USPS implementation expects float return value, implying internal knowledge of the method: https://github.com/magento/magento2/blob/2.2.2/app/code/Magento/Usps/Model/Carrier.php#L341
If any rounding is to be added, it should probably be done via a new extra method, deprecating the old one (if at all)

Preconditions

  1. Magento CE 2.1.10, Magento CE 2.2.2
  2. PHP 7.0.18

Steps to reproduce

  1. Open file app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php
  2. Scroll down to getTotalNumOfBoxes method

Expected result

  1. Observe correct return type "float" declared in PHPDoc block
    OR proper rounding applied to the $weight variable.

Actual result

  1. Observe incorrect return type "int" declared in PHPDoc block
    AND no rounding applied to $weight variable

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

Fixed magento#13319 , added type casting and updated function comment..
@cedmudit cedmudit closed this Feb 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant