Magento – M2/ How to fix “Incompatible argument type: Required type: string. Actual type …”

constructmagento2setup-di-compile

A compilation was started.

Interception cache generation… 6/7 [========================>—]
85% 1 min 404.0 MiB Errors during compilation:
SkyFox\Supplier\UI\DataProvider\Supplier\DataProvider

Incompatible argument type: Required type: string.

Actual type:\SkyFox\Supplier\UI\DataProvider\Supplier\name;

File: /var/www/html/sky.local/app/code/SkyFox/Supplier/UI/DataProvider/Supplier/DataProvider.php

Total Errors Count: 1
[Magento\Framework\Validator\Exception]
Error during compilation

setup:di:compile

This's my code:

namespace SkyFox\Supplier\Model\Company;

use Magento\Framework\App\Request\DataPersistorInterface;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider {

    /**
     * @var \SkyFox\Supplier\Model\ResourceModel\Supplier\Collection
     */
    protected $collection;

    /**
     * @var DataPersistorInterface
     */
    protected $dataPersistor;

    /**
     * @var array
     */
    protected $loadedData;

    /**
     * @var \Magento\Framework\DB\Adapter\AdapterInterface
     */
    protected $connection;

    /**
     * DataProvider constructor.
     * @param $name
     * @param $primaryFieldName
     * @param $requestFieldName
     * @param \SkyFox\Supplier\Model\ResourceModel\Supplier\CollectionFactory $collectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        \SkyFox\Supplier\Model\ResourceModel\Supplier\CollectionFactory $collectionFactory,
        DataPersistorInterface $dataPersistor,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        $this->connection = $connection;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();

        foreach ($items as $supplier) {
            $this->loadedData[$supplier->getData('company_id')] = $supplier->getData();
        }

        $data = $this->dataPersistor->get('supplier');
        if (!empty($data)) {
            $supplier = $this->collection->getNewEmptyItem();
            $supplier->setData($data);
            $this->loadedData[$supplier->getData('company_id')] = $supplier->getData();
            $this->dataPersistor->clear('supplier');
        }

        return $this->loadedData;
    }
}

Thanks all

Best Answer

The error Incompatible argument type: Required type: string. Actual type: \SkyFox\Supplier\UI\DataProvider\Supplier\name; File is triggered because of the missing type in the __construct phpDoc annotation.

So the __construct phpDoc annotation need to be as below:

/**
 * DataProvider constructor.
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param \SkyFox\Supplier\Model\ResourceModel\Supplier\CollectionFactory $collectionFactory
 * @param DataPersistorInterface $dataPersistor
 * @param array $meta
 * @param array $data
 */
 public function __construct(
     $name,
     $primaryFieldName,
     $requestFieldName,
     \SkyFox\Supplier\Model\ResourceModel\Supplier\CollectionFactory $collectionFactory,
     DataPersistorInterface $dataPersistor,
     array $meta = [],
     array $data = []
) {
     $this->dataPersistor = $dataPersistor;
     $this->connection = $connection;
     parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
   }

Means:

@param $name

@param $primaryFieldName

@param $requestFieldName

Should be

@param string $name

@param string $primaryFieldName

@param string $requestFieldName

For more details check Magento\Ui\DataProvider\AbstractDataProvider class

You can also remove \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, As you are not using it any where on the class.