Fatal error: Uncaught ArgumentCountError: Too few arguments to function TYPO3\CMS\Core\Imaging\IconFactory::__construct()

Viewed 3564

After following the composer installation guide for v10 of typo3. I pointed apache vhost to the public folder. Once I navigate to the index.php location in the browser, I get this error

Fatal error: Uncaught ArgumentCountError: Too few arguments to function
TYPO3\CMS\Core\Imaging\IconFactory::__construct()
0 passed in /home/user/projects/typo3/public/typo3/sysext/core/Classes/Utility/GeneralUtility.php
on line 3423
and exactly 2 expected in
/home/user/projects/typo3/public/typo3/sysext/core/Classes/Imaging/IconFactory.php:71

It looks like a dependency injection problem. Please can anybody help with this error

3 Answers

For me this issue occured after moving an existing project from a server into DDEV (which is similar to changing the path/URL by a vhost config). My guess is it has to do with changed paths/URLs in cached files. This is how I solved it:

A) Manually delete all cached files:

t3project$ rm -rf public/typo3temp/*
t3project$ rm -rf var/*

B) Also I had to change the ownership of some autogenerated folders/files to my current user (sudo chown -R myuser:myuser t3project/), then I was able to use the "Fix folder structure" tool in "Environment > Directory Status", now everything was working fine again. Not sure if the last step is helpful for you, as it might be only related to my case where certain folder/files had a wrong owner as they was copied.

I had the same problem today and it occured because I was XClass'ing one of the Core Classes and used GeneralUtility::makeInstance(IconFactory::class) in this code.
The fix is to use DI in this class, just as you suggested. Also flush all caches afterwards to rebuild the DI container.

From this:

class CTypeList extends AbstractList
{
    public function itemsProcFunc(&$params)
    {
            $fieldHelper = GeneralUtility::makeInstance(MASK\Mask\Helper\FieldHelper::class);
            $storageRepository = GeneralUtility::makeInstance(MASK\Mask\Domain\Repository\StorageRepository::class);
            ...

To this:

class CTypeList extends AbstractList
{

    protected StorageRepository $storageRepository;
    protected FieldHelper $fieldHelper;

    public function __construct(StorageRepository $storageRepository, FieldHelper $fieldHelper)
    {
        $this->storageRepository = $storageRepository;
        $this->fieldHelper = $fieldHelper;
    }

    public function itemsProcFunc(&$params)
    {
        $this->storageRepository->doStuff();
        $this->fieldHelper->doStuff();
        ...

For future reference for others:

This can also happen in own extensions when the Core uses GeneralUtility::makeInstance on your classes. (e.g. in AuthenticationServices). The trick here is to make these DI services public like so:

(in extension_path/Configuration/Serivces.yaml)

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Vendor\ExtensionName\Service\FrontendOAuthService:
    public: true

Here's documentation for it: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/DependencyInjection/Index.html#knowing-what-to-make-public

I had this error because i used the Services.yaml file in one of my extensions, but did not configure it correct.

More infos about the file itself can be found here

Since the file is responsible for the dependency injection, small mistakes e.g. in namespaces lead to the above mentioned error.

To locate the error you can uninstall extensions with a Services.yaml. When you have found the file/extension, you have to check if all Namespaces in the Classes Directory are correct. This means:

  • All filenames are correct regarding the Class they contains
  • All Namespaces in the files are correct for path and filename
  • The Namespace can be found via composer. So the extension have to be installed via composer or must have an entry in the autoload list of composer.json
Related