user15459507
user15459507

Reputation:

Undefined variable: returnValue

<?php

namespace StripeIntegration\Payments\Plugin\Sales\Model\Service;

class OrderService
{
    public function __construct(
        \StripeIntegration\Payments\Helper\Rollback $rollback,
        \StripeIntegration\Payments\Helper\GenericFactory $helperFactory
    ) {
        $this->rollback = $rollback;
        $this->helperFactory = $helperFactory;
    }

    public function aroundPlace($subject, \Closure $proceed, $order)
    {
        try
        {
            $this->rollback->reset();
            $returnValue = $proceed($order);
            $this->rollback->reset();
        }
        catch (\Exception $e)
        {
            $helper = $this->helperFactory->create();
            \StripeIntegration\Payments\Helper\Logger::log($e->getMessage());
            if ($order->getId())
            {
                // The order has already been saved, so we don't want to run the rollback. The exception likely occurred in an order_save_after observer.
                $this->rollback->reset();
                $helper->dieWithError($e->getMessage(), $e);
            }
            else
            {
                $msg = $e->getMessage();
                if (!$this->isAuthenticationRequiredMessage($msg))
                    $this->rollback->run($e);
                else
                    $this->rollback->reset(); // In case some customization is trying to place multiple split-orders

                $helper->dieWithError($e->getMessage(), $e);
            }
        }
        return $returnValue;
    }

}

Any thoughts how to solve this error ? I am using magento version 2.4 we the call run through this file the error appears It seems like the variable is not getting any data in code. I need some fix to prevent this error

Upvotes: 0

Views: 235

Answers (1)

Shahlin Ibrahim
Shahlin Ibrahim

Reputation: 1095

The problem is you're trying to access the variable returnValue which is not defined. Your code throws an exception in the closure (or it could be in the reset() function), so returnValue is never defined.

What's happening:

  1. Enters try block
  2. Calls the function reset(). Lets assume this doesn't throw the exception
  3. Calls the closure. This throws an exception. So your variable is not successfully defined.
  4. Goes to the catch block
  5. Goes to return. At this point, the variable returnValue is never defined

A possible fix it to declare the variable with some value on top like shown below

namespace StripeIntegration\Payments\Plugin\Sales\Model\Service;

class OrderService
{
    public function __construct(
        \StripeIntegration\Payments\Helper\Rollback $rollback,
        \StripeIntegration\Payments\Helper\GenericFactory $helperFactory
    ) {
        $this->rollback = $rollback;
        $this->helperFactory = $helperFactory;
    }

    public function aroundPlace($subject, \Closure $proceed, $order)
    {
        $returnValue = null; // Or have it equal to some meaningful value
        try
        {
            $this->rollback->reset();
            $returnValue = $proceed($order);
            $this->rollback->reset();
        }
        catch (\Exception $e)
        {
            // handle exception
        }
        return $returnValue;
    }

}

Upvotes: 1

Related Questions