How to detect unused composer packages?

Sometimes you should work with project developed by someone other.. Sometimes you may want to continue own project after long break. That was happened with me, I decide to back to old project after an 8 month of break. When i looket at composer.json, i couldn’t remember, why half of the dependencies were installed. It seems I tried to choose the best packages from several similar.

I thought that some automatic way must be existed for detect which packages really used in project. I searched it in google, in stackoverflow… but found nothing. So i decide to dive deeper in composer autoloading.

Every application entrypoint include file ‘vendor/autoload.php’, which include file ‘vendor/composer/autoload_real.php’ . It collect data from files ‘autoload_classmap.php’, ‘autoload_namespaces.php, ‘autoload_psr4.php’, that represent the arrays of matching the namespace and actual file location. Well, we can get all of namespaces provided by installed packages and check which used in our project, but how we can to relate them with composer packages names?? It made me think for a while… and then it hit me, by the composer convention, each package named as vendor_name/package_name. This means that the beginning of the file path will always match the name of the package!

So i wrote simple utility that helped me identify unnecessary packages. You can get it also

Install it as global composer package, for using in any project
composer global require insolita/unused-scanner

Put configuration file in root of your project, named as scanner_config.php

$projectPath = __DIR__ ;
//Declare directories which contains php code$scanDirectories = [
$projectPath . '/app/',
$projectPath . '/Acme/',
$projectPath . '/resources/views/',
//Optionally declare standalone files
$scanFiles = [
$projectPath . '/helpers.php',
return [
'composerJsonPath' => $projectPath . '/composer.json', 'vendorPath' => $projectPath . '/vendor/',
'scanDirectories' => $scanDirectories,

In terminal change work directory to your project root, and run

unused_scanner scanner_config.php

#php,#yii,#laravel,#javascript, #python,#linux,#archlinux,#vue

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store