shipmonk-rnd / composer-dependency-analyser

🚀 Fast detection of composer dependency issues (unused dependencies, shadow dependencies, misplaced dependencies)
MIT License
400 stars 10 forks source link

File/Dir `dev` flag priority? #141

Closed LastDragon-ru closed 3 months ago

LastDragon-ru commented 3 months ago

I faced a problem about File/Dir dev flag priority. Let's imagine that we have composer.json

{
  "autoload": {
    "psr-4": {
      "App\\": "path/to",
    }
  },
}

and we want to add path/to/dev-file.php as a dev dependency in composer-dependency-analyser.php:

$config->addPathToScan('path/to/dev-file.php', true)

Unfortunately, it will not work :( because the dev flag will be overwriten by autoload in

https://github.com/shipmonk-rnd/composer-dependency-analyser/blob/2a45e4e7f58f45bf8f63032fc2da04652aec48ff/src/Analyser.php#L278-L293


I see a few options:

1) Provide access to ComposerJson in composer-dependency-analyser.php that will allow redefine order of scanned paths.

2) Do not override file dev flag by directory flag.

What do you think? Maybe I've missed something and there is another way? (except reimplementing ComposerJson...).

janedbal commented 3 months ago

I think the proper way is to process paths from shortest to longest. That way any more specific mark overrides the more genetic one no matter how those were added to the list.

janedbal commented 3 months ago

Fixed in https://github.com/shipmonk-rnd/composer-dependency-analyser/pull/142.

For older versions, you can disable automatic autoload and adjust the order of paths manually:

$config->disableComposerAutoloadPathScan()
    ->addPathToScan('path/to', false)
    ->addPathToScan('path/to/dev-file.php', true)
LastDragon-ru commented 3 months ago

Cool, I'll try it tomorrow. Thanks :)