0

WARNING | Only dependency assignment operations are allowed in | | constructor. No other operations are allowed.

I am getting this type of error while checking magento coding standard.

I posted here my controller code when error generated.

<?php
namespace CompanyName\ModuleName\Controller\Index;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\MediaStorage\Model\File\UploaderFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * [$_fileUploaderFactory description]
     * @var [type]
     */
    protected $_fileUploaderFactory;

    /**
     * [$filesystem description]
     * @var [type]
     */
    protected $filesystem;

    /**
     * [$scopeConfig description]
     * @var [type]
     */
    protected $scopeConfig;

    /**
     * [$storeManager description]
     * @var [type]
     */
    protected $storeManager;
    /**
     * [$helper description]
     * @var [type]
     */
    protected $helper;

    /**
     * [__construct description]
     * @param \Magento\Framework\App\Action\Context              $context             [description]
     * @param UploaderFactory                                    $fileUploaderFactory [description]
     * @param \Magento\Framework\Filesystem                      $filesystem          [description]
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig         [description]
     * @param \CompanyName\ModuleName\Helper\Image                    $helper              [description]
     * @param \Magento\Store\Model\StoreManagerInterface         $storeManager        [description]
     */
    public function __construct( //warning line display here
        \Magento\Framework\App\Action\Context $context,
        UploaderFactory $fileUploaderFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \CompanyName\ModuleName\Helper\Image $helper,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->filesystem = $filesystem;
        $this->scopeConfig = $scopeConfig;
        $this->helper = $helper;
        $this->storeManager = $storeManager;
        return parent::__construct($context);
    }

    public function execute()
    {

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $max_file_size = $this->scopeConfig->getValue(self::XML_PATH_MAX_UPLOAD_SIZE, $storeScope);
        $currentStore = $this->storeManager->getStore();
        $uploader = $this->_fileUploaderFactory->create(['fileId' => 'file']);
        $fInam = $uploader->validateFile();
        ********************

        $uploader = $this->_fileUploaderFactory->create(['fileId' => 'file']);

        ********************
        $path = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('foldername/');
        $imgpath = $this->helper->imageResize('folder/' . $newFileName, 100, 100, 'folder/');

        ********************
    }
}

Can anyone please help me to solve this warning ?

  • Remove return just use parent::__construct($context); at last line of constructor. – Nalin Savaliya Jun 18 '18 at 5:15
  • @NalinSavaliya I am facing the same issue while trying to save my extension over the marketplace. I am not using return in my constructor. – iqbal malik Mar 28 at 5:52
  • @iqbalmalik please post your question with your code, as specially constructor. – Nalin Savaliya Mar 28 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.