php build_error ai_generated true

Warning: Ambiguous class resolution, "App\Models\User" was found in both "/var/www/app/src/Models/User.php" and "/var/www/app/src/Models/Admin/User.php"

ID: php/composer-outdated-autoload

Also available as: JSON · Markdown · 中文
88%Fix Rate
85%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
composer 2.6 active
composer 2.7 active
php 8.1 active
php 8.2 active

Root Cause

Composer's autoloader detects two files that both declare the same fully-qualified class name due to overlapping namespace-to-directory mappings or duplicate class definitions in different files.

generic

中文

Composer 自动加载器检测到两个文件声明了相同的完全限定类名,原因是命名空间到目录的映射重叠或不同文件中存在重复的类定义。

Official Documentation

https://getcomposer.org/doc/04-schema.md#autoload

Workarounds

  1. 90% success Run `composer dump-autoload -o` to regenerate the optimized autoload map, then check the generated `vendor/composer/autoload_classmap.php` for duplicate entries and remove the incorrect one.
    Run `composer dump-autoload -o` to regenerate the optimized autoload map, then check the generated `vendor/composer/autoload_classmap.php` for duplicate entries and remove the incorrect one.
  2. 95% success Refactor the namespace: move `App\Models\Admin\User` to a distinct namespace like `App\Models\AdminUser` or adjust the PSR-4 prefix in `composer.json` to avoid overlap.
    Refactor the namespace: move `App\Models\Admin\User` to a distinct namespace like `App\Models\AdminUser` or adjust the PSR-4 prefix in `composer.json` to avoid overlap.

中文步骤

  1. Run `composer dump-autoload -o` to regenerate the optimized autoload map, then check the generated `vendor/composer/autoload_classmap.php` for duplicate entries and remove the incorrect one.
  2. Refactor the namespace: move `App\Models\Admin\User` to a distinct namespace like `App\Models\AdminUser` or adjust the PSR-4 prefix in `composer.json` to avoid overlap.

Dead Ends

Common approaches that don't work:

  1. Clearing the Composer cache with `composer clear-cache` 80% fail

    The cache clear does not fix the underlying duplicate class mapping; it only removes cached metadata.

  2. Renaming one of the files without updating the namespace 95% fail

    The class name is defined by the namespace and class declaration in the file, not the filename; renaming the file without changing the class declaration does not resolve the ambiguity.