phpcomposer-phptypo3typo3-extensionsphpstan

PHPStan Configuration Error in TYPO3 Extension: Alias Loader Already Registered


I’m encountering an issue while configuring PHPStan for a TYPO3 extension (TYPO3 v13.4.1, PHP 8.2, DDEV environment).

I have a custom extension and need to configure PHPStan to use rules from an additional package while avoiding TYPO3 core code. To achieve this, I created a custom ddev command to run PHPStan within the DDEV container. However, when I execute the command, I get the following error:

/var/www/html/vendor/bin/phpstan analyse -c phpstan.neon PHP Fatal error: Uncaught RuntimeException: Cannot set the alias loader, as it is already registered! in /var/www/html/packages/my_extension/vendor/typo3/class-alias-loader/src/ClassAliasMap.php:72

Here is the relevant part of my composer.json file for the extension:

"require-dev": {
"friendsofphp/php-cs-fixer": "3.64.*",
"phpstan/phpstan": "1.12.*",
"phpstan/extension-installer": "1.4.*",
"saschaegerer/phpstan-typo3": "1.10.*",
"captainhook/captainhook": "5.23.*"
}

Observations:

What I've tried:


Solution

  • Modify your extension’s composer.json to disable the alias loader in your extension's context:

    "config": { "allow-plugins": { "typo3/class-alias-loader": false } }

    After making this change, run:

    composer dump-autoload