Search code examples
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:

  • When I run the standard phpstan analyse command, the analysis executes successfully using my custom phpstan.neon configuration. However, it includes errors related to TYPO3 core code, which suggests that TYPO3-specific rules are not being applied.
  • The same configuration works correctly in another environment running TYPO3 v12 and PHP 8.2. I have compared both environments but haven’t found any significant differences.

What I've tried:

  • Ensuring the configurations and dependencies are identical in both environments.
  • Checking the PHPStan, extension-installer, phpstan-typo3 and TYPO3-related documentation, but I couldn’t find a solution.
  • Searching online for similar issues, but nothing has resolved the problem so far.

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