Insolita / unused-scanner

Detect unused composer dependencies
MIT License
429 stars 38 forks source link

Laravel autodiscovery #38

Open wedi opened 3 years ago

wedi commented 3 years ago

Is it somehow possible to support Laravels autodicovery feature?

I tried to dig into Laravel to find the relevant code but I only found the place where the extra gets loaded, not where it's actually stored.

Php version:

8.0 (but doesn't really matter)

Os:

macOS / Ubunutu / Alpine

Composer packages with detection troubles:

All Laravel autodiscovered packages, e.g. https://github.com/spatie/laravel-json-api-paginate

Which type of autoload it uses?

Autodiscovery via the package's composer.json extra.
See: https://laravelpackage.com/03-service-providers.html#autoloading

composer.json of the package that is not detected:

{
  ...,

  "autoload": { ... },

  "extra": {
      "laravel": {
          "providers": [
              "JohnDoe\\BlogPackage\\BlogPackageServiceProvider"
          ]
      }
  }
}

Code example of package classes usage that not detected:

The methods are magically injected, you call the package methods on your own models without any use.

YourModel::jsonPaginate();

See: https://github.com/spatie/laravel-json-api-paginate#usage

Insolita commented 3 years ago

Thanks for report, will check it

Levivb commented 2 years ago

Laravel reads the vendor/composer/installed.json. You could check the "extra": { "laravel": { "providers": [ of all packages listed there. Keep in mind that the root composer.json could also contain excludes for packages from autoloading:

"extra": {
        "laravel": {
            "dont-discover": [

which are then not autoload anymore.

:)