快速检测作曲依赖性问题(未使用的依赖性,阴影依赖性,错位依赖性)
| Project | Dead
dependency | Shadow
dependency | Misplaced
in require | Misplaced
in require-dev | Time* |
|-------------------------------------------|---------------------|------------------------|--------------------------|-------------------------------|------------|
| maglnet/
composer-require-checker | ❌ | ✅ | ❌ | ❌ | 124 secs |
| icanhazstring/
composer-unused | ✅ | ❌ | ❌ | ❌ | 72 secs |
| shipmonk/
composer-dependency-analyser | ✅ | ✅ | ✅ | ✅ | 2 secs |
*Time measured on codebase with ~15 000 files
composer require --dev shipmonk/composer-dependency-analyserNote that this package itself has zero composer dependencies.
vendor/bin/composer-dependency-analyserExample output:
Found shadow dependencies!
(those are used, but not listed as dependency in composer.json)
• nette/utils
e.g. Nette\Utils\Strings in app/Controller/ProductController.php:24 (+ 6 more)
Found unused dependencies!
(those are listed in composer.json, but no usage was found in scanned paths)
• nette/utils
(scanned 13970 files in 2.297 s)This tool reads your composer.json and scans all paths listed in autoload & autoload-dev sections while analysing you dependencies (both packages and PHP extensions).
composer.json--configpsr/log-implementation), which exist only via provide of another packageroave/security-advisories), which ship requirements and conflicts onlyvendor/composer/installed.php, so it requires installed dependenciescomposer install --no-devrequire-dev to requirerequire to require-dev--composer-json path/to/composer.json for custom path to composer.json--dump-usages symfony/console to show usages of certain package(s), * placeholder is supported--config path/to/config.php for custom path to config file--version display version--help display usage & cli options--verbose to see more example classes & usages--show-all-usages to see all usages--format to use different output format, available are: console (default), junit--disable-ext-analysis to disable php extensions analysis (e.g. ext-xml)--ignore-unknown-classes to globally ignore unknown classes--ignore-unknown-functions to globally ignore unknown functions--ignore-shadow-deps to globally ignore shadow dependencies--ignore-unused-deps to globally ignore unused dependencies--ignore-dev-in-prod-deps to globally ignore dev dependencies in prod code--ignore-prod-only-in-dev-deps to globally ignore prod dependencies used only in dev pathsWhen a file named composer-dependency-analyser.php is located in cwd, it gets loaded automatically.
The file must return ShipMonk\ComposerDependencyAnalyser\Config\Configuration object.
You can use custom path and filename via --config cli option.
Here is example of what you can do:
…All paths are expected to exist. If you need some glob functionality, you can do it in your config file and pass the expanded list to e.g. ignoreErrorsOnPaths.
Some classes might be used only in your DIC config files. Here is a simple way to extract those:
$classNameRegex = '[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*'; // https://www.php.net/manual/en/language.oop5.basic.php
$dicFileContents = file_get_contents(__DIR__ . '/config/services.yaml');
preg_match_all(
"~$classNameRegex(?:\\\\$classNameRegex)+~", // at least one backslash
$dicFileContents,
$matches
); // or parse the yaml properly
$config->addForceUsedSymbols($matches[1]); // possibly filter by class_exists || interface_existsSimilar approach should help you to avoid false positives in unused dependencies. Another approach for DIC-only usages is to scan the generated php file, but that gave us worse results.
--composer-json to composer.json of the other codebaseNO_COLOR environment variable to disable colored output:NO_COLOR=1 vendor/bin/composer-dependency-analyserext-* analysis, your enabled extensions of your php runtime should be superset of those used in the scanned projectcomposer checkcomposer fix:cs暂无开放 Issues,或尚未同步最近议题。