iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐷

[PHP] I created a static analysis tool to list classes dependent on specific classes or namespaces

に公開

Overview

I created a static analysis tool that analyzes classes depending on specific classes or namespaces.

https://github.com/tasuku43/dependency-analyzer

It feels very much like reinventing the wheel, but I was interested in how static analysis tools are built, so I'm fine with it.

I envision use cases such as helping with major version upgrades of dependent libraries.
For example, if you know a specific class has breaking changes, you can immediately check which classes in your project are affected.
By combining it with library upgrade tools like dependabot, it's also possible to comment on the generated PR with the affected classes.

Install

It is published on packagist, so it can be installed via composer.

composer require --dev tasuku43/dependency-analyzer

Usage

We will use laravel/laravel as a sample project to verify the operation.

$ composer create-project laravel/laravel example-app
$ cd example-app
$ composer require --dev tasuku43/dependency-analyzer
$ tree -L 1
.
├── README.md
├── app
├── artisan
├── bootstrap
├── composer.json
├── composer.lock
├── config
├── database
├── lang
├── package.json
├── phpunit.xml
├── public
├── resources
├── routes
├── storage
├── tests
├── vendor
└── webpack.mix.js

11 directories, 7 files

Check the list of classes that depend on a specified namespace

This is intended for scenarios such as performing analysis on the namespace of a library targeted for a version upgrade.

$ ./vendor/bin/dependency-analyzer analyse --path app --pattern "Illuminate\Support"
 [============================] 100%
 ---------------- ------------------------------------
  Depender         App\Providers\AppServiceProvider
 ---------------- ------------------------------------
  Dependent List   Illuminate\Support\ServiceProvider
 ---------------- ------------------------------------

 ---------------- -----------------------------------
  Depender         App\Providers\AuthServiceProvider
 ---------------- -----------------------------------
  Dependent List   Illuminate\Support\Facades\Gate
 ---------------- -----------------------------------

 ---------------- ----------------------------------------
  Depender         App\Providers\RouteServiceProvider
 ---------------- ----------------------------------------
  Dependent List   Illuminate\Support\Facades\RateLimiter
                   Illuminate\Support\Facades\Route
 ---------------- ----------------------------------------

 ---------------- ----------------------------------------
  Depender         App\Providers\BroadcastServiceProvider
 ---------------- ----------------------------------------
  Dependent List   Illuminate\Support\Facades\Broadcast
                   Illuminate\Support\ServiceProvider
 ---------------- ----------------------------------------

 ---------------- ------------------------------------
  Depender         App\Providers\EventServiceProvider
 ---------------- ------------------------------------
  Dependent List   Illuminate\Support\Facades\Event
 ---------------- ------------------------------------

 ---------------- ---------------------------------------------
  Depender         App\Http\Middleware\RedirectIfAuthenticated
 ---------------- ---------------------------------------------
  Dependent List   Illuminate\Support\Facades\Auth
 ---------------- ---------------------------------------------


 [OK] Found 6 dependers

Check the list of classes that depend on a specified class

This is intended for scenarios such as searching for classes that depend on a specific class that has breaking changes during a version upgrade.

$ ./vendor/bin/dependency-analyzer analyse --path app --pattern "Illuminate\Support\ServiceProvider"
 [============================] 100%
 ---------------- ------------------------------------
  Depender         App\Providers\AppServiceProvider
 ---------------- ------------------------------------
  Dependent List   Illuminate\Support\ServiceProvider
 ---------------- ------------------------------------

 ---------------- ----------------------------------------
  Depender         App\Providers\BroadcastServiceProvider
 ---------------- ----------------------------------------
  Dependent List   Illuminate\Support\ServiceProvider
 ---------------- ----------------------------------------


 [OK] Found 2 dependers

Check the list of classes that depend on a specified class, grouped by "dependent classes"

You can change the display format by adding the group-by option. While the default view lists "dependent classes" for each "depender class," specifying --group-by dependent allows you to list "depender classes" for each "dependent class."

$ ./vendor/bin/dependency-analyzer analyse --path app --pattern "Illuminate\\Support\\ServiceProvider" --group-by dependent
 [============================] 100%
 --------------- ----------------------------------------
  Dependent       Illuminate\\Support\\ServiceProvider
 --------------- ----------------------------------------
  Depender List   App\\Providers\\AppServiceProvider
                  App\\Providers\\BroadcastServiceProvider
 --------------- ----------------------------------------


 [OK] Found 1 dependents

Conclusion

I would be happy if you could give it a try.

Discussion