0

I recently use PhpStorm to write my code and it finds an error in a controller who get data from a <form> to save them in a custom model. (works fine even without correction).

Class StockRequestForm extends \Magento\Framework\App\Action\Action
{
    public function __construct(....) 
    {
        ....
    }

    /**
     * @inheritDoc
     */
    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        if (!$this->helper->getFormKeyValidation($this->getRequest())) {
          $this->messageManager->addErrorMessage('Invalid request!');
          return $resultRedirect->setRefererOrBaseUrl();
        }

        $post = (array)$this->getRequest()->getPost();
        if (!empty($post)) {
            try {
                ... // saving $model
                $this->messageManager->addSuccessMessage(__('You\'re request have been submitted.'));
                return $resultRedirect->setRefererOrBaseUrl();
            } catch (\Exception $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
                return $resultRedirect->setRefererOrBaseUrl();
            }
        }
    
        $this->_view->loadLayout();
        $this->_view->renderLayout();

    } // here the missing 'return' statement !
}
2
  • You have an error in your code. A curly braces is missing Jul 18, 2020 at 11:01
  • its not really the full method, just part of it and I forgot the last one
    – cap340
    Jul 18, 2020 at 13:56

1 Answer 1

3

That is because of this phpDoc Block of your execute function

/**
 * @inheritDoc
 */

If you check the parent interface

Magento\Framework\App\ActionInterface

It clearly defines the return type

/**
 * Execute action based on request and return result
 *
 * Note: Request will be added as operation argument in future
 *
 * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
 * @throws \Magento\Framework\Exception\NotFoundException
 */
public function execute();

In order to fix this you should write your own or update your phpDoc Block to return void.

/**     
 * @return void
 */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.