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

AbstractCarrierInterface::collectRates() has incorrect return signature #28514

Open
kassner opened this issue Jun 4, 2020 · 6 comments
Open
Labels
Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: ready for confirmation Priority: P3 May be fixed according to the position in the backlog. Progress: ready for dev Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@kassner
Copy link
Contributor

kassner commented Jun 4, 2020

Summary

\Magento\Shipping\Model\Carrier\AbstractCarrierInterface::collectRates currently has in its signature that it will return \Magento\Framework\DataObject, but technically \Magento\Shipping\Model\Rate\Result is used everywhere, and it does not inherit the DataObject, so the signature is, de facto, incorrect.

Examples

\Magento\Shipping\Model\Shipping:

        if (!$result) {
            //One shipment for all items.
            $result = $carrier->collectRates($request);
        }

        if (!$result) {
            return $this;
        } elseif ($result instanceof Result) { // use Magento\Shipping\Model\Rate\Result;
            $this->getResult()->appendResult($result, $carrier->getConfigData('showmethod') != 0);
        } else {
            $this->getResult()->append($result);
        }

Both append methods expect the result class:

    /**
     * Append result received from a carrier.
     *
     * @param Result $result
     * @param bool $appendFailed Append result's errors as well.
     * @return void
     */
    public function appendResult(Result $result, bool $appendFailed): void
    {
        $this->results[] = ['result' => $result, 'appendFailed' => $appendFailed];
    }
    /**
     * Add a rate to the result
     *
     * @param \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult|\Magento\Shipping\Model\Rate\Result $result
     * @return $this
     */
    public function append($result)
    {
        if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
            $this->setError(true);
        }
        if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult) {
            $this->_rates[] = $result;
        } elseif ($result instanceof \Magento\Shipping\Model\Rate\Result) {
            $rates = $result->getAllRates();
            foreach ($rates as $rate) {
                $this->append($rate);
            }
        }
        return $this;
    }

Some Magento shipping modules also don't expect the DataObject to be used:

Flatrate:

    /**
     * Collect and get rates
     *
     * @param RateRequest $request
     * @return Result|bool
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function collectRates(RateRequest $request)
    {
        /** @var Result $result */
        $result = $this->_rateResultFactory->create();
...
        return $result;
    }

Freeshipping:

    /**
     * FreeShipping Rates Collector
     *
     * @param RateRequest $request
     * @return \Magento\Shipping\Model\Rate\Result|bool
     */
    public function collectRates(RateRequest $request)
    {
...
        /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->_rateResultFactory->create();
...
        return $result;
    }

UPS:

    /**
     * Collect and get rates/errors
     *
     * @param RateRequest $request
     * @return Result|Error|bool
     */
    public function collectRates(RateRequest $request)
    {
...
    }

Fedex:

    /**
     * Collect and get rates
     *
     * @param RateRequest $request
     * @return Result|bool|null
     */
    public function collectRates(RateRequest $request)
    {
        if (!$this->canCollectRates()) {
            return $this->getErrorMessage();
        }

        $this->setRequest($request);
        $this->_getQuotes();
        $this->_updateFreeMethodQuote($request);

        return $this->getResult();
    }

Basically no class expects \Magento\Framework\DataObject, as the AbstractCarrierInterface::collectRates() suggests.

Proposed solution

Change the method's signature to reflect the real world, but the method is marked with @api, so I don't know how to proceed.

@kassner kassner added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Jun 4, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jun 4, 2020

Hi @kassner. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


@ghost ghost added this to Ready for QA in Community Backlog Jun 4, 2020
@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Jun 4, 2020
@sidolov sidolov added this to Ready for Grooming in Low Priority Backlog Sep 24, 2020
@sidolov sidolov added this to Ready for Confirmation in Issue Confirmation and Triage Board Oct 21, 2020
@ghost ghost removed this from Ready for QA in Community Backlog Oct 21, 2020
@ghost ghost removed this from Ready for Grooming in Low Priority Backlog Oct 21, 2020
@ghost ghost added Issue: ready for confirmation and removed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed labels Oct 21, 2020
@stale
Copy link

stale bot commented Jan 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale issue label Jan 5, 2021
@kassner
Copy link
Contributor Author

kassner commented Jan 7, 2021

up

@stale stale bot removed the stale issue label Jan 7, 2021
@gabrieldagama gabrieldagama added Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Jan 7, 2021
@m2-community-project m2-community-project bot moved this from Ready for Confirmation to Confirmed in Issue Confirmation and Triage Board Jan 7, 2021
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @gabrieldagama
Thank you for verifying the issue. Based on the provided information internal tickets MC-40335 were created

Issue Available: @gabrieldagama, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@gabrieldagama gabrieldagama added Issue: ready for confirmation Priority: P3 May be fixed according to the position in the backlog. Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. labels Jan 7, 2021
@m2-community-project m2-community-project bot added this to Ready for Development in Low Priority Backlog Jan 7, 2021
@stale
Copy link

stale bot commented Mar 25, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

@stale stale bot added the stale issue label Mar 25, 2021
@kassner
Copy link
Contributor Author

kassner commented Mar 26, 2021

up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: ready for confirmation Priority: P3 May be fixed according to the position in the backlog. Progress: ready for dev Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Low Priority Backlog
  
Ready for Development
Development

No branches or pull requests

3 participants