5

Is there a way to disable the “Ambiguous class resolution” warning when running composer install?

I use a package which has classes with the same name (and namespace) in different folders.

I know of this bug, but it's not that because the classes are actually twice in the vendor. I just can't do anything about it.

I'm also aware of the --no-autoloader flag which of course doesn't throw the warning, but just because it skips the autoloader generation.

Community
  • 1
  • 1
Christian Kolb
  • 1,368
  • 2
  • 23
  • 43
  • What is the autoload definition of that package? And how does it distinguish between the two classes in it's code, i.e. how does it load one file? – Sven Mar 27 '15 at 17:42

3 Answers3

7

Instead of removing files from vendor directory (which should be avoided) it is better to add files/direactories with ambiguous classes to exclude-from-classmap section in your composer.json:

"autoload": {
    ...
    "exclude-from-classmap": [
        "vendor/somevendor/somepackage/directory/with/ambiguous/classes/",
        "vendor/somevendor/somepackage/src/AmbiguousClass.php"
    ]
},

Then Composer will ignore these files during classmap generation.

rob006
  • 21,383
  • 5
  • 53
  • 74
4

Found a solution, although it's not that obvious. Wrote about it here.

When using the paypal/merchant-sdk-php composer package and running composer install or composer update you're getting a lot of warning messages like the following:

...
Writing lock file  
Generating autoload files  
Warning: Ambiguous class resolution, "SetDataRequestType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/SetDataRequestType.php", the first will be used.  
Warning: Ambiguous class resolution, "MerchantPullPaymentType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/MerchantPullPaymentType.php", the first will be used.  
Warning: Ambiguous class resolution, "InitiateRecoupReq" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/InitiateRecoupReq.php", the first will be used.  
Warning: Ambiguous class resolution, "CreateBillingAgreementResponseType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/CreateBillingAgreementResponseType.php", the first will be used.  
Warning: Ambiguous class resolution, "MeasureType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/MeasureType.php", the first will be used.  
Warning: Ambiguous class resolution, "BusinessInfoType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/BusinessInfoType.php", the first will be used.  
Warning: Ambiguous class resolution, "ButtonSearchResultType" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/ButtonSearchResultType.php", the first will be used.  
Warning: Ambiguous class resolution, "SetAuthFlowParamReq" was found in both "/project/vendor/paypal/merchant-sdk-php/lib/services/PayPalAPIInterfaceService/PayPalAPIInterfaceService.php" and "/project/vendor/paypal/merchant-sdk-php/lib/PayPalAPIInterfaceService/SetAuthFlowParamReq.php", the first will be used.  
...
# Many more lines

There already is a bug ticket on Github for this issue but it doesn't seems to be worked on with a high priority (as it was opened on 31 Jul 2014).

After searching for a while, I found the source of the problem, which is / are called PPAutoloader and PayPalAPIInterfaceService. Every single class of the API is copied into the PayPalAPIInterfaceService. It is used for the examples (which are part of the package) and loaded with the PPAutoloader if no vendor/autoload.php is available. But this internal logic (which autoloader has to be called) is not relevant for the "composer autoloader generation process" which runs through all folders and builds it's own configuration.

So what's the solution?

There is no way to exclude folders (which are within the vendor folder) from the autoloader generation process. Of course you could run composer with the --no-autoloader flag. But then you would need to handle the autoloading yourself which is a little bit drastic for an issue which "only" produces warnings.

In this case there is an easier way. As this class is only relevant for the examples (which aren't used in the system), I can simply delete the file.

For that I use the scripts part of the composer.json and the pre-autoload-dump command event (which is triggered right before the autoloader generation process). The command deletes the services folder which contains the PayPalAPIInterfaceService.

"scripts": {
    "pre-autoload-dump": [
        "rm -rf vendor/paypal/merchant-sdk-php/lib/services"
    ]
},
bummi
  • 27,123
  • 14
  • 62
  • 101
Christian Kolb
  • 1,368
  • 2
  • 23
  • 43
-1

Removing the vendor directory and running composer install got rid of the errors for me.

cmac
  • 3,123
  • 6
  • 36
  • 49